合并元组列表

时间:2017-10-13 12:49:37

标签: python

实现以下目标的pythonic方式是什么?

从:

l1 = ([('ADJ', 29), ('CONJ', 1), ('PRT', 2), ('X', 3), ('ADV', 18), ('VERB', 52), ('ADP', 1), ('NOUN', 27)])
l2 = ([('ADJ', 312), ('INTJ', 2), ('ADP', 5), ('PART', 6), ('DET', 2), ('ADV', 323), ('VERB', 1196), ('NOUN', 1162)])

到:

l3 = ([('ADJ', 312,29), ('ADP', 5,1), ('ADV', 323,18), ('VERB', 1196,52), ('NOUN', 1162,27)])

3 个答案:

答案 0 :(得分:1)

你可以试试这个:

l1 = ([('ADJ', 29), ('CONJ', 1), ('PRT', 2), ('X', 3), ('ADV', 18), ('VERB', 52), ('ADP', 1), ('NOUN', 27)])
l2 = ([('ADJ', 312), ('INTJ', 2), ('ADP', 5), ('PART', 6), ('DET', 2), ('ADV', 323), ('VERB', 1196), ('NOUN', 1162)])
d1 = dict(l1)
d2 = dict(l2)
new_data = tuple([(c, d1[c], d2.get(c, None)) for c in d1])    
final_data = list(filter(lambda x:len(x) > 2, tuple(map(tuple, tuple([(b for b in i if b is not None) for i in new_data])))))

输出:

[('ADV', 18, 323), ('NOUN', 27, 1162), ('ADP', 1, 5), ('VERB', 52, 1196), ('ADJ', 29, 312)]

答案 1 :(得分:0)

我的意思是你可以这样做:

Sub GetFolder() Dim FD As Office.FileDialog Dim FolderPath as string Set FD = Application.FileDialog(msoFileDialogFolderPicker) FD.Show FolderPath = FD.SelectedItems(1) Msgbox FolderPath End Sub

我相信这会做你所要求的(但我现在无法测试)

答案 2 :(得分:0)

我不确定python是否有一个内容,但你可以做的是使用字典合并两个列表。

l1 = ([('ADJ', 29), ('CONJ', 1), ('PRT', 2), ('X', 3), ('ADV', 18), ('VERB', 52), ('ADP', 1), ('NOUN', 27)])
l2 = ([('ADJ', 312), ('INTJ', 2), ('ADP', 5), ('PART', 6), ('DET', 2), ('ADV', 323), ('VERB', 1196), ('NOUN', 1162)])
d2 = dict(l2)

#Merge l1 and d2
result = [(k, v, d2[k]) for k, v in l1]