Python Forum
Iterating over two lists in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterating over two lists in python
#1
I have two lists:

node=['v', 'r', 'r', 'v', 'r', 'r', 'r', 'r', 'v', 'v', 'r', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v']

percent=[5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 55.0, 60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0, 100.0]

I want to assign points to each node based on whether it is "v" or "r" and the relative position on a percent scale based on the "percent" list as follows;

for "v",

position 0-25=0,
position 26-50=2,
position 51-75=5,
position 76-100=10

for "r";

position 0-25=10,
position 26-50=5,
position 51-75=2,
position 76-100=1

I would like to end up with a new list that looks like this:

points=[0,10,0,10,5,5,5,2,2,2,5,5,5,5,10,10,10,10,10]

My code to attempt this is as follows:

node=['v', 'r', 'r', 'v', 'r', 'r', 'r', 'r', 'v', 'v', 'r', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v']
percent=[5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 55.0, 60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0, 100.0]
points=[]
for nod in node:
    for val in percent:
        if nod == "v" and val <= 25:
            point=0
            points.append(point)
        elif nod == "v" and 25 < val < 51:
            point=2
            points.append(point)
        elif nod == "v" and 50 < val < 71:
            point=5
            points.append(point)
        elif nod == "v" and 70 < val < 101:
            point=10
            points.append(point)
        elif nod == "r" and val <= 25:
            point=10
            points.append(point)
        elif nod == "r" and 25 < val < 51:
            point=5
            points.append(point)
        elif nod == "r" and 50 < val < 71:
            point=2
            points.append(point)
        elif nod == "r" and 70 < val < 101:
            point=1
            points.append(point)
print(points)
It seems there is something I am not getting right. Is there a better way to do this?
Reply


Messages In This Thread
Iterating over two lists in python - by dgm - Jul-24-2018, 07:29 PM
RE: Iterating over two lists in python - by buran - Jul-24-2018, 07:44 PM
RE: Iterating over two lists in python - by dgm - Jul-24-2018, 08:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Star --- Python lists and Linked Lists --- sabe 3 2,759 Nov-22-2020, 05:51 PM
Last Post: DeaD_EyE
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,497 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Iterating through lists with different letter cases fatherted99 2 1,936 Jun-07-2020, 01:22 PM
Last Post: deanhystad
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,376 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Best form of iterating over a lsit of lists tonymcgregor 15 7,855 Oct-15-2017, 11:19 PM
Last Post: tonymcgregor
  iterating over N lists in parallel Skaperen 6 5,259 Mar-24-2017, 06:51 AM
Last Post: buran

Forum Jump:

User Panel Messages

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