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
#2
check this thread https://python-forum.io/Thread-Homework-...ing-System
we had nice discussion on similar issue - no need to use huge if/else block

def get_value(node, percent):
    breakpoints = {'v':((25, 0), (50, 2), (75, 5), (100, 10)),
                   'r':((25, 10), (50, 5), (75, 2), (100, 1))}

    if not 0 <= percent <= 100:
        raise ValueError('Ivalid percent value. Expected 0-100, got {}.'.format(percent))

    try:
        for breakpoint, value in breakpoints[node]:
            if percent <= breakpoint:
                return value
    except KeyError:
        raise KeyError('Not a valid node value {}.'.format(node))

nodes=['v', 'r', 'r', 'v', 'r', 'r', 'r', 'r', 'v', 'v', 'r', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v', 'v']
percents=[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]
# percents = range(5, 101, 5)
points = [get_value(node, percent) for node, percent in zip(nodes, percents)]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks buran for the link to the thread. I have learnt quite a bit from it, and thanks also for the code you shared. It helps me learn to write better code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star --- Python lists and Linked Lists --- sabe 3 2,648 Nov-22-2020, 05:51 PM
Last Post: DeaD_EyE
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Iterating through lists with different letter cases fatherted99 2 1,833 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,190 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Best form of iterating over a lsit of lists tonymcgregor 15 7,563 Oct-15-2017, 11:19 PM
Last Post: tonymcgregor
  iterating over N lists in parallel Skaperen 6 5,113 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