从列表中提取列表并使用新格式打印每个列表 - Python 3.6

我有一个以下格式的列表,包括末尾的额外方括号:


[[[['10.0.0.0-E', '10.0.0.0-B'], ['172.0.0.0-E', '172.0.0.0-B'], ['12.0.0.0-E', '12.0.0.0-B']]]]

我想拿走这份清单并打印出以下内容:


10.0.0.0-E - 10.0.0.0-B


172.0.0.0-E - 172.0.0.0-B


12.0.0.0-E - 12.0.0.0-B

额外的括号把我搞砸了吗?


我试过这个..


newList = []

for item in result:

    newList.append(item[0].split(","))

print(newList)


潇湘沐
浏览 155回答 5
5回答

慕运维8079593

您的列表是列表中的列表中的列表,这解释了额外的括号。这有效:result = [[[['10.0.0.0-E', '10.0.0.0-B'], ['172.0.0.0-E', '172.0.0.0-B'], ['12.0.0.0-E', '12.0.0.0-B']]]]for item in result[0][0]:    print(f'{item[0]} - {item[1]}')result[0][0]选择 的第一个元素result(一个列表),然后选择其第一个元素(另一个列表) - for 循环然后将该列表的每个元素item一次分配给一个。print(f'{item[0]} - {item[1]}')获取该项目并打印您需要的格式化字符串:10.0.0.0-E - 10.0.0.0-B172.0.0.0-E - 172.0.0.0-B12.0.0.0-E - 12.0.0.0-B

DIEA

我希望这对我的朋友有帮助:a = [[[['10.0.0.0-E', '10.0.0.0-B'], ['172.0.0.0-E', '172.0.0.0-B'], ['12.0.0.0-E', '12.0.0.0-B']]]]main_list = a[0][0]for i in range(len(main_list)):    print(main_list[i][0], '-' ,main_list[i][1])输出:10.0.0.0-E - 10.0.0.0-B172.0.0.0-E - 172.0.0.0-B12.0.0.0-E - 12.0.0.0-B

SMILET

所以你基本上有四个级别的嵌套列表:[ # <- first level&nbsp; &nbsp; [ # <- second level&nbsp; &nbsp; &nbsp; &nbsp; [ # <- third level&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ['10.0.0.0-E', '10.0.0.0-B'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ['172.0.0.0-E', '172.0.0.0-B'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ['12.0.0.0-E', '12.0.0.0-B']&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ]]在这种情况下,第一级和第二级并不重要。您想要迭代第三层并连接字符串。首先删除这两个外部级别:my_list = [[[['10.0.0.0-E', '10.0.0.0-B'], ['172.0.0.0-E', '172.0.0.0-B'], ['12.0.0.0-E', '12.0.0.0-B']]]]list_of_pairs = my_list[0][0]然后迭代list_of_pairs使用for pair in list_of_pairs并连接字符串:for pair in list_of_pairs:&nbsp; &nbsp; print(' - '.join(pair))把它们放在一起:my_list = [[[['10.0.0.0-E', '10.0.0.0-B'], ['172.0.0.0-E', '172.0.0.0-B'], ['12.0.0.0-E', '12.0.0.0-B']]]]for pair in my_list[0][0]:&nbsp; &nbsp; print(' - '.join(pair))或者,摆脱显式的 for 循环:my_list = [[[['10.0.0.0-E', '10.0.0.0-B'], ['172.0.0.0-E', '172.0.0.0-B'], ['12.0.0.0-E', '12.0.0.0-B']]]]print('\n'.join(' - '.join(pair) for pair in my_list[0][0]))

眼眸繁星

假设内部列表始终是一对字符串:from functools import reducea = [[[['10.0.0.0-E', '10.0.0.0-B'], ['172.0.0.0-E', '172.0.0.0-B'], ['12.0.0.0-E', '12.0.0.0-B']]]]if a:&nbsp; &nbsp; while type(a[0]) == list:&nbsp; &nbsp; &nbsp; &nbsp; a = reduce(lambda x,y:x+y,a)for i in range(0,len(a),2):&nbsp; &nbsp; print(a[i],a[i+1])

幕布斯6054654

您是否尝试过像这样循环指定索引for i in x[0][0]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; print(i)&nbsp;Result&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;['10.0.0.0-E', '10.0.0.0-B']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;['172.0.0.0-E', '172.0.0.0-B']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;['12.0.0.0-E', '12.0.0.0-B']或者你当然可以像这个例子一样使用end=''&nbsp;for i in x[0][0]:&nbsp; &nbsp; for j in I:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; print(j, end='')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; print(end='\n')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;Result:&nbsp; 10.0.0.0-E10.0.0.0-B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; 172.0.0.0-E172.0.0.0-B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; 12.0.0.0-E12.0.0.0-B&nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP