更新:列表中填充了我编辑列表的字符串以显示此
我有3个不同的列表,例如
Section = [('1', '1.1', '1.2'), ('1', '2', '2.2', '3'), ('1', '1.2', '3.2', '3.5')]
Page = [('1', '1', '3'), ('1', '2', '2', '2'), ('1', '2', '3', '5')]
Titles = [('General', 'Info', 'Titles'), ('More', 'Info', 'Section', 'Here'), ('Another', 'List', 'Of', 'Strings')]
我希望将它们组合起来,例如
Combined_List = [('1', '1.1', '1.2'), ('1', '2', '2.2', '3'), ('1', '1.2', '3.2', '3.5'),
('1', '1', '3'), ('1', '2', '2', '2'), ('1', '2', '3', '5'),
('General', 'Info', 'Titles'), ('More', 'Info', 'Section', 'Here'), ('Another', 'List', 'Of', 'Strings')]
或任何其他形式,允许我按照标题为部分的列表中的数字对它们进行排序。
在这种情况下,它将是
Sorted_list = [('1', '1', '1', '1.1', '1.2', '1.2', '2', '2.2', '3', '3.2', '3.5'),
('1', '1', '1', '1', '3', '2', '2', '2', '2', '3', '5'),
('General', 'More', 'Another', 'Info', 'Titles', 'List', 'Info', 'Section', 'Here', 'Of', 'Strings')
我需要这样,所以我最终可以通过Section将排序列表导出到excel中。如果您能想出更好的显示/格式化方法,请分享!
答案 0 :(得分:2)
你可以这样做:
from itertools import chain
tuples = zip(map(float, list(chain(*Section))),
list(chain(*Page)),
list(chain(*Title)))
zip(*sorted(tuples, key=lambda x: x[0]))
Out[232]:
[(1.0, 1.0, 1.0, 1.1, 1.2, 1.2, 2.0, 2.2, 3.0, 3.2, 3.5),
('1', '1', '1', '1', '3', '2', '2', '2', '2', '3', '5'),
('General',
'More',
'Another',
'Info',
'Titles',
'List',
'Info',
'Section',
'Here',
'Of',
'Strings')]
在这里,您首先要删除您的三个列表(list(chain(*L))
做了什么)并将它们打包在zip
的元组中。提示"元组"看它看起来如何。
然后在第二行代码中,您可以根据所需元组的元素应用排序。然后解压缩结果。
答案 1 :(得分:2)
试试这个,
Section = [('1', '1.1', '1.2'), ('1', '2', '2.2', '3'), ('1', '1.2', '3.2', '3.5')]
Page = [('1', '1', '3'), ('1', '2', '2', '2'), ('1', '2', '3', '5')]
Titles = [('General', 'Info', 'Titles'), ('More', 'Info', 'Section', 'Here'), ('Another', 'List', 'Of', 'Strings')]
# Flat a list of tuples into a list
l1 = [item for sublist in Section for item in sublist]
l2 = [item for sublist in Page for item in sublist]
l3 = [item for sublist in Titles for item in sublist]
# Python2, `zip` returns a list of tuples
#result = zip(*sorted(zip(l1, l2, l3), key=lambda x:float(x[0])))
# Python3, `zip` returns an iterator of tuples
result = list(zip(*sorted(zip(l1, l2, l3), key=lambda x:float(x[0]))))
print(result)
# Output
[ ('1', '1', '1', '1.1', '1.2', '1.2', '2', '2.2', '3', '3.2', '3.5'),
('1', '1', '1', '1', '3', '2', '2', '2', '2', '3', '5'),
('General', 'More', 'Another', 'Info', 'Titles', 'List', 'Info', 'Section', 'Here', 'Of', 'Strings')]
答案 2 :(得分:-1)
我的方法。列表理解,无需导入模块。我认为它很快,而且很简单。
编辑:我添加了一个未排序的方法和一个排序的方法。
#Unsorted
newList =[
[item for sublist in Section for item in sublist],
[item for sublist in Page for item in sublist],
[item for sublist in Titles for item in sublist]
]
print newList
#Output
#[[1, 1.1, 1.2, 1, 2, 2.2, 3, 1, 1.2, 3.2, 3.5],
# [1, 1, 3, 1, 2, 2, 2, 1, 2, 3, 5],
# ['General', 'Info', 'Titles', 'More', 'Info', 'Section', 'Here', 'Another', 'List', 'Of', 'Strings']]
#Sort first two lists afterwards, if desired
for i in range(2):
newList[i].sort()
print newList
#Output
#[[1, 1, 1, 1.1, 1.2, 1.2, 2, 2.2, 3, 3.2, 3.5],
# [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 5],
# ['General', 'Info', 'Titles', 'More', 'Info', 'Section', 'Here', 'Another', 'List', 'Of', 'Strings']]