赋值:
a, b, c = ‘xixi‘, ‘haha‘, ‘hehe‘
连接字典:
>>> s = {1:‘a‘, 2:‘b‘, 3:‘c‘}
>>> s.keys()
[1, 2, 3]
>>> s.values()
[‘a‘, ‘b‘, ‘c‘]
>>> s.items()
[(1, ‘a‘), (2, ‘b‘), (3, ‘c‘)]
>>> [(k, v) for (k, v) in s.items()]
[(1, ‘a‘), (2, ‘b‘), (3, ‘c‘)]
>>> [(k:v) for (k, v) in s.items()]
File "<stdin>", line 1
[(k:v) for (k, v) in s.items()]
^
SyntaxError: invalid syntax
>>> [‘%d:%s‘%(k, v) for (k, v) in s.items()]
[‘1:a‘, ‘2:b‘, ‘3:c‘]
>>> ‘,‘.join([(‘%d:%s‘)%(k, v) for (k, v) in s.items()])
‘1:a,2:b,3:c‘
使用 if 0<i<10:, 比if i>0 and i<10: 性能要好
python 的一些小技巧
时间: 2024-10-12 12:17:16