根据第一个元素组合嵌套列表
Combining nested lists according to first element
所以我有三个列表:
lst1 = [('test1', 0.2), ('test7', 0.2)]
lst2 = [('test1', 5.2), ('test2', 11.1), ('test7', 0.2)]
lst3 = [('test1', 19.2), ('test2', 12.1), ('test7', 19.2), ('test9', 15.1)]
我想做的是遍历列表并创建以下元组:
[(test1, 0.2, 5.2, 19.2), (test2, 0.0, 11.1, 12.1), (test7, 0.2, 0.2, 19.2), (test9, 0.0, 0.0, 15.1)]
我已经尝试通过多种方法解决这个问题,但没有成功,欢迎任何帮助!
如果你真的想使用 test1
等作为唯一键,你最好使用字典。
我建议如下:使用 itertools.chain
to combine the lists that you want to iterate over, and use a default dictionary,您只需将项目附加到。
import itertools as it
from collections import defaultdict
lst1 = [('test1', 0.2), ('test7', 0.2)]
lst2 = [('test1', 5.2), ('test2', 11.1), ('test7', 0.2)]
lst3 = [('test1', 19.2), ('test2', 12.1), ('test7', 19.2), ('test9', 15.1)]
mydict = defaultdict(list)
for key, value in it.chain(lst1, lst2, lst3):
mydict[key].append(value)
print(mydict)
> defaultdict(
<class 'list'>,
{'test1': [0.2, 5.2, 19.2],
'test7': [0.2, 0.2, 19.2],
'test2': [11.1, 12.1],
'test9': [15.1]}
)
lst1 = [('test1', 0.2), ('test7', 0.2)]
lst2 = [('test1', 5.2), ('test2', 11.1), ('test7', 0.2)]
lst3 = [('test1', 19.2), ('test2', 12.1), ('test7', 19.2), ('test9', 15.1)]
# create a list containing all these lists
lst_of_lists = [lst1, lst2, lst3]
# Create a dictionary where keys are the test1, test2, test3...
# and the values are [0.0, 0.0, ....] as many as the number of lists
output_dict = {
name:[0.0]*len(lst_of_lists) for lst in lst_of_lists
for (name, value) in lst
}
# Now, 'test1' in lst1 has to modify output_dict['test1'][0]
# 'test1' in lst2 modifies output_dict['test1'][1]
# and so on...
for idx, lst in enumerate(lst_of_lists):
for key, value in lst:
output_dict[key][idx] = value
# this gives you a dictionary
print("output_dict: ", output_dict)
# you can convert it back to a list with .items(), and since you want it sorted
print("dict_as_list: ", sorted(output_dict.items()))
# since you want the keys and values together in the same tuple
output_list = [tuple([key] + val_list) for key, val_list in output_dict.items()]
# and sort that
output_list = sorted(output_list)
print("output_list: ", output_list)
输出:
output_dict: {'test1': [0.2, 5.2, 19.2], 'test7': [0.2, 0.2, 19.2], 'test2': [0.0, 11.1, 12.1], 'test9': [0.0, 0.0, 15.1]}
dict_as_list: [('test1', [0.2, 5.2, 19.2]), ('test2', [0.0, 11.1, 12.1]), ('test7', [0.2, 0.2, 19.2]), ('test9', [0.0, 0.0, 15.1])]
output_list: [('test1', 0.2, 5.2, 19.2), ('test2', 0.0, 11.1, 12.1), ('test7', 0.2, 0.2, 19.2), ('test9', 0.0, 0.0, 15.1)]
所以我有三个列表:
lst1 = [('test1', 0.2), ('test7', 0.2)]
lst2 = [('test1', 5.2), ('test2', 11.1), ('test7', 0.2)]
lst3 = [('test1', 19.2), ('test2', 12.1), ('test7', 19.2), ('test9', 15.1)]
我想做的是遍历列表并创建以下元组:
[(test1, 0.2, 5.2, 19.2), (test2, 0.0, 11.1, 12.1), (test7, 0.2, 0.2, 19.2), (test9, 0.0, 0.0, 15.1)]
我已经尝试通过多种方法解决这个问题,但没有成功,欢迎任何帮助!
如果你真的想使用 test1
等作为唯一键,你最好使用字典。
我建议如下:使用 itertools.chain
to combine the lists that you want to iterate over, and use a default dictionary,您只需将项目附加到。
import itertools as it
from collections import defaultdict
lst1 = [('test1', 0.2), ('test7', 0.2)]
lst2 = [('test1', 5.2), ('test2', 11.1), ('test7', 0.2)]
lst3 = [('test1', 19.2), ('test2', 12.1), ('test7', 19.2), ('test9', 15.1)]
mydict = defaultdict(list)
for key, value in it.chain(lst1, lst2, lst3):
mydict[key].append(value)
print(mydict)
> defaultdict(
<class 'list'>,
{'test1': [0.2, 5.2, 19.2],
'test7': [0.2, 0.2, 19.2],
'test2': [11.1, 12.1],
'test9': [15.1]}
)
lst1 = [('test1', 0.2), ('test7', 0.2)]
lst2 = [('test1', 5.2), ('test2', 11.1), ('test7', 0.2)]
lst3 = [('test1', 19.2), ('test2', 12.1), ('test7', 19.2), ('test9', 15.1)]
# create a list containing all these lists
lst_of_lists = [lst1, lst2, lst3]
# Create a dictionary where keys are the test1, test2, test3...
# and the values are [0.0, 0.0, ....] as many as the number of lists
output_dict = {
name:[0.0]*len(lst_of_lists) for lst in lst_of_lists
for (name, value) in lst
}
# Now, 'test1' in lst1 has to modify output_dict['test1'][0]
# 'test1' in lst2 modifies output_dict['test1'][1]
# and so on...
for idx, lst in enumerate(lst_of_lists):
for key, value in lst:
output_dict[key][idx] = value
# this gives you a dictionary
print("output_dict: ", output_dict)
# you can convert it back to a list with .items(), and since you want it sorted
print("dict_as_list: ", sorted(output_dict.items()))
# since you want the keys and values together in the same tuple
output_list = [tuple([key] + val_list) for key, val_list in output_dict.items()]
# and sort that
output_list = sorted(output_list)
print("output_list: ", output_list)
输出:
output_dict: {'test1': [0.2, 5.2, 19.2], 'test7': [0.2, 0.2, 19.2], 'test2': [0.0, 11.1, 12.1], 'test9': [0.0, 0.0, 15.1]}
dict_as_list: [('test1', [0.2, 5.2, 19.2]), ('test2', [0.0, 11.1, 12.1]), ('test7', [0.2, 0.2, 19.2]), ('test9', [0.0, 0.0, 15.1])]
output_list: [('test1', 0.2, 5.2, 19.2), ('test2', 0.0, 11.1, 12.1), ('test7', 0.2, 0.2, 19.2), ('test9', 0.0, 0.0, 15.1)]