简体   繁体   中英

Comparing two list with greater than > or less than <

I have two lists that I am trying to compare with < or > in python.

One (list1) is a slope and then a horizontal line (picture a obtuse angle of around 130 degrees) the second is a linear function with a negative slope that I calculated from the first list (list2). What I would like to do is compare the two lists list1 and list2 and create a new list3 where list3 = all the points in list1 > list2 . I am having trouble determining how to approach this. I have attempted list comprehension but I get the error

ValueError: The truth value of an array with more than one element is ambiguous.

v = [c for c in f if c > y]
list1= [0.0, 0.36, 0.34, 0.32, 0.32, 0.3, 0.3, 0.28, 0.28, 0.26, 0.26, 0.24, 0.24, 0.22, 0.22, 0.2, 0.2, 0.18, 0.18, 0.16, 0.16, 0.14, 0.14, 0.12, 0.12, 0.1, 0.1, 0.08, 0.08, 0.06, 0.06, 0.04, 0.04, 0.02, 0.02, 0.0,..., 0.0]
list2= [ 0.36  0.35  0.34 ..., -9.62 -9.63 -9.64]

Any help, advice, or guidance would be greatly appreciated. Also please let me know if my question is unclear in anyway.

如果你的意思是list3list1中值的集合,其中list2中的对应值较小,那么:

list3 = [item1 for item1, item2 in zip(list1, list2) if item1 > item2]

You say "list", but from the error message and the outputs I suspect you're working with numpy arrays. In any case, the problem with v = [c for c in f if c > y] is that you're comparing an element c with the array y. You want to compare element to element.

In the case of a list, you can do the following:

>>> aa = [2,3,4,5]
>>> bb = [3,4,2,5]
>>> 
>>> z = [a for a,b in zip(aa,bb) if a > b]
>>> z
[4]

Or if you're really working with numpy arrays, you can make it even easier:

>>> import numpy
>>> aa = numpy.array([2,3,4,5])
>>> bb = numpy.array([3,4,2,5])
>>> aa > bb
array([False, False,  True, False], dtype=bool)
>>> aa[aa > bb]
array([4])
[elmt1 for elmt1, elmt2 in zip(list1, list2) if elmt1 > elmt2]

This essentially directly mirrors the question: select elements from list1 that are larger than the corresponding element in list2.

If one of the lists is longer, its extra elements cannot be compared to elements from the other list, and they are ignored.

假设两个列表大小相等,

list3 = [list1[i] for i in range(len(list1)) if list1[i]>list2[i]]

使用izip_longest http://docs.python.org/library/itertools.html#itertools.izip_longest

list3 = [i for i,j in itertools.izip_longest(list1, list2, 0) if i > j]

Assuming I correctly understand your question to mean a list of the points (x,y) at which list1[i] > list2[i] rather than just a list of values from list1, this

list3 = [(i,val) for i, val in enumerate(list1) if val > list2[i]]

will return that list as points. If you need to know at which index list1 > list2 then enumerate is helpful.

If all you are interested in is the list of indexes at which the two arrays are equal: all x for which f(x) > g(x) in other words, then all you need are the indexes:

list3 = [i for i, val in enumerate(list1) if val > list2[i]]

For those of you who want a lazy iterator approach:

from itertools import izip, ifilter
i = ifilter(lambda (x,y): x > y, izip(list1, list2))
list3 = list(i)

This will quit once either list runs out of values. To iterate over the longest list, use izip_longest :

from itertools import izip_longest, ifilter
i = ifilter(lambda (x,y): x > y, izip_longest(list1, list2, fillvalue=0))
list3 = list(i)

And since this uses iterators, you can replace list3 = list(i) bit with a loop to actually use the values as they get generated, if necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM