因此,我尝试构建一个字典,其中键是列表的第一个元素,其余元素是值,因此它具有以下格式: {str:[str,str,str,int,str]}但是现在我不知道如何从列表中获取第一个元素作为键,其余元素作为值。 举个例子:
这是名单
['asmith', 'Alice', 'Smith', '31', 'F', 'alice.smith@example.com']
第一个元素[0]是键,其余元素是值
到目前为止,我掌握的情况如下:
def create_dict(my_file):
information = my_file.read()
information = information.split()
res = []
for element in information:
res.append(element)
print(res)
print(res[0])
d = dict((res[0], res[1:6]) for res[0] in res[1:6])
print(d)
你的理解搞砸了:
您可以在循环中执行类似的操作,假设
res
是一个列表列表:或使用理解:
相关问题 更多 >
编程相关推荐