【发布时间】:2019-02-01 01:55:30
【问题描述】:
您好,我有一个清单:
lst = ['Bedroom,24', 'Bedroom,26', 'Kitchen,22', 'Kitchen,23', 'Living Room,23', 'Living Room,24', 'Living Room,25']
现在我想不出任何方法可以将其更改为字典:
lst = {'temp':24,.....}
我尝试替换逗号,但由于它是一个列表,因此没有成功。我使用了 for 循环,但似乎没有用,而且我是初学者,我不知道如何将数字更改为整数。下面的示例列出了:
fname = open("temps1.txt")
lst = []
for line in fname:
line = line.rstrip()
words = line.splitlines()
for word in words:
if word in lst: continue
lst = lst + words
lst.sort()
print(lst)
>>>>>>['temp,24', 'bedroom,26', 'class,23'] #output
最后,是否可以将列表转换为字典?
我的输出应该是:
Average temp:
bedroom: #value
living room:#value
等等
【问题讨论】:
标签: python dictionary