[英]Creating a dictionary of lists from a single list?
我对字典有疑问。 我是一个菜鸟,已经广泛地研究了这个主题,但似乎无法解决这个问题。 我要做的是从文本文件中获取这个庞大的列表(1200万个术语),将其放入字典中,然后将具有某些特征的项目放入字典中的一个列表中,以便在搜索时字典,将显示具有该特征的每个元素。
一些列表元素的示例:
0022 hello https:example.com/blah
0122 john https:example.com/blah
3502 hello https:example.com/blah
现在,根据上面的数据,我希望有一个dict元素,该元素是每次出现单词“ hello”并以“ hello”为键的列表,因此当我搜索“ hello”时,我将返回
0022 hello https:example.com/blah
3502 hello https:example.com/blah
关于如何有效执行此操作的任何提示?
我知道数据库可能是一个更快,更好的解决方案,但是我对DB一无所知,我什至都不是CS学生,我只是选修。 谢谢您的帮助
如建议的那样, defaultdict(list)
非常适合执行此操作:
from collections import defaultdict
data = defaultdict(list)
with open('input.txt') as f_input:
for line in f_input:
key = line.split()[1]
data[key].append(line)
print(''.join(data['hello']))
将显示以下行:
0022 hello https:example.com/blah
3502 hello https:example.com/blah
这是一个pandas
解决方案:
import pandas as pd
lst = ['0022 hello https:example.com/blah',
'0122 john https:example.com/blah',
'3502 hello https:example.com/blah']
df = pd.DataFrame([x.split(' ') for x in lst],
columns=['code', 'name', 'url'])
df['code-url'] = list(zip(df['code'], df['url']))
d = df.groupby('name')['code-url'].apply(list).to_dict()
# {'hello': [('0022', 'https:example.com/blah'),
# ('3502', 'https:example.com/blah')],
# 'john': [('0122', 'https:example.com/blah')]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.