Dan*_*rro 2 python coding-style
我经常发现自己编写这样的代码:
tupla = (1, 2, 3, 4)
if (1 in tupla) and (4 in tupla):
...
Run Code Online (Sandbox Code Playgroud)
是否有任何Pythonic方式可以更紧凑地编写这个,而无需输入tupla两次?
if all(x in tupla for x in list_of_x):
Run Code Online (Sandbox Code Playgroud)
要么
if set(list_of_x).issubset(set(tupla)):
Run Code Online (Sandbox Code Playgroud)
第一个不会在第一个x不在时停止tupla.第二个将在任何情况下创建两个集合.
| 归档时间: |
|
| 查看次数: |
95 次 |
| 最近记录: |