Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Organizing results
#1
Im trying to run the results in this program so each output that has the same beach will go in alphabetical order. If you run it you will see that all the results are out of order. To fix this I just want to put every output with with a beach is order. Any way to go about this

import random

beach = ['Civic','Middle', '2 Chair', 'Main', 'Malibu', 'Nassau 2', 'Nassau 5', 'Reef', 'Anchor', 'E.lido', 'Lido', 'W.Lido', 'Surfing Bay', 'Lido West', 'EAB', 'E.EAB', 'Sea Glades']
lifeguard = ['Rodriguez','Mills', 'L Fitzgerald', 'Laibach', 'Hannon', 'Ruddick', 'Pitzer', 'TFlannigann', 'Vurro', 'Harrington', 'Chase', 'Clarkson', 'Digregorio', 'Nicharkwick', 'Riobo', 'Czartoyski', 'Meringolo', 'Brady', 'Barklow', 'Treadwell', 'Flynn', 'Geodecke', 'Murphy', 'Spinella', 'Joyce', 'TFitzgerald', 'Dreidy', 'EMquade', 'Schroeder', 'Mcveigh', 'Harloff', 'Rtreadwell', 'Cdauria', 'Ochs', 'Mriordan', 'Scibelli', 'Shanley', 'Charkowick', 'Kappel', 'Shutkind', 'Tcrafa', 'BCreigh', 'JCrafa', 'Creagh', 'Fitzpatrick', 'Curry', 'KFlannigan', 'Schlicte', 'Aicher', 'Kinneally', 'CCrafa', 'Fahy', 'Mulloolly', 'Zimmerman', 'Marcote', 'Kluss', 'Wiets', 'Duaria', 'PMquade', 'Campbell', 'Ptucker', 'Henderson', 'Lupia', 'Dunn', 'Rainus', 'lark', 'Peers', 'TLark',  'Martinez', 'Scheinburg', 'Rainus', 'Techera', 'Mcglughlin', 'Carr', 'Farrel', 'Bergin', 'Demeo', 'Trebel', 'Vanella', 'Serrao', 'Brown', 'Paoli', 'Dilsner', 'Swanson', 'JBurns', 'Stapleton', 'Correa', 'Formes', 'Hurst', 'BHarrington', 'Marks', 'MByrne', 'Boccio', 'Neuwieth', 'Babel', 'Hooker', 'Orourke', 'Martinsen', 'Leone', 'Wiessburg', 'Compton', 'Mazur', 'Tucker', 'Kerr', 'Dunster', 'Hutchinson', 'TFahy', 'Giordana', 'Hughes', 'Mahony', 'Gallego', 'CoNewby', 'Vitale', 'Shineburg', 'Longo', 'Michelman', 'Henne']
days_off = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
print ('To add an lifeguards type "lifeguard.append(name of lifeguard)"')
print ('To remove an lifeguard type "lifeguard.pop(name of lifeguard)"')


run = input("Hit enter to run program")
if input:
    for names in lifeguard:

        #randomly picks one beach out from array lifeguard
        random.shuffle(beach)
        win = beach[1]
        print (" %s" % win)

        #randomly picks one lifeguard out from array lifeguard
        random.shuffle(lifeguard)
        win = lifeguard.pop()
        print (" %s" % win)

        #randomly picks two days off from array days_off
        random.shuffle(days_off)
        win = days_off[1]
        print (" %s" % win)

        random.shuffle(days_off)
        win = days_off[1]
        print (" %s" % win)

        print('--------------')
        
        
Reply
#2
there is a built in function sorted that will sort the list alphabetically
>>> beach = ['Civic','Middle', '2 Chair', 'Main', 'Malibu', 'Nassau 2', 'Nassau 5', 'Reef', 'Anchor', 'E.lido', 'Lido', 'W.Lido', 'Surfing Bay', 'Lido West', 'EAB', 'E.EAB', 'Sea Glades']
>>> sorted(beach)
['2 Chair', 'Anchor', 'Civic', 'E.EAB', 'E.lido', 'EAB', 'Lido', 'Lido West', 'Main', 'Malibu', 'Middle', 'Nassau 2', 'Nassau 5', 'Reef', 'Sea Glades', 'Surfing Bay', 'W.Lido']
order is punctuation, numbers, uppercase, lowercase. Uppercase gets sorted first, then lowercase. IF you want the uppercase and lowercase seperated independently then give the argument key=str.lower. Not going to change it with this list as there are no elements that start with lowercase.
>>> sorted(beach,key=str.lower)
Recommended Tutorials:
Reply
#3
To make metulburr's suggestion work, you will need to remove the random.shuffle(beach) on line 15. You don't need that anyway. Also, you only need to randomly shuffle the lifeguard list once.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Organizing several similar classes with overlapping variables 6hearts 7 1,329 May-07-2023, 02:00 PM
Last Post: 6hearts
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,190 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Organizing list of objects by attribute scarney1988 3 2,155 Mar-11-2020, 03:55 PM
Last Post: scarney1988
  How to append one function1 results to function2 results SriRajesh 5 3,072 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  Organizing Data in Dictionary Ranjirock 3 2,580 Aug-27-2019, 02:48 PM
Last Post: Ranjirock
  organizing by distance gonzo620 7 3,821 Oct-16-2018, 01:41 AM
Last Post: stullis
  Organizing tips Demini 1 2,185 Feb-10-2018, 05:32 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

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