鱼C论坛

 找回密码
 立即注册
查看: 1031|回复: 10

list中元素根据需求进行相加,最后输出累加后完整的那个元素该怎么做呢?

[复制链接]
发表于 2020-5-7 10:04:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 m823433263 于 2020-5-7 10:17 编辑

下面是示例list,我自己根据需求也写了下,但是不太对,这样写每次相加的结果都会打印出一次,目的是只要最后累加完的那个完整的元素;
大佬们帮忙看看怎么解决,我也学习一下~

  1. list = ['xgei-0/0/0/1   optical    Duplex/full  10000  up    up    up    UT-[HW', '-RT01-NE5000E]', '-Gig-11/0/0-10G', 'xgei-0/0/0/2   optical    Duplex/full  10000  up    up    up    UT-[HW', '-RT02-NE5000E]', '-Gig-1/10/2/3-10', 'G', 'xgei-0/0/0/3   optical    Duplex/full  10000  down    down    down    UT-[HW', '-RT01-NE5000E]', '-Gig-1/0/4-10G']

  2. for i in range(len(list)):
  3.     if 'up' in list[i] or 'down' in list[i]:
  4.         port_info = list[i]
  5.         # print(port_info)
  6.     else:
  7.         des_info = list[i]
  8.         # print(des_info)
  9.         port_info = port_info + des_info
  10.         print(port_info)
复制代码


上面表达的有问题,下面是打印出来的结果,以及需求需要的东西
xgei-0/0/0/1   optical    Duplex/full  10000  up    up    up    UT-[HW-RT01-NE5000E]
xgei-0/0/0/1   optical    Duplex/full  10000  up    up    up    UT-[HW-RT01-NE5000E]-Gig-11/0/0-10G    ####需要这个元素
xgei-0/0/0/2   optical    Duplex/full  10000  up    up    up    UT-[HW-RT02-NE5000E]
xgei-0/0/0/2   optical    Duplex/full  10000  up    up    up    UT-[HW-RT02-NE5000E]-Gig-1/10/2/3-10
xgei-0/0/0/2   optical    Duplex/full  10000  up    up    up    UT-[HW-RT02-NE5000E]-Gig-1/10/2/3-10G   ####需要这个元素
xgei-0/0/0/3   optical    Duplex/full  10000  down    down    down    UT-[HW-RT01-NE5000E]
xgei-0/0/0/3   optical    Duplex/full  10000  down    down    down    UT-[HW-RT01-NE5000E]-Gig-1/0/4-10G    ####需要这个元素
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-7 10:05:19 | 显示全部楼层
把print挪出去:
  1. list = ['xgei-0/0/0/1   optical    Duplex/full  10000  up    up    up    UT-[HW', '-RT01-NE5000E]', '-Gig-11/0/0-10G', 'xgei-0/0/0/2   optical    Duplex/full  10000  up    up    up    UT-[HW', '-RT02-NE5000E]', '-Gig-1/10/2/3-10', 'G', 'xgei-0/0/0/3   optical    Duplex/full  10000  down    down    down    UT-[HW', '-RT01-NE5000E]', '-Gig-1/0/4-10G']

  2. for i in range(len(list)):
  3.     if 'up' in list[i] or 'down' in list[i]:
  4.         port_info = list[i]
  5.         # print(port_info)
  6.     else:
  7.         des_info = list[i]
  8.         # print(des_info)
  9.         port_info = port_info + des_info
  10. print(port_info)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-7 10:07:21 | 显示全部楼层

这才一分钟,就写出来了,我还在想题,大佬已经解出来了?!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-7 10:09:23 | 显示全部楼层
Pythonnewers 发表于 2020-5-7 10:07
这才一分钟,就写出来了,我还在想题,大佬已经解出来了?!

看ta说的问题咯,ta说只想要最后一次打印的数据,
而不是每次相加都打印,直接找出for循环里面的print,
挪出去就好。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-7 10:18:51 | 显示全部楼层

不好意思,大佬,我刚刚表达的有点问题,我又重新编辑了下,麻烦再看一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-7 10:22:46 | 显示全部楼层
m823433263 发表于 2020-5-7 10:18
不好意思,大佬,我刚刚表达的有点问题,我又重新编辑了下,麻烦再看一下

简单粗暴:
  1. list = ['xgei-0/0/0/1   optical    Duplex/full  10000  up    up    up    UT-[HW', '-RT01-NE5000E]', '-Gig-11/0/0-10G', 'xgei-0/0/0/2   optical    Duplex/full  10000  up    up    up    UT-[HW', '-RT02-NE5000E]', '-Gig-1/10/2/3-10', 'G', 'xgei-0/0/0/3   optical    Duplex/full  10000  down    down    down    UT-[HW', '-RT01-NE5000E]', '-Gig-1/0/4-10G']

  2. for i in range(len(list)):
  3.     if 'up' in list[i] or 'down' in list[i]:
  4.         port_info = list[i]
  5.         # print(port_info)
  6.     else:
  7.         des_info = list[i]
  8.         # print(des_info)
  9.         port_info = port_info + des_info
  10.         if port_info[-1] == "G":
  11.             print(port_info)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-7 10:23:29 | 显示全部楼层
m823433263 发表于 2020-5-7 10:18
不好意思,大佬,我刚刚表达的有点问题,我又重新编辑了下,麻烦再看一下

怎么累加的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-7 10:26:07 | 显示全部楼层

是指累加的规则吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-7 10:28:50 | 显示全部楼层

哈哈,我傻了,对于这个示例没问题,但实际情况可能会有不同的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-7 10:41:27 | 显示全部楼层
m823433263 发表于 2020-5-7 10:28
哈哈,我傻了,对于这个示例没问题,但实际情况可能会有不同的

那你可以给它加一个特殊标记
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-7 15:23:41 | 显示全部楼层
写出来了
  1. import re

  2. list = ['xgei-0/0/0/1   optical    Duplex/full  10000  up    up    up    UT-[HW', '-RT01-NE5000E]', '-Gig-11/0/0-10G', 'xgei-0/0/0/2   optical    Duplex/full  10000  up    up    up    UT-[HW', '-RT02-NE5000E]', '-Gig-1/10/2/3-10', 'G', 'xgei-0/0/0/3   optical    Duplex/full  10000  down    down    down    UT-[HW', '-RT01-NE5000E]', '-Gig-1/0/4-10G']

  3. target_list = []
  4. for i in range(len(list)):
  5.     max_len = len(list)
  6.     if 'up' in list[i] or 'down' in list[i]:
  7.         port_info = list[i]
  8.         # print(port_info)
  9.         j = i
  10.         while re.search('up', list[j + 1]) == None and re.search('down', list[j + 1]) == None:
  11.             port_info = port_info + list[j + 1]
  12.             j = j + 1
  13.             j = max_len
  14.             break
  15.         target_list.append(port_info)
  16. print(target_list)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 19:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表