Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Comparing values in separate lists
I'm having problems with my code, i want to compare 2 values from a list. There are 4 groups (a,b,c,d) where the values can end up in. A if both values are above 5, b and c if one of the two is above 5 and d if both are below 5. The problem is that the first value will compare with all the values from list 2. I also tried to get the values in tuples but it didn't work?
I'll post my code below.

[12.4, 2.7, 5.1] [10.9, 2.3, 3.7]

I tried it with using a nestled for loop but it uses all the values from list 1 in 2.
my code:
for i in p1stb_list_new:
    for j in p2stb_list_new:
        if i>=5 and j>=5:
        elif i<5 and j>=5:
        elif i>=5 and j<5:
        elif i<5 and j<5:
[12.4, 10.9, 5.1, 10.9] [2.7, 10.9] [12.4, 2.3, 12.4, 3.7, 5.1, 2.3, 5.1, 3.7] [2.7, 2.3, 2.7, 3.7]

desired output:
I would suggest to use the zip function in combination with a for loop if you want to compare the first elements, then the second elements, etc.
so instead of you two for loops use:
list_1 = [12.4, 2.7, 5.1]
list_2 = [10.9, 2.3, 3.7]
for element_1, element_2 in zip(list_1, list_2):
       if element_1>=5 and element_2>=5:
        elif element_1<5 and element_2>=5:
        elif element_1>=5 and element_2<5:
        elif element_1<5 and element_2<5:
print(a, b, c, d)
If you want to compare items in the same position/offset in each list, then you use the offset to step through. And using i, l, or O as single digit variable names can sometimes look like numbers.
if len(p1stb_list_new) == len(p2stb_list_new):
    for offset in range(len(p1stb_list_new)):
        ## shorter names are easier to key in
        print(offset, p1, p2)

        if p1>=5 and p2>=5:
        elif p1<5 and p1>=5:
            b.extend((p1. p2))
        elif p1>=5 and p2<5:
            c.extend((p1, p2))
        elif p1<5 and p2<5:
Thank you sooo much guys! This helped me so much :)
I'm new to programming so sometimes its just not working out :P

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Inserting values from multiple lists sqlite azulu 1 257 May-24-2020, 08:40 AM
Last Post: ibreeden
  Getting Unique values from LISTS aankrose 2 378 Oct-17-2019, 05:33 PM
Last Post: aankrose
  Why does this list of lists end up with the same values alehak 2 559 Jul-18-2019, 12:34 PM
Last Post: perfringo
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 522 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Comparing values in large txt files StevenVF 2 531 Feb-28-2019, 09:07 AM
Last Post: StevenVF
  Looping through dictionary and comparing values with elements of a separate list. Mr_Keystrokes 5 978 Jun-22-2018, 03:08 PM
Last Post: wavic
  comparing multiple values Skaperen 5 7,140 Dec-06-2017, 03:25 AM
Last Post: wavic
  Create a new list by comparing values in a list and string DBS 2 1,572 Jan-14-2017, 07:59 AM
Last Post: Yoriz

Forum Jump:

Users browsing this thread: 1 Guest(s)