Python Forum
Help With Parallel Arrays
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help With Parallel Arrays
#1
I have a homework assignment, nothing urgent, but any help would be appreciated.
The goal is to gather an amount of rainfall for every month in a year. The program has to use two parallel arrays, one for the months, and one for the the amount of rain. Then it has to sort the rain amount in ascending order. My problem is that I can't really think of how to keep both arrays sorted in the exact same way so that both lists stay parallel. My two main problems are: how can I make the "X" in the prompt display the month that I'm gathering for, and how can I link the monthList to the rainAmount list after sorting the rainAmount list? I'd rather not use functions as I'm not very comfortable using them just yet.
Here is my code so far:
monthList = [
    'January',
    'Febuary',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December'
    ]

#
rainAmount = []
size = 12

#
for i in range(size):
    amount = input('Enter amount of rain for X month: ')
    rainAmount.append(amount)

#
for i in range(0, len(rainAmount) - 1):
    for j in range(0, len(rainAmount) - 1 - i):
        if rainAmount[j] > rainAmount[j+1]:
            rainAmount[j], rainAmount[j+1] = rainAmount[j+1], rainAmount[j]

#
print(rainAmount)
Reply
#2
(May-02-2018, 07:20 AM)toxicxarrow Wrote: The program has to use two parallel arrays, one for the months, and one for the the amount of rain.
is this a requirement as per the assignment or just your interpretation?
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
It is a requirement. The point is to implement a sorting algorithm, not just import a function, and to integrate both parallel arrays in that specific way. Also, is this the correct way to reply to you question? Or is there some "reply to user" button that I'm not seeing?
Reply
#4
Since your month stay the same and are unique you could think of using a dictionary.
A different approach could be to have a third list. Lets consider that monthList are your month and rainAmount is your rain amount. Let us make a new list amount_indices containing [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] which are the indices of your rainAmount. Now you sort your list and therefore configure the indices in your amount_indices, after that you have a mapping from your indices of your amount_indices to the values stored in amount_indices.
A slightly different approach would be if you sort both list parallel while changing the index of an element in rainAmount you change the index of the corresponding month. so just change this little part of your code to:
for i in range(0, len(rainAmount) - 1):
    for j in range(0, len(rainAmount) - 1 - i):
        if rainAmount[j] > rainAmount[j+1]:
            rainAmount[j], rainAmount[j+1] = rainAmount[j+1], rainAmount[j]
            monthList[j], monthList[j+1] = monthList[j+1], monthList[j]
Reply
#5
Hi,

For the first question, how to put the month name in the prompt, just remember that in python you can always iterate the list/strings... by element, you do not need an index:
for month in monthList:
    amount = input(f'Enter amount of rain for {month}: ')
    rainAmount.append(amount)
To maintain both list in sync the easiest trick is to perform exactly the same operations to both lists, so adding
            monthList[j], monthList[j+1] = monthList[j+1], monthList[j]
after the line 30 in your code is enough. But you can see that that the code looks messy, because the idea of keeping this information in 2 lists is not the best. Without doing things more complex I would preferred a single list as [(amount, month), (amount, month)...] that you can sort using sorted() command (remember that tuples compare element by element, so if a=(3, 'May') and b=(1, 'June') will be b > a because first compare the 3 vs 1)
Reply
#6
Hey, that definitely worked, did the trick. Any way to make it more... In line? Maybe print each index pair together?

ThiefOfTime, killerex, Thank you both for that end bit, it totally flew past my head. Very helpful.

killerrex, that first bit blew my mind! I had no idea you could do something like that in Python. Very helpful, thank you. Any suggestions on how to make it more neat, so that the index of each array gets paired up in a column? Something like:

March: 4"
July: 2"
August: 5"
etc...
Reply
#7
another approach is:
1.zip your 2 lists in a list of 2-element tuples
2.sort, using built in sort or implement it your self if required by the assignment
3. unzip it back in 2 lists

months = ['January', 'Febuary', 'March',  'April', 'May', 'June',
          'July', 'August', 'September', 'October', 'November', 'December']
rain = [40, 45, 30, 20, 15, 12, 13, 10, 17, 19, 20, 35]

monthly_rain =  zip(rain, months)
print (monthly_rain)
monthly_rain.sort(reverse=True)
print(monthly_rain)
rain, months = zip(*monthly_rain)
print(months)
print(rain)
Output:
[(40, 'January'), (45, 'Febuary'), (30, 'March'), (20, 'April'), (15, 'May'), (1 2, 'June'), (13, 'July'), (10, 'August'), (17, 'September'), (19, 'October'), (2 0, 'November'), (35, 'December')] [(45, 'Febuary'), (40, 'January'), (35, 'December'), (30, 'March'), (20, 'Novemb er'), (20, 'April'), (19, 'October'), (17, 'September'), (15, 'May'), (13, 'July '), (12, 'June'), (10, 'August')] ('Febuary', 'January', 'December', 'March', 'November', 'April', 'October', 'Sep tember', 'May', 'July', 'June', 'August') (45, 40, 35, 30, 20, 20, 19, 17, 15, 13, 12, 10) >>>
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
#8
That's pretty interesting, buran. Thank you for the suggestion.
Reply
#9
(May-02-2018, 08:15 AM)toxicxarrow Wrote: Any suggestions on how to make it more neat, so that the index of each array gets paired up in a column?
A dictionary is a natural solution for this.
λ ptpython
>>> months = ['January', 'Febuary', 'March',  'April', 'May', 'June',
...           'July', 'August', 'September', 'October', 'November', 'December']
... rain = [40, 45, 30, 20, 15, 12, 13, 10, 17, 19, 20, 35]

>>> record = dict(zip(months, rain))
>>> record
{'January': 40,
 'Febuary': 45,
 'March': 30,
 'April': 20,
 'May': 15,
 'June': 12,
 'July': 13,
 'August': 10,
 'September': 17,
 'October': 19,
 'November': 20,
 'December': 35}

>>> # Then look up is easy
>>> record['March']
30

>>> record.get('May', 'Not in record')
15
>>> record.get('holliday', 'Not in record')
'Not in record'
Reply
#10
That's a handy little trick, snippsat; thank you for the suggestion. When I do that, however, I get a horizontal list rather than a column, as so:

{'January': 1.0, 'May': 1.0, 'September': 1.0, 'Febuary': 2.0, 'April': 2.0, 'June': 2.0, 'August': 2.0, 'October': 2.0, 'December': 2.0, 'March': 3.0, 'July': 3.0, 'November': 3.0}
Is there a way to unpack it like you have in your code? This is the way I structured mine:
neat = dict(zip(monthList, rainAmount))
print(neat)
Reply


Forum Jump:

User Panel Messages

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