Python Forum
Comparing values in separate lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparing values in separate lists
#1
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.

lists:
[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:
            a.extend((i,j))
        elif i<5 and j>=5:
            b.extend((i,j))
        elif i>=5 and j<5:
            c.extend((i,j))
        elif i<5 and j<5:
            d.extend((i,j))
print(a,b,c,d)
output:
[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:
[(12.4,10.9)][][(5.1,2.3)][(2.7,3.7)]
Reply


Messages In This Thread
Comparing values in separate lists - by KaleBosRatjes - May-02-2018, 03:42 PM
RE: Comparing values in separate lists - by woooee - May-02-2018, 04:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 8 331 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  Comparing List values to get indexes Edward_ 7 1,168 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  How to separate the values in an element to add them monty024 4 1,035 Jan-23-2023, 04:28 AM
Last Post: deanhystad
  user input values into list of lists tauros73 3 1,075 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,387 May-17-2022, 07:49 AM
Last Post: Pedroski55
  comparing 2 lists rwahdan 2 1,624 Jul-18-2021, 06:30 PM
Last Post: DeaD_EyE
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,383 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Comparing items from 2 lists of dictionaries illwill 7 2,772 Sep-14-2020, 10:46 PM
Last Post: bowlofred
  Comparing Values/QC Within Two Strings uttadms31 2 1,909 Jul-07-2020, 03:49 PM
Last Post: uttadms31
  Inserting values from multiple lists sqlite azulu 1 2,509 May-24-2020, 08:40 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020