我有一个这样的清单:
[[846033, 365, 202], [849432, 276, 140], [821121, 209, 111], [820180, 244, 133], [849401, 971, 572], [848613, 790, 596], [846978, 914, 272]]
小名单的第一个元素是“idnumber”,其他2个元素是我想要比较的数据。
我想要做的是:我想删除两个数据编号中至少有一个低于250的大列表元素。答案应如下所示:
[[849401, 971, 572], [848613, 790, 596], [846978, 914, 272]].
我试图在for循环中创建一个for循环但是我失败了:
def zonderRuis(sigIdSpot=[[846033, 365, 202], [849432, 276, 140], [821121, 209, 111], [820180, 244, 133], [849401, 971, 572], [848613, 790, 596], [846978, 914, 272]]):
ruisvrij=[]
for i in range(len(sigIdSpot)):
for r in i:
if r[2]>=250 and r[3]>=250:
ruisvrij.append(r)
return ruisvrij
答案 0 :(得分:8)
[x for x in data if all(y >= 250 for y in x[1:])]
答案 1 :(得分:0)
In [15]: l = [[846033, 365, 202], [849432, 276, 140], [821121, 209, 111], [820180, 244, 133], [849401, 971, 572], [848613, 790, 596], [846978, 914, 272]]
In [16]: [sub for sub in l if min(sub[1:]) >= 250]
Out[16]: [[849401, 971, 572], [848613, 790, 596], [846978, 914, 272]]
答案 2 :(得分:0)
list1 = [[846033, 365, 202], [849432, 276, 140], [821121, 209, 111], [820180, 244, 133], [849401, 971, 572], [848613, 790, 596], [846978, 914, 272]]
list2 = []
for x in list1:
if x[1] > 250 and x[2] > 250:
lijst1.append(x)
答案 3 :(得分:0)
关于迭代的一点:
for i in range(len(sigIdSpot)):
x = sigIdSpot[i]
print x
# becomes more clear if you do the following:
for x in sigIdSpot:
print x
现在,如果你想调试你的代码,你可以放一些打印来查看你的代码发生了什么:
def zonderRuis(sigIdSpot=[[846033, 365, 202], [849432, 276, 140], [821121, 209, 111], [820180, 244, 133], [849401, 971, 572], [848613, 790, 596], [846978, 914, 272]]):
ruisvrij=[]
for i in range(len(sigIdSpot)):
print 'i is:', i
for r in i:
print 'r is:', r
if r[2]>=250 and r[3]>=250:
ruisvrij.append(r)
print 'r appended to ruisvrij:', ruisvrij
return ruisvrij
请记住,交互式shell是我们最好的朋友。