Python Forum
Help With Parallel Arrays - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Help With Parallel Arrays (/thread-9865.html)

Pages: 1 2


Help With Parallel Arrays - toxicxarrow - May-02-2018

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)



RE: Help With Parallel Arrays - buran - May-02-2018

(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?


RE: Help With Parallel Arrays - toxicxarrow - May-02-2018

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?


RE: Help With Parallel Arrays - ThiefOfTime - May-02-2018

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]



RE: Help With Parallel Arrays - killerrex - May-02-2018

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)


RE: Help With Parallel Arrays - toxicxarrow - May-02-2018

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


RE: Help With Parallel Arrays - buran - May-02-2018

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



RE: Help With Parallel Arrays - toxicxarrow - May-02-2018

That's pretty interesting, buran. Thank you for the suggestion.


RE: Help With Parallel Arrays - snippsat - May-02-2018

(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'



RE: Help With Parallel Arrays - toxicxarrow - May-03-2018

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)