基于重复值合并嵌套列表项 [英] Merge nested list items based on a repeating value
问题描述
尽管编写得很糟糕,但是该代码:
Although poorly written, this code:
marker_array = [['hard','2','soft'],['heavy','2','light'],['rock','2','feather'],['fast','3'], ['turtle','4','wet']]
marker_array_DS = []
for i in range(len(marker_array)):
if marker_array[i-1][1] != marker_array[i][1]:
marker_array_DS.append(marker_array[i])
print marker_array_DS
返回:
[['hard', '2', 'soft'], ['fast', '3'], ['turtle', '4', 'wet']]
它完成了部分任务,即创建一个包含所有嵌套列表的新列表,除了那些在索引[1]中具有重复值的嵌套列表.但是我真正需要的是将已删除列表中的匹配索引值连接起来,创建一个像这样的列表:
It accomplishes part of the task which is to create a new list containing all nested lists except those that have duplicate values in index [1]. But what I really need is to concatenate the matching index values from the removed lists creating a list like this:
[['hard heavy rock', '2', 'soft light feather'], ['fast', '3'], ['turtle', '4', 'wet']]
不得将索引[1]中的值连接在一起.我设法使用另一篇文章中的技巧来完成串联部分:
The values in index [1] must not be concatenated. I kind of managed to do the concatenation part using a tip from another post:
newlist = [i + n for i, n in zip(list_a, list_b]
但是我正在努力寻找产生期望结果的方法. "marker_array"列表在传递给此代码之前已经按照升序排序.索引[1]位置中的所有相似值都将是连续的.如上所述,某些嵌套列表可能没有[0]和[1]以外的任何值.
But I am struggling with figuring out the way to produce the desired result. The "marker_array" list will be already sorted in ascending order before being passed to this code. All like-values in index [1] position will be contiguous. Some nested lists may not have any values beyond [0] and [1] as illustrated above.
推荐答案
from collections import defaultdict
d1, d2 = defaultdict(list) , defaultdict(list)
for pxa in marker_array:
d1[pxa[1]].extend(pxa[:1])
d2[pxa[1]].extend(pxa[2:])
res = [[' '.join(d1[x]), x, ' '.join(d2[x])] for x in sorted(d1)]
如果您确实需要2元组(我认为不太可能):
If you really need 2-tuples (which I think is unlikely):
for p in res:
if not p[-1]:
p.pop()
这篇关于基于重复值合并嵌套列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!