简体   繁体   English

如何从给定过滤条件的python中的列表中提取元素

[英]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: 这里有一些提示:

  • Apparently, you want to go through a data structure one by one, checking a predicate for every element, so what kind of loop could help you with this? 显然,要通过一个经过一个数据结构中的一个,检查谓词的每一个元素,那么什么样的循环可以帮助您吗?
  • For each element you want to compare the next one to the previous one and check whether the difference between two elements is greater than a specific value, so how could your predicate look like? 对于每个元素,您都想将下一个与上一个进行比较,并检查两个元素之间的差异是否大于特定值,那么您的谓词看起来如何?
  • And last but not least, what do you want to do with an item if your predicate returns true? 最后但并非最不重要的一点是,如果您的谓词返回true,您想对一个项目做什么?

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.

相关问题 如何从 Python 中的复杂列表中提取元素 - How to extract elements from a complex list in Python 如何有效地从numpy数组中提取由其索引给出的元素列表? - How to extract a list of elements given by their indices from a numpy array efficiently? 在python中按成对标准对列表中的元素进行分组 - group elements in list by pairwise criterion in python Python-如何从给定列表中创建N个元素的列表 - Python-How to create a list of N elements from a given list 如何从python列表中的元素中提取浮点数? - How do you extract the floats from the elements in a python list? (Python)如何从拼接列表中提取括号之间的元素 - (Python) How to extract elements between parentheses from a spliced list 如何从 Python 中的 XML 中的列表中提取子元素 - How to extract sub-elements from a list within an XML in Python 如何将数字从字符串提取为列表中的单个元素? - How can extract numbers from a string to a list as individual elements in python? 如何从一个ReportLab表的python中的元组列表中提取元素? - How to extract elements from a list of tuples in python for a ReportLab table? 如何使用Python和xarray从变量满足netCDF数据集条件的位置提取坐标? - How to extract coordinates from where the variable meets a criterion for a netCDF dataset with Python and xarray?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM