我有下面的词典列表:
dictionary =[{'Flow': 100, 'Location': 'USA', 'Name': 'A1'},
{'Flow': 90, 'Location': 'Europe', 'Name': 'B1'},
{'Flow': 20, 'Location': 'USA', 'Name': 'A1'},
{'Flow': 70, 'Location': 'Europe', 'Name': 'B1'}]
我想创建一个新的词典列表,其中所有Flow
和Location
相同的所有词典的Name
值相加。我想要的输出将是:
new_dictionary =[{'Flow': 120, 'Location': 'USA', 'Name': 'A1'},
{'Flow': 160, 'Location': 'Europe', 'Name': 'B1'},]
我该如何实现?
答案 0 :(得分:14)
这是可能的,但在python中实现并非易事。我可以建议使用熊猫吗?使用import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
public class HashM {
public static void main(String[] args) {
HashMap<String, String> lyrics = new HashMap<>();
// KEY :- Lyrics AND Value is Album Name
lyrics.put("that's me in the corner, that's me in the spotlight", "Losing My Religion - R.E.M.");
lyrics.put("I will try not to breathe I can hold my head still with my hands at my knees",
"Automatic for the People");
lyrics.put("Eu não aturo mais Sou um trator", "É Duda Brack");
randomLyrics(lyrics);
}
public static void randomLyrics(HashMap<String, String> lyrics) {
ArrayList<String> lyricKey = new ArrayList<String>(lyrics.keySet());
if (lyrics.size() > 0) {
Random rand = new Random();
int index = rand.nextInt(lyrics.size());
System.out.println("###::####Your Random Number is :-" + rand.nextInt(lyrics.size()));
System.out.println(lyricKey.get(rand.nextInt(lyrics.size())));
}
}
}
,groupby
和sum
很简单。
to_dict
要安装,请使用import pandas as pd
(pd.DataFrame(dictionary)
.groupby(['Location', 'Name'], as_index=False)
.Flow.sum()
.to_dict('r'))
[{'Flow': 160, 'Location': 'Europe', 'Name': 'B1'},
{'Flow': 120, 'Location': 'USA', 'Name': 'A1'}]
。
否则,您可以使用pip install --user pandas
应用伪通用组操作。
itertools.groupby
答案 1 :(得分:8)
虽然我也尽可能使用熊猫,但这是使用普通python的解决方案:
In [1]: import itertools
In [2]: dictionary =[{'Flow': 100, 'Location': 'USA', 'Name': 'A1'},
...: {'Flow': 90, 'Location': 'Europe', 'Name': 'B1'},
...: {'Flow': 20, 'Location': 'USA', 'Name': 'A1'},
...: {'Flow': 70, 'Location': 'Europe', 'Name': 'B1'}]
...:
In [3]: import operator
In [4]: key = operator.itemgetter('Location', 'Name')
In [5]: [{'Flow': sum(x['Flow'] for x in g),
...: 'Location': k[0],
...: 'Name': k[1]}
...: for k, g in itertools.groupby(sorted(dictionary, key=key), key=key)]
...:
...:
Out[5]:
[{'Flow': 160, 'Location': 'Europe', 'Name': 'B1'},
{'Flow': 120, 'Location': 'USA', 'Name': 'A1'}]
另一种方法是使用defaultdict,这使您的表示形式稍有不同(尽管您可以根据需要将其转换回字典列表):
In [11]: import collections
In [12]: cnt = collections.defaultdict(int)
In [13]: for r in dictionary:
...: cnt[(r['Location'], r['Name'])] += r['Flow']
...:
In [14]: cnt
Out[14]: defaultdict(int, {('Europe', 'B1'): 160, ('USA', 'A1'): 120})
In [15]: [{'Flow': x, 'Location': k[0], 'Name': k[1]} for k, x in cnt.items()]
Out[15]:
[{'Flow': 120, 'Location': 'USA', 'Name': 'A1'},
{'Flow': 160, 'Location': 'Europe', 'Name': 'B1'}]
答案 2 :(得分:6)
不完全是您期望的输出,但是..
使用collections.Counter()
count = Counter()
for i in dictionary:
count[i['Location'], i['Name']] += i['Flow']
print count
会给:
Counter({ ('Europe', 'B1'): 160,
('USA', 'A1'): 120 })
我希望这至少会让您有所了解。