问题描述
我有一个列表ll的整数。我想知道ll中的每个数字是否在0..maxnum范围内是
我可以写它但我想知道是否''更好的方法吗?
TIA
-
时间过得很快。果蝇像香蕉一样苍蝇。奇怪的事情有.0。
发生但没有比这更奇怪。你的驾驶执照是否说器官..0
捐助者?黑洞是上帝除以零的地方。听我说!我们全都是个人b $ b!如果这不是一个假设的问题怎么办?
steveo在syslang.net
I have a list ll of intergers. I want to see if each number in ll is
within the range of 0..maxnum
I can write it but I was wondering if there''s a better way to do it?
TIA
--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver''s license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren''t a hypothetical question?
steveo at syslang.net
推荐答案
我可能会做一些使用max和min函数之类的东西这个:
seq = [...]#your list
if min(seq)> = 0&& max(seq)< = MAXNUM:
做的东西
I would probably do something using max and min functions like this:
seq = [...] # your list
if min(seq) >= 0 && max(seq) <= MAXNUM:
do stuff
一个可能很好的解决方案(Python V. 2.5):
data = [1,20,22,11,400,7]
maxnum = 100
打印all(0
maxnum = 1000
print all(0< = x< = maxnum for x in数据)
再见,
熊宝宝
One possible nice solution (Python V.2.5):
data = [1, 20, 22, 11, 400, 7]
maxnum = 100
print all(0 <= x <= maxnum for x in data)
maxnum = 1000
print all(0 <= x <= maxnum for x in data)
Bye,
bearophile
如何 :
maxnum = 100
inlist = range(90,120)
for i in [i for i in inlist if i> = 0且i< = maxnum]:
#用i做点什么,它在有效范围内
打印我
问候,
Jordan
How about:
maxnum = 100
inlist = range(90, 120)
for i in [i for i in inlist if i >= 0 and i <= maxnum]:
# do something with i, it''s in the valid range
print i
Regards,
Jordan
这篇关于列表中的每个数字都在一个范围内吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!