[英]how to extract elements from a list in python given filter criterion
I have this list: 我有这个清单:
data = [[0.0322249406353, 1.00005691884],
[0.0322267803497, 0.999999986608],
[0.0322286200641, 0.499997756763],
[0.0322304597785, 0.333330346815],
[0.0322322994929, 0.249996641841],
[0.0322341392073, 0.199996418857],
[0.0322359789217, 0.166662936867],
[0.0322378186361, 0.142853306874],
[0.0322396583505, 0.12499608438],
[0.032241498065, 0.111107133551],
[0.0322433377794, 0.0999959728877],
[0.0322451774938, 0.0909050232541],
[0.0322470172082, 0.0833292318929],
[0.0322488569226, 0.0769189468948],
[0.032250696637, 0.0714244168966],
[0.032350696637, 0.],
[0.032450696637, -0.04]]
Because I'll use it later and I don't want to use a list with many information clustered in a particular range and scarce information in the rest of the data I'd like to filter my original list so that I end up with a list where the distance bewteen consecutive values of data[i][1]
(second column) is larger than a given value, say 0.05
, if they are within [0,1]. 因为我稍后会使用它,所以我不想使用包含特定范围内聚集的许多信息的列表,而我不想使用剩余数据中的稀缺信息,所以我想过滤原始列表,因此最终得到一个列出
data[i][1]
(第二列)的连续两个值之间的距离大于给定值(例如0.05
(如果它们在[0,1]内)的列表。 data is a list where the second column values continuously decrease, so data[i][1] < data[i-1][1]
. data是第二列值连续减小的列表,因此
data[i][1] < data[i-1][1]
。 So the list that I want is something like: 所以我想要的列表是这样的:
data2 = [[0.0322249406353, 1.00005691884],
[0.0322267803497, 0.999999986608],
[0.0322286200641, 0.499997756763],
[0.0322304597785, 0.333330346815],
[0.0322322994929, 0.249996641841],
[0.0322378186361, 0.142853306874],
[0.032350696637, 0.],
[0.032450696637, -0.04]]
Any ideas how this can be done? 任何想法如何做到这一点? Thanks
谢谢
EDIT (first attempt): 编辑(首次尝试):
data2=[]
for i in xrange(0,len(data)):
if 0>data[i][1] or data[i][1]>1:
data2.append([data[i][0],data[i][1]])
for j in xrange(0,len(data)):
if j>i and 0<data[i][1]<1 and 0<data[j][1]<1:
if data[i][1] - data[j][1] > 0.05:
data2.append([data[i][0],data[i][1]])
i = j
break
This works partially, because I get an incomplete and incorrect new list: 这部分起作用,因为我得到的清单不完整且不正确:
data2=[[0.0322267803497, 0.999999986608], [0.0322286200641, 0.499997756763], [0.0322304597785, 0.333330346815], [0.0322322994929, 0.249996641841], [0.0322341392073, 0.199996418857], [0.0322359789217, 0.166662936867], [0.0322378186361, 0.142853306874], [0.0322396583505, 0.12499608438], [0.032450696637, -0.04]]
Here are a few tips: 这里有一些提示:
EDIT: 编辑:
Using a for loop is the correct strategy, albeit nesting is not really necessary here. 使用for循环是正确的策略,尽管这里实际上并不需要嵌套。 You can simply add any value to the the new list if it is greater
1
or smaller 0
. 您可以简单地将任何值添加到新列表中,如果它大于
1
或小于0
。 For every other element you simply have to check against the newly generated list if the difference between the current last element in the result list and the current element to check is within or without of the boundary and then add it or ignore it. 对于其他每个元素,您只需要对照新生成的列表检查结果列表中当前的最后一个元素与要检查的当前元素之间的差异是否在边界之内或之外,然后添加或忽略它。
EDIT 2: 编辑2:
Here is a possible solution: 这是一个可能的解决方案:
data2=[]
limit=0.1
j = 0
# get all values > 1 in the result list
while data[j][1] > 1:
data2.append(data[j])
j = j + 1
# the next one too
data2.append(data[j])
for i in xrange(0,len(data)):
# compare current to last in results and see if it is smaller than the limit
if abs(data[i][1] - data2[len(data2)-1][1]) > limit and data[i][1] > 0:
data2.append(data[i])
j = i
# in the end add all the elements < 0
for i in xrange(0,len(data)):
if data[i][1] < 0:
data2.append(data[i])
You should make generators for filter your list. 您应该生成用于过滤列表的生成器。 Below, an example of usage for get only lists which have a pair number as first:
下面是一个使用获取示例的示例,该示例仅获取以对号开头的列表:
l = [ [0,1], [1,2], [2,3], [3,4] ]
def get_first_divisible_by_2(l):
for i,j in l:
if not i % 2:
yield i,j
[ c for c in get_first_divisible_by_2(l) ]
[(0, 1), (2, 4)]
or a generator comprehension: 或生成器理解:
list( ( (i,j) for i,j in l if not i % 2 ) )
[(0, 1), (2, 4)]
Simply adapt it with your filter and maybe with coroutines , you'll be able to handle easily previous result. 只需将其与您的过滤器以及协程配合使用,便可以轻松处理先前的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.