说我有这个代码:
someDict = {}
for line in open("example.txt"):
key, val = line.strip().split(",")
someDict[key] = val
Run Code Online (Sandbox Code Playgroud)
example.txt是每行上的两个数据,用逗号分隔,例如:
one,un
two,deux
three,trois
Run Code Online (Sandbox Code Playgroud)
等等
这适用于制作字典,但是我很好奇这是否可以在一行中完成(例如,使用列表/字典理解).这可能,或者更简单/更简单的方法吗?
python dictionary list-comprehension dictionary-comprehension