在python

时间:2018-03-18 05:30:06

标签: python list time-complexity grouping

给定这样的列表,其中第一列是id,第二列是字符串,

[ [2, ["00_01_02"]],
  [1, ["00_03_04"]],
  [3, ["00_03_04"]],
  [6, ["00_03_04"]],
  [4, ["01_02_03"]],
  [5, ["01_02_03"]],    
 ]

正如您所看到的,相邻的元素是相同的。例如,id 1,3和6具有相同的字符串,因此我想将它们分组到另一个列表中。对于id 5和4也是如此。我们还可以假设列表按字符串排序。

我想解决这个问题 O(NC)如果可能,其中N是列表中元素的数量,C是字符串中的字符数。

示例输出类似于

[ [[1,3,6], ["00_03_04"]],
  [[4,5]  , ["01_02_03"]] ]

3 个答案:

答案 0 :(得分:1)

使用itertools提高效率 -

a = [ [2, ["00_01_02"]],
  [1, ["00_03_04"]],
  [3, ["00_03_04"]],
  [6, ["00_03_04"]],
  [4, ["01_02_03"]],
  [5, ["01_02_03"]],    
  [7, ["00_03_04"]],
 ]

from itertools import groupby
from operator import itemgetter

print([ [[ g[0] for g in grp], key] for key, grp in groupby(a, key=itemgetter(1))])

<强>输出

[[[2], ['00_01_02']], [[1, 3, 6], ['00_03_04']], [[4, 5], ['01_02_03']], [[7], ['00_03_04']]]

答案 1 :(得分:0)

你绝对应该使用collectins.defaultdict

from collections import defaultdict
l = [ [2, ["00_01_02"]],
  [1, ["00_03_04"]],
  [3, ["00_03_04"]],
  [6, ["00_03_04"]],
  [4, ["01_02_03"]],
  [5, ["01_02_03"]],    
 ]

for v, k in l:
    new_d[k[0]].append(v)


new_d
Out[102]: defaultdict(list, {'00_01_02': [2], '00_03_04': [1, 3, 6], '01_02_03': [4, 5]})

如果您想要所需的类似输出。

[[v,[k]] for k,v in new_d.items() if len(v) > 1]
Out[118]: [[[1, 3, 6], ['00_03_04']], [[4, 5], ['01_02_03']]]

答案 2 :(得分:0)

您可以尝试一行:

[[[2, ['00_01_02']]], [[1, ['00_03_04']], [3, ['00_03_04']], [6, ['00_03_04']]], [[4, ['01_02_03']], [5, ['01_02_03']]]]

输出:

similar={}
for j in data:
    if j[1][0] not in similar:
        similar[j[1][0]]=[j[0]]
    else:
        similar[j[1][0]].append(j[0])
print(similar)

如果您不想使用任何导入,那么:

{'01_02_03': [4, 5], '00_01_02': [2], '00_03_04': [1, 3, 6]}

输出:

woocommerce_new_booking