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
#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


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,796 Nov-22-2020, 05:51 PM
Last Post: DeaD_EyE
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,558 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Iterating through lists with different letter cases fatherted99 2 1,953 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,463 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Best form of iterating over a lsit of lists tonymcgregor 15 8,038 Oct-15-2017, 11:19 PM
Last Post: tonymcgregor
  iterating over N lists in parallel Skaperen 6 5,308 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