[英]Read text file of titles into dictionary
I have this text file of destinations and I need to read them into a dictionary and sort them out based on the starting place and destination. 我有目的地的文本文件,我需要将它们读入字典并根据出发地和目的地对其进行分类。
JFK MCO
ORD DEN
ORD HOU
DFW PHX
JFK ATL
ORD DFW
ORD PHX
ATL HOU
DEN PHX
PHX LAX
JFK ORD
DEN LAS
DFW HOU
ORD ATL
LAS LAX
ATL MCO
HOU MCO
LAS PHX
STL PDX
Expected Results: 预期成绩:
{'JKF' : {'MCO', 'ATL','ORD'}, 'ORD' : {'DEN' , 'HOU' , 'DFW' , 'PHX' , 'ATL' ........}}
The main ideas is that the starting place is on the left and the destination is on the right. 主要思想是起点在左侧,目的地在右侧。 Group the destinations to each starting place.
将目的地分组到每个起点。
You can iterate through the lines, split the lines and use dict.setdefault
to store the destinations into a dict of sets with the originating place as the keys: 您可以遍历各行,拆分各行,然后使用
dict.setdefault
将目标存储到以原始位置为键的集合dict中:
d = {}
with open('file.txt') as file:
for line in file:
orig, dest = line.split()
d.setdefault(orig, set()).add(dest)
d
becomes: d
变为:
{'JFK': {'ORD', 'ATL', 'MCO'}, 'ORD': {'DEN', 'HOU', 'ATL', 'DFW', 'PHX'}, 'DFW': {'PHX', 'HOU'}, 'ATL': {'MCO', 'HOU'}, 'DEN': {'PHX', 'LAS'}, 'PHX': {'LAX'}, 'LAS': {'PHX', 'LAX'}, 'HOU': {'MCO'}, 'STL': {'PDX'}}
This is done very easily using very basic for loop and a dictdefaultdict 使用非常基本的for循环和dictdefaultdict可以很容易地完成此操作
from collections import defaultdict
d = defaultdict(set)
with open("filename.txt", "r") as f:
for line in f:
start, place = line.split()
d[start].add(place)
It can also be done using the csv module 也可以使用csv模块来完成
from collections import defaultdict
import csv
d = defaultdict(set)
with open("filename.txt", "r") as f:
r = csv.reader(f, delimiter=' ')
for start, place in r: d[start].add(place)
Of course, this results in a defaultdict
, although you can still use it the same as a dict, if you want d
to be a true dict after use d = dict(d)
. 当然,这导致
defaultdict
,但你仍然可以使用它一样的字典,如果你想d
是使用后的真实字典d = dict(d)
try this 尝试这个
depart_place = ['JFK', 'ORD', 'ORD', 'DFW', 'JFK']
destination = ['MCO', 'DEN', 'HOU', 'PHX', 'ATL']
place_dict = {}
for place, dest in zip(depart_place, destination):
if place in place_dict:
place_dict[place].add(dest)
else:
place_dict[place] = {dest}
So, essentially, we are iterating through tuples of departing place and destination (place, dest)
resulting from zip(depart_place, destination)
. 因此,从本质
zip(depart_place, destination)
我们正在遍历由zip(depart_place, destination)
导致的出发地点和目的地(place, dest)
元组。 Then, we check if the place
is already in the dictionary, we append the destination
to it or else, we create a new key-value
pair for the place
然后,我们检查
place
已经在字典中,我们将追加destination
它,否则,我们创建了一个新的key-value
对的对place
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.