识别数组中的相似实例并将其合并

时间:2018-11-13 23:56:42

标签: python arrays python-3.x

我有一个数组数组,像这样...

[['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Summer Smith', 4], ['Summer Smith', 4], ['Summer Smith', 4], ['Abradolf Lincler', 4], ['Summer Smith', 4], ['Summer Smith', 4]]

我希望能够扫描每个数组,如果数组中已经存在元素0,则将它们合并并将第一个元素添加在一起。因此,例如,有5个Summer Smith实例。代码应识别出同一位玩家,因此将所有Summer Smith得分相加,从而使Summer Smith总得分为20。它应该为每个玩家做到这一点。因此,一切看起来都像

[['Harry',20], ['Jake', 16]]....

我试图...

for array in arrays:
        if array[0] in [not sure what to do now]

4 个答案:

答案 0 :(得分:2)

考虑使用dictionary跟踪每个玩家的分数总和:

scores = [['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Summer Smith', 4], ['Summer Smith', 4], ['Summer Smith', 4], ['Abradolf Lincler', 4], ['Summer Smith', 4], ['Summer Smith', 4]]

grouped_scores = {}
for name, score in scores:
    if name not in grouped_scores: grouped_scores[name] = score
    else: grouped_scores[name] += score

然后,您可以将结果作为列表列表取回:

merged_scores = [list(t) for t in grouped_scores.items()]
print(merged_scores)
# [['Summer Smith', 20], ['Scary Terry', 20], ['Abradolf Lincler', 32]]

答案 1 :(得分:1)

更好地使用setdefault

d={}
for x,y in arrays:
   d.setdefault(x,[]).append(y)
print(list(map(list,{k:sum(v) for k,v in d.items()}.items())))

输出:

[['Abradolf Lincler', 32], ['Scary Terry', 20], ['Summer Smith', 20]]

所有defaultdict最佳:

from collections import defaultdict
d=defaultdict(int)
for x,y in arrays:
   d[x]+=y
print(list(map(list,dict(d).items())))   

输出:

[['Abradolf Lincler', 32], ['Scary Terry', 20], ['Summer Smith', 20]]

答案 2 :(得分:1)

我会使用Counter

>>> from collections import Counter
>>> arrays = [['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Summer Smith', 4], ['Summer Smith', 4], ['Summer Smith', 4], ['Abradolf Lincler', 4], ['Summer Smith', 4], ['Summer Smith', 4]]
>>> result = Counter()
>>> for k, v in arrays:
...     result[k] += v
...
>>> result
Counter({'Abradolf Lincler': 32, 'Scary Terry': 20, 'Summer Smith': 20})

答案 3 :(得分:0)

您可以使用import java.util.*; public class HelloWorld { public static void main(String[] args) { List number = new ArrayList(); number.add(100); number.add(1000); number.add(10000); int[] changedNumber = number.stream(); int A = chagedNumber.get[2]; int B = chagedNumber.get[0]; if (A > B) { System.out.println(number.get(2) + " is bigger than " + number.get(0)); } else { System.out.println(number.get(2) + " is smaller than" + number.get(0)); } } }

itertools.groupby

相当于循环:

from itertools import groupby
l = [['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Scary Terry', 4], ['Scary Terry', 4], ['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Abradolf Lincler', 4], ['Summer Smith', 4], ['Summer Smith', 4], ['Summer Smith', 4], ['Abradolf Lincler', 4], ['Summer Smith', 4], ['Summer Smith', 4]]

res = [[name, sum(s[1] for s in score)] for name, score in groupby(sorted(l, key=lambda x: x[0]), key=lambda x: x[0])]

并返回:

res = []
for name, score in groupby(sorted(l, key=lambda x: x[0]), key=lambda x: x[0]):
    res.append([name, sum(s[1] for s in score)])