pythonic
if else 判断语句
expression1 if condition else expression2 for item in iterable
for item in iterable:
if condition:
expression1
else:
expression2
无else语句
expression for item in iterable if condition
给定两个列表 x、y,要求返回 x、y 中所有元素对组成的元祖,相 等情况除外。
[(xx, yy) for xx in x for yy in y if xx != yy]
attributes = ['name', 'dob', 'gender']
values = [['jason', '2000-01-01', 'male'],
['mike', '1999-01-01', 'male'],
['nancy', '2001-02-01', 'female']
]
# expected output:
[{'name': 'jason', 'dob': '2000-01-01', 'gender': 'male'},
{'name': 'mike', 'dob': '1999-01-01', 'gender': 'male'},
{'name': 'nancy', 'dob': '2001-02-01', 'gender': 'female'}]
l = []
for value in values:
d = {}
for i in range(3):
d[attributes[i]] = value[i]
l.append(d)
print(l)
[dict(zip(attributes, value)) for value in values]
[{key:value[index] for index,key in enumerate(attributes)}for value in values]
网友评论