# Ask
attributes = ['name', 'dob', 'gender']
values = [['jason', '2000-01-01', 'male'],
['mike', '1999-01-01', 'male'],
['nancy', '2001-02-01', 'female']]
# Answers1
lis = []
for i in values:
print(i)
print(dict(zip(attributes, i)))
print(lis.append(dict(zip(attributes, i))))
# Answers2
[dict(zip(attributes, i)) for i in values]
# Answers3
list(map(lambda x: dict(zip(attributes, x)), values))
# Answers4
lis = []
for i in values:
dic = {}
for n, j in enumerate(i):
dic[attributes[n]] = j
lis.append(dic)
print(lis)
我没有想到用多行条件循环语句实现的方法。先留言,然后看别人怎么做的。
展开