Python 如何从列表中获取要添加到字典的键和值

Python 如何从列表中获取要添加到字典的键和值,python,Python,因此,我尝试构建一个字典,其中键是列表的第一个元素,其余元素是值,因此它具有以下格式: {str:[str,str,str,int,str]}但是现在我不知道如何从列表中获取第一个元素作为键,其余元素作为值。 以下是一个例子: 这是名单 ['asmith', 'Alice', 'Smith', '31', 'F', 'alice.smith@example.com'] 第一个元素[0]是键,其余元素是值 以下是我到目前为止的情况: def create_dict(my_file): i

因此,我尝试构建一个字典,其中键是列表的第一个元素,其余元素是值,因此它具有以下格式: {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)

你的理解在这里是混乱的:

d = dict((res[0], res[1:6]) for res[0] in res[1:6])
假设
res
是一个列表列表,您可以在循环中执行类似操作:

d = {}
for r in res:
    d[r[0]] = r[1:6]
或使用理解:

d = {r[0]:r[1:6] for r in res}

根据理解,这是我得到的不正确的输出:{'S':'mith','F':'A':'lice','3':'1','A':'lice.}您需要提供输入数据的MCVE。看起来您没有注意我在回答中提到的警告(“假设
res
是一个列表列表”)。如果
res
是一个列表(不是嵌套列表),那么答案是不一样的。否则,如果您的列表只代表一个人,那么将其转换为dict似乎毫无意义/多余/不必要。请在您的问题中显示
信息的一些示例数据。