from collections import namedtuple
Name = namedtuple('Name', 'name age sex')
a = Name('王大锤', 18, '女')
b = Name('丑小鸭', 20, '鸭')
print(a.name, a.age, a.sex) # 结果: 王大锤 18 女
print(b.name, b.age, b.sex) # 结果: 丑小鸭 20 鸭
name = '王大锤'
sex = '男'
age = 18
print(f'{name}居然只有{age},{sex}?') # 结果王大锤居然只有18,男?
- 如何给字典排序,按键?按值?(貌似不新,不过挺有用)
my_dict = {'e': 4, 'b': 2, 'c': 1, 'd': 3}
a = sorted(my_dict)
b = sorted(my_dict.items())
c = sorted(my_dict.items(), key=lambda x: x[1])
print(a)
print(b)
print(c)
'''
结果:
['b', 'c', 'd', 'e']
[('b', 2), ('c', 1), ('d', 3), ('e', 4)]
[('c', 1), ('b', 2), ('d', 3), ('e', 4)]
'''
x, y, z = 0, 1, 0
if x == 1 or y == 1 or z == 1:
print('yes')
if x or y or z:
print('yes')
if 1 in (x, y, z):
print('yes')
if not all([x, y, z]):
print('yes')
if any((x, y, z)):
print('yes')
if all((x, y, z)):
print('yes')
else:
print('no')
if x and y and z:
print('yes')
else:
print('no')
'''
结果:
yes
yes
yes
yes
yes
no
no
'''
网友评论