0

我有一个文本文件,其中包含一个接一个地给出的一组餐厅的详细信息。详细信息是特定餐厅的名称、评级、价格和菜肴类型。文本文件的内容如下所示。

George Porgie
87%
$$$
Canadian, Pub Food

Queen St. Cafe
82%
$
Malaysian, Thai

Dumpling R Us
71%
$
Chinese

Mexican Grill
85%
$$
Mexican

Deep Fried Everything
52%
$
Pub Food

我想创建一组字典,如下所示:

Restaurant name to rating:
# dict of {str : int}
name_to_rating = {'George Porgie' : 87,
'Queen St. Cafe' : 82,
'Dumpling R Us' : 71,
'Mexican Grill' : 85,
'Deep Fried Everything' : 52}

Price to list of restaurant names:
# dict of {str : list of str }
price_to_names = {'$'   :  ['Queen St. Cafe', 'Dumpling R Us', 'Deep Fried Everything'],
'$$'  :  ['Mexican Grill'],
'$$$' :  ['George Porgie'], 
'$$$$' : [ ]}

Cuisine to list of restaurant name:
#dic of {str : list of str }
cuisine_to_names = {'Canadian' : ['George Porgie'],
'Pub Food' : ['George Porgie', 'Deep Fried Everything'],
'Malaysian' : ['Queen St. Cafe'],
'Thai' : ['Queen St. Cafe'],
'Chinese' : ['Dumpling R Us'],
'Mexican' : ['Mexican Grill']}

Python 中填充上述字典的最佳方法是什么?

4

2 回答 2

1

初始化一些容器:

name_to_rating = {}
price_to_names = collections.defaultdict(list)
cuisine_to_names = collections.defaultdict(list)

将您的文件读入临时字符串:

with open('/path/to/your/file.txt') as f:
  spam = f.read().strip()

假设结构是一致的(即由双换行符分隔的 4 行块),遍历这些块并填充您的容器:

restraunts = [chunk.split('\n') for chunk in spam.split('\n\n')]
for name, rating, price, cuisines in restraunts:
  name_to_rating[name] = rating
  # etc ..
于 2013-03-26T05:05:44.987 回答
0

对于主读取循环,您可以使用 enumerate 和 modulo 来了解一行上的数据:

for lineNb, line in enumerate(data.splitlines()):
    print lineNb, lineNb%4, line

对于price_to_namescuisine_to_names字典,您可以使用默认字典:

from collections import defaultdict
price_to_names = defaultdict(list)
于 2013-03-26T05:09:15.417 回答