Comparing Two Lists Using the Greater Than or Less Than Operator

Comparing two lists using the greater than or less than operator

From Comparing Sequences and Other Types in the Python tutorial:

The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.

See also the Wikipedia article about lexicographical order.

Comparing two list with greater than or less than

If you mean that list3 is the collection of values from list1 where the corresponding value in list2 is smaller, then:

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

Greater than and less than in a list in python

From the Python documentation:

The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.

How does list comparison work?

You can use a list comprehension for comparing two lists element wise then use all function to check all of comparison are True:

a = [8,9,9,11]
b = [8,7,20,10]
all(a[i]>=b[i] for i in range(len(a))) # False

Using greater than operator to Compare corresponding integers of a 2 lists

You can use zip() function.

new_list = [i for i, j in zip(currnt_scr, max_scr_in_half) if i < j]

How does Python compare two lists of unequal length?

The standard comparisons (<, <=, >, >=, ==, !=, in , not in ) work exactly the same among lists, tuples and strings.

The lists are compared element by element.

If they are of variable length, it happens till the last element of the shorter list

If they are same from start to the length of the smaller one, the length is compared i.e. shorter is smaller

Comparing two lists using the greater than or less than operator

From Comparing Sequences and Other Types in the Python tutorial:

The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.

See also the Wikipedia article about lexicographical order.

Compare two lists of float values element wise using min/max function in python

You're not actually pairing element wise the list elements... min(a, b) in this case is literally saying which of the entire list of a and b is the lowest and it does that by comparing each element of each list individually. Since 1915.2322937926342 < 2563.0 - Python doesn't need to look further forward to decide that the entire list a is "less" than list b. (In tie breaks, eg: [0, 0, 1] and [0, 0, -1] - as soon it sees that last -1 - it can determine that -1 is less than 1 and thus can conclude which list is not equal to the other and in which direction). Note that something like [0, 0, 0] < [0, 0, 0] will return False.

You need to explicitly pair items across the lists using zip and then take the min of those, eg:

result = [min(els) for els in zip(a, b)]

That'll give you: [1915.2322937926342, -4320.21030101545]

See: Comparing two lists using the greater than or less than operator

How 'greater than or equal' comparison between sets with characters work

The data within a set are unordered. That is a property of hash tables being referred to as "Unordered associative array" since the order of elements stored differs based on the hashing technique used e.g. "b" might be placed on a memory after "a" in one run and could be reversed the next run, unlike arrays/lists/tuples which would be stored/accessed in the same order as how it was defined, regardless of machine.

To visualize this, create a script:

print({"a", "b", "c"})

Then try running it 3 times:

$ python3 script.py 
{'c', 'a', 'b'}
$ python3 script.py
{'b', 'a', 'c'}
$ python3 script.py
{'a', 'b', 'c'}

As you can see, relying on the lexical order of the set to compare per element would be very unreliable since given the exact same input, the order will differ every run.

Lastly, you might want to look at the documentation of set. As you can see, it explicitly defines how the comparison operators would behave e.g. <= means issubset. Thus the main reason why the comparison of sets are different is because of such overridden methods.

issubset(other)

set <= other

Test whether every element in the set is in other.

  • It doesn't indicate anything about lexical order. All it tells is that it will check if the elements in one is in another, without regard to order.
  • This is just one comparison method. Refer to the docs for the complete list.


Related Topics



Leave a reply



Submit