#๐ How to compare numbers in two different list
100 messages ยท Page 1 of 1 (latest)
@pale badger
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.
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?
what do you mean "compare"
x > y
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.
in terms of checking if a number is bigger than the other
then just check i[0] > i[2]
they're already "zipped"
check the input data
but I need that for each iterable
it's a list[str] where each is x:y
yeah I guess you could just use the games object and do the comparison directly
what do you mean?
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
Its already explained by "they typed my script with go". Use the games object and do the comparison directly while in the for loop with i[0] > i[2]
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
What is i in your for i in games: ?
a string element
OK give me one example of a string element value from games that represents i
['1:0']`
So if i[0], what does that return?
1
And i[2]?
0
And if you do i[0] > i[2]?
I actually don't know abt this I haven't tried
You can compare number strings?
!e
i = '1:0'
print(i[0])
print(i[2])
print(f'i[0] > i[2] = {i[0] > i[2]}')
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1
002 | 0
003 | i[0] > i[2] = True
Because of the unicode values behind those string values
That's how Python compares them
!e
print(ord('1'))
print(ord('0'))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 49
002 | 48
If you want, you can still convert them to integers
And then compare
Totally up to you
ah like int(i[0]?
sure
np
How did you do the !e thing btw?
code
!eval
print('true')
:white_check_mark: Your 3.12 eval job has completed with return code 0.
true
ahh
I see
are the values in the list always single digits?
what do you mean?
Thanks a lot
Like can it be '99:10'?
then just getting i[0] and i[2] is fine yeah. if they were multiple digits, then you could use .split(":")
the list elements are strings
(to be fair, this is not what you'd want this data to be stored as in your code, tuples of 2 integers would be better)
no, just i.split(":"). i[0] would be getting the first character of the string
two integers are normally stored in tuples?
like as in > 10 values?
or smth like 10, 10
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
Type !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.