#๐Ÿ”’ How to compare numbers in two different list

100 messages ยท Page 1 of 1 (latest)

pale badger
#

Need some help

marble summitBOT
#

@pale badger

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

pale badger
#
 
def points(games):

    total_x = 0
    total_y = 0

    list_x = []
    list_y = []

    for i in games:
        list_x.append(int(i[0]))
        list_y.append(int(i[2]))

    print(list_x)
    print(list_y)




points(['1:0','2:0','3:0','4:0','2:1','3:1','4:1','3:2','4:2','4:3'])
#

So currently my output looks smth like this

#

What I wanna do is compare each values in the list

#

How do you do that?

round coral
pale badger
#

x > y

trim zenith
#

You could zip them and then compare

#

!zip

marble summitBOT
#
The `zip` function

The zip function allows you to iterate through multiple iterables simultaneously. It joins the iterables together, almost like a zipper, so that each new element is a tuple with one element from each iterable.

letters = 'abc'
numbers = [1, 2, 3]
# list(zip(letters, numbers)) --> [('a', 1), ('b', 2), ('c', 3)]
for letter, number in zip(letters, numbers):
    print(letter, number)

The zip() iterator is exhausted after the length of the shortest iterable is exceeded. If you would like to retain the other values, consider using itertools.zip_longest.

For more information on zip, please refer to the official documentation.

pale badger
#

in terms of checking if a number is bigger than the other

round coral
round coral
#

check the input data

pale badger
#

but I need that for each iterable

round coral
#

it's a list[str] where each is x:y

trim zenith
#

yeah I guess you could just use the games object and do the comparison directly

round coral
pale badger
#

Sorry if I phrased that wrongly, I mean something like

#

[1,2,3,4]
[0,0,0,0]

I wanna check if the 1st element in list 1 is greater than the 1st element in list 2, and then so on so fort

check if 2nd element in list 1 is greater than the 2nd element in list 2....

#

ssomething like that

#

Comparing each index values? i don't know how I should phrase this

trim zenith
pale badger
#

i also thought abt using a while loop but do I need like an in range?

#

i wasn't sure how it'd go

#

idk why i didnt even try it tho

trim zenith
#

you don't need another loop

#

Just use the same for loop that you already have

pale badger
#

how do you do that?

#

like for each iterable

trim zenith
pale badger
#

a string element

trim zenith
#

OK give me one example of a string element value from games that represents i

pale badger
#
 ['1:0']`
trim zenith
#

So if i[0], what does that return?

pale badger
#

1

trim zenith
#

And i[2]?

pale badger
#

0

trim zenith
#

And if you do i[0] > i[2]?

pale badger
#

You can compare number strings?

trim zenith
#

!e

i = '1:0'
print(i[0])
print(i[2])
print(f'i[0] > i[2] = {i[0] > i[2]}')
marble summitBOT
pale badger
#

im so confused, why is that possible?

#

isn't it a string in this instance?

trim zenith
#

That's how Python compares them

#

!e

print(ord('1'))
print(ord('0'))
marble summitBOT
pale badger
#

oh wow

#

that's very interesting

#

I did not know that

trim zenith
#

If you want, you can still convert them to integers

#

And then compare

#

Totally up to you

pale badger
#

ah like int(i[0]?

trim zenith
#

sure

pale badger
#

I see

#

Thanks a lot for that explanation, I appreciate it

trim zenith
#

np

pale badger
#

How did you do the !e thing btw?

trim zenith
#

That's short for eval

#

!eval

marble summitBOT
#
Missing required argument

code

pale badger
#

!eval


print('true')

marble summitBOT
pale badger
#

ahh

trim zenith
pale badger
#

I see

fallow ferry
#

are the values in the list always single digits?

pale badger
pale badger
trim zenith
pale badger
#

no I don't think so

#

the max is 4

fallow ferry
#

then just getting i[0] and i[2] is fine yeah. if they were multiple digits, then you could use .split(":")

pale badger
#

how'd you do that? i thought that method is only for strings?

#

or we convert first?

fallow ferry
pale badger
#

ohh

#

smth like i[0].split(":")

fallow ferry
#

no, just i.split(":"). i[0] would be getting the first character of the string

pale badger
#

ohh yeah your right

#

thanks for the insight

pale badger
#

like as in > 10 values?

#

or smth like 10, 10

fallow ferry
#

its not about greater than 10, its just that you dont want to be parsing strings each time to get the values you actually want to work with. you can use types that better fit the data: here each element is two numbers, so use a tuple with two numbers

pale badger
#

ahh I see

#

Thanks for that insight

#

.close

trim zenith
pale badger
#

oh i see

#

thanks

#

!close

marble summitBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.