我在python中合并了两个列表和一个公共元素。
例如我有以下列表列表:
[[1.0, 'Software Developer', 1256],
[1.0, 'Software Developer', 1329],
[1.0, 'Software Developer', 1469],
[1.0, 'Software Developer', 2086],
[0.9230769230769231, 'United States', 1256],
[0.9230769230769231, 'United States', 1329],
[0.9230769230769231, 'United States', 1469],
[0.9230769230769231, 'United States', 2086]]
和我的欲望输出如下:
{'ID': 1469,
'Location': 'United States',
'Location_score': 0.9230769230769231,
'title': 'Software Developer',
'title_score': 1.0}
{'ID': 1256,
'Location': 'United States',
'Location_score': 0.9230769230769231,
'title': 'Software Developer',
'title_score': 1.0}
这是我想为所有人做的示例输出。
任何人都可以告诉我如何合并所有列表中最后的所有常见元素。然后在Dictionary中转换List。
我尝试使用'Union'
功能。并且有些谷歌,但我没有得到正确答案。
任何人都可以帮助我。
先谢谢
答案 0 :(得分:1)
使用collections.defaultdict
:
from collections import defaultdict
lst = [[1.0, 'Software Developer', 1256],
[1.0, 'Software Developer', 1329],
[1.0, 'Software Developer', 1469],
[1.0, 'Software Developer', 2086],
[0.9230769230769231, 'United States', 1256],
[0.9230769230769231, 'United States', 1329],
[0.9230769230769231, 'United States', 1469],
[0.9230769230769231, 'United States', 2086]]
# initialize defaultdict of dicts
d = defaultdict(dict)
# calculate half length of list
n = int(len(lst)/2)
# iterate first part of list
for title_score, title, ID in lst[:n]:
d[ID]['title_score'] = title_score
d[ID]['title'] = title
# iterate second part of list
for Location_score, Location, ID in lst[n: len(lst)]:
d[ID]['Location_score'] = Location_score
d[ID]['Location'] = Location
<强>结果强>
defaultdict(dict,
{1256: {'Location': 'United States', 'Location_score': 0.9230769230769231,
'title': 'Software Developer', 'title_score': 1.0},
1329: {'Location': 'United States', 'Location_score': 0.9230769230769231,
'title': 'Software Developer', 'title_score': 1.0},
1469: {'Location': 'United States', 'Location_score': 0.9230769230769231,
'title': 'Software Developer', 'title_score': 1.0},
2086: {'Location': 'United States', 'Location_score': 0.9230769230769231,
'title': 'Software Developer', 'title_score': 1.0}})
如果您需要词典列表,可以使用列表推导:
res = [{**{'ID': k}, **v} for k, v in d.items()]
答案 1 :(得分:1)
这是使用集合的一种方法。
data = [[1.0, 'Software Developer', 1256],
[1.0, 'Software Developer', 1329],
[1.0, 'Software Developer', 1469],
[1.0, 'Software Developer', 2086],
[0.9230769230769231, 'United States', 1256],
[0.9230769230769231, 'United States', 1329],
[0.9230769230769231, 'United States', 1469],
[0.9230769230769231, 'United States', 2086]]
from collections import defaultdict
d = defaultdict(list)
for i in data:
d[i[-1]].extend(i)
res = []
for i in d.values():
res.append({"ID": i[-1], 'title_score': i[0], 'title': i[1],'Location_score':i[3], 'Location': i[4]})
print(res)
<强>输出:强>
[{'Location_score': 0.9230769230769231, 'Location': 'United States', 'ID': 1256, 'title_score': 1.0, 'title': 'Software Developer'}, {'Location_score': 0.9230769230769231, 'Location': 'United States', 'ID': 1329, 'title_score': 1.0, 'title': 'Software Developer'}, {'Location_score': 0.9230769230769231, 'Location': 'United States', 'ID': 1469, 'title_score': 1.0, 'title': 'Software Developer'}, {'Location_score': 0.9230769230769231, 'Location': 'United States', 'ID': 2086, 'title_score': 1.0, 'title': 'Software Developer'}]
答案 2 :(得分:0)
使用普通字典,只假设&#34;标题&#34;记录是第一位的:
>>> lol = [[1.0, 'Software Developer', 1256],
... [1.0, 'Software Developer', 1329],
... [1.0, 'Software Developer', 1469],
... [1.0, 'Software Developer', 2086],
... [0.9230769230769231, 'United States', 1256],
... [0.9230769230769231, 'United States', 1329],
... [0.9230769230769231, 'United States', 1469],
... [0.9230769230769231, 'United States', 2086]]
>>>
>>> keys = [(gr + '_score', gr, 'ID') for gr in ('title', 'Location')]
>>>
>>> out = {}
>>> for L in lol:
... d = out.setdefault(L[-1], {})
... d.update(zip(keys[bool(d)], L))
...
>>> out # dict of dicts
{1256: {'title_score': 1.0, 'title': 'Software Developer', 'ID': 1256, 'Location_score': 0.9230769230769231, 'Location': 'United States'}, 1329: {'title_score': 1.0, 'title': 'Software Developer', 'ID': 1329, 'Location_score': 0.9230769230769231, 'Location': 'United States'}, 1469: {'title_score': 1.0, 'title': 'Software Developer', 'ID': 1469, 'Location_score': 0.9230769230769231, 'Location': 'United States'}, 2086: {'title_score': 1.0, 'title': 'Software Developer', 'ID': 2086, 'Location_score': 0.9230769230769231, 'Location': 'United States'}}
>>> list(out.values()) # list of dicts
[{'title_score': 1.0, 'title': 'Software Developer', 'ID': 1256, 'Location_score': 0.9230769230769231, 'Location': 'United States'}, {'title_score': 1.0, 'title': 'Software Developer', 'ID': 1329, 'Location_score': 0.9230769230769231, 'Location': 'United States'}, {'title_score': 1.0, 'title': 'Software Developer', 'ID': 1469, 'Location_score': 0.9230769230769231, 'Location': 'United States'}, {'title_score': 1.0, 'title': 'Software Developer', 'ID': 2086, 'Location_score': 0.9230769230769231, 'Location': 'United States'}]
或者---如果字典顺序很重要(Python 3.6+ inofficial,Python 3.7+官方):
>>> out = {}
>>> for l in lol:
... d = out.setdefault(l[-1], {})
... d.update(zip(*map(reversed, (keys[bool(d)], l))))
...
>>> out
{1256: {'ID': 1256, 'title': 'Software Developer', 'title_score': 1.0, 'Location': 'United States', 'Location_score': 0.9230769230769231}, 1329: {'ID': 1329, 'title': 'Software Developer', 'title_score': 1.0, 'Location': 'United States', 'Location_score': 0.9230769230769231}, 1469: {'ID': 1469, 'title': 'Software Developer', 'title_score': 1.0, 'Location': 'United States', 'Location_score': 0.9230769230769231}, 2086: {'ID': 2086, 'title': 'Software Developer', 'title_score': 1.0, 'Location': 'United States', 'Location_score': 0.9230769230769231}}