Mar*_*ara 0 python loops for-loop python-3.x
我有一个循环读取数据,但编号不连续.因此,我想跳过具体的值.但我只知道如何跳过一个,而不是一组值.这是我的示例代码:
for n in [x for x in range(2,m) if x!=9]:
if n < 10:
stationsnr = '00'+np.str(n)
elif n < 100:
stationsnr = '0'+np.str(n)
else:
stationsnr = np.str(n)
Run Code Online (Sandbox Code Playgroud)
但是代替"x!= 9"我需要像x!=其中一个值[9,10,12,16,......](编辑:值存储在列表中).有什么建议?
您可以测试该值是否为集合的成员:
[... if x not in {9, 10, 12, 16, }]
Run Code Online (Sandbox Code Playgroud)
设置成员资格测试是O(1)恒定时间(如此之快!).
| 归档时间: |
|
| 查看次数: |
300 次 |
| 最近记录: |