所以我是一名初学程序员并正在学习编程:在cousera上制作高质量的代码课程.我有一个文件restaurant_small.txt
restaurant.txt格式是评分的餐厅,
georgieporgie:50
dumpling r us:70
queens cafe:60
Run Code Online (Sandbox Code Playgroud)
我可以逐行阅读
dictionary = {}
our_file = open(file)
#using an iterator to read files
for line in iter(our_file):
dictionary = ??
Run Code Online (Sandbox Code Playgroud)
我想能够建立一个字典{'restaurant':'rating'}
我如何去做这个,一步一步简单的欣赏
类似于列夫的答案,但是建立一个线条生成器,首先从最终分裂,并使用字典理解来一次性构建它...
with open('input') as fin:
lines = (line.rsplit(':', 1) for line in fin)
dictionary = {k:int(v) for k, v in lines}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
80 次 |
| 最近记录: |