list中元素根据需求进行相加,最后输出累加后完整的那个元素该怎么做呢?
本帖最后由 m823433263 于 2020-5-7 10:17 编辑下面是示例list,我自己根据需求也写了下,但是不太对,这样写每次相加的结果都会打印出一次,目的是只要最后累加完的那个完整的元素;
大佬们帮忙看看怎么解决,我也学习一下~
list = ['xgei-0/0/0/1 optical Duplex/full10000up up up UT-', '-Gig-11/0/0-10G', 'xgei-0/0/0/2 optical Duplex/full10000up up up UT-', '-Gig-1/10/2/3-10', 'G', 'xgei-0/0/0/3 optical Duplex/full10000down down down UT-', '-Gig-1/0/4-10G']
for i in range(len(list)):
if 'up' in list or 'down' in list:
port_info = list
# print(port_info)
else:
des_info = list
# print(des_info)
port_info = port_info + des_info
print(port_info)
上面表达的有问题,下面是打印出来的结果,以及需求需要的东西
xgei-0/0/0/1 optical Duplex/full10000up up up UT-
xgei-0/0/0/1 optical Duplex/full10000up up up UT--Gig-11/0/0-10G ####需要这个元素
xgei-0/0/0/2 optical Duplex/full10000up up up UT-
xgei-0/0/0/2 optical Duplex/full10000up up up UT--Gig-1/10/2/3-10
xgei-0/0/0/2 optical Duplex/full10000up up up UT--Gig-1/10/2/3-10G ####需要这个元素
xgei-0/0/0/3 optical Duplex/full10000down down down UT-
xgei-0/0/0/3 optical Duplex/full10000down down down UT--Gig-1/0/4-10G ####需要这个元素 把print挪出去:
list = ['xgei-0/0/0/1 optical Duplex/full10000up up up UT-', '-Gig-11/0/0-10G', 'xgei-0/0/0/2 optical Duplex/full10000up up up UT-', '-Gig-1/10/2/3-10', 'G', 'xgei-0/0/0/3 optical Duplex/full10000down down down UT-', '-Gig-1/0/4-10G']
for i in range(len(list)):
if 'up' in list or 'down' in list:
port_info = list
# print(port_info)
else:
des_info = list
# print(des_info)
port_info = port_info + des_info
print(port_info) qiuyouzhi 发表于 2020-5-7 10:05
把print挪出去:
这才一分钟,就写出来了,我还在想题,大佬已经解出来了?!{:10_247:} Pythonnewers 发表于 2020-5-7 10:07
这才一分钟,就写出来了,我还在想题,大佬已经解出来了?!
看ta说的问题咯,ta说只想要最后一次打印的数据,
而不是每次相加都打印,直接找出for循环里面的print,
挪出去就好。 qiuyouzhi 发表于 2020-5-7 10:05
把print挪出去:
不好意思,大佬,我刚刚表达的有点问题,我又重新编辑了下,麻烦再看一下 m823433263 发表于 2020-5-7 10:18
不好意思,大佬,我刚刚表达的有点问题,我又重新编辑了下,麻烦再看一下
简单粗暴:
list = ['xgei-0/0/0/1 optical Duplex/full10000up up up UT-', '-Gig-11/0/0-10G', 'xgei-0/0/0/2 optical Duplex/full10000up up up UT-', '-Gig-1/10/2/3-10', 'G', 'xgei-0/0/0/3 optical Duplex/full10000down down down UT-', '-Gig-1/0/4-10G']
for i in range(len(list)):
if 'up' in list or 'down' in list:
port_info = list
# print(port_info)
else:
des_info = list
# print(des_info)
port_info = port_info + des_info
if port_info[-1] == "G":
print(port_info) m823433263 发表于 2020-5-7 10:18
不好意思,大佬,我刚刚表达的有点问题,我又重新编辑了下,麻烦再看一下
怎么累加的 永恒的蓝色梦想 发表于 2020-5-7 10:23
怎么累加的
是指累加的规则吗? qiuyouzhi 发表于 2020-5-7 10:22
简单粗暴:
哈哈,我傻了{:10_257:},对于这个示例没问题,但实际情况可能会有不同的 m823433263 发表于 2020-5-7 10:28
哈哈,我傻了,对于这个示例没问题,但实际情况可能会有不同的
那你可以给它加一个特殊标记 写出来了{:5_109:}
import re
list = ['xgei-0/0/0/1 optical Duplex/full10000up up up UT-', '-Gig-11/0/0-10G', 'xgei-0/0/0/2 optical Duplex/full10000up up up UT-', '-Gig-1/10/2/3-10', 'G', 'xgei-0/0/0/3 optical Duplex/full10000down down down UT-', '-Gig-1/0/4-10G']
target_list = []
for i in range(len(list)):
max_len = len(list)
if 'up' in list or 'down' in list:
port_info = list
# print(port_info)
j = i
while re.search('up', list) == None and re.search('down', list) == None:
port_info = port_info + list
j = j + 1
j = max_len
break
target_list.append(port_info)
print(target_list)
页:
[1]