Python Forum
how to get vairable to sort with list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get vairable to sort with list?
#1
analyst = list([304, 213, 175, 379, 572, 801, 630])

print(list(analyst))
print(sorted(analyst))

analyst[0] = "Monday "
analyst[1] = "Tuesday "
analyst[2] = "Wednesday "
analyst[3] = "Thursday "
analyst[4] = "Friday "
analyst[5] = "Saturday "
analyst[6] = "Sunday "

print(list(analyst))
print(sorted(analyst))

# i am trying to get two types of information, first the list from monday - sunday with numbers from each day
# the secound print i am trying to get the list with numbers sorted from lowest to highest and the variable with string sorted from lowest to highest
# i get the variable to go from a - z in order but i want them to follow the same order as the list
Reply
#2
Note that what you do is to replace the values in the original list, i.e. you have lost the numbers.

values = list([304, 213, 175, 379, 572, 801, 630])
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

# data as list of tuples
data = list(zip(values, weekdays))
print(sorted(data))

print('\n\n')
data = list(zip(weekdays, values))
print(sorted(data, key=lambda x: x[1]))

print('\n\n')
from operator import itemgetter
print(sorted(data, key=itemgetter(1)))
print('\n\n')

# data as dict
data = dict(zip(weekdays, values))
print(sorted(data.items(), key=lambda x: x[1]))
# or alternatives
print(dict(sorted(data.items(), key=lambda x: x[1]))) # as of 3.7 order of insertion of dict elements is preserved
print(sorted(data.items(), key=itemgetter(1)))
print(dict(sorted(data.items(), key=itemgetter(1))))
print('\n\n')

# now going fancy with namedtuple from collections
from collections import namedtuple

Record =  namedtuple('Record', ['day', 'value'])
data = [Record(day, value) for day, value in zip(weekdays, values)]
print(sorted(data, key=lambda x: (x.value, x.day)))
Output:
[(175, 'Wednesday'), (213, 'Tuesday'), (304, 'Monday'), (379, 'Thursday'), (572, 'Friday'), (630, 'Sunday'), (801, 'Saturday')] [('Wednesday', 175), ('Tuesday', 213), ('Monday', 304), ('Thursday', 379), ('Fri day', 572), ('Sunday', 630), ('Saturday', 801)] [('Wednesday', 175), ('Tuesday', 213), ('Monday', 304), ('Thursday', 379), ('Fri day', 572), ('Sunday', 630), ('Saturday', 801)] [('Wednesday', 175), ('Tuesday', 213), ('Monday', 304), ('Thursday', 379), ('Fri day', 572), ('Sunday', 630), ('Saturday', 801)] {'Wednesday': 175, 'Tuesday': 213, 'Monday': 304, 'Thursday': 379, 'Friday': 572 , 'Sunday': 630, 'Saturday': 801} [('Wednesday', 175), ('Tuesday', 213), ('Monday', 304), ('Thursday', 379), ('Fri day', 572), ('Sunday', 630), ('Saturday', 801)] {'Wednesday': 175, 'Tuesday': 213, 'Monday': 304, 'Thursday': 379, 'Friday': 572 , 'Sunday': 630, 'Saturday': 801} [Record(day='Wednesday', value=175), Record(day='Tuesday', value=213), Record(da y='Monday', value=304), Record(day='Thursday', value=379), Record(day='Friday', value=572), Record(day='Sunday', value=630), Record(day='Saturday', value=801)] >>>
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  list.sort() returning None SmallCoder14 8 405 Mar-19-2024, 09:49 PM
Last Post: SmallCoder14
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,277 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  list sort() function bring backs None CompleteNewb 6 4,000 Mar-26-2022, 03:34 AM
Last Post: Larz60+
  [solved] Sort list paul18fr 5 2,801 Aug-18-2021, 06:34 AM
Last Post: naughtyCat
  Sort List of Lists by Column Nju 1 10,049 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  How to sort os.walk list? Denial 6 11,290 Oct-10-2020, 05:28 AM
Last Post: Denial
  Converting to a list and sort tantony 6 3,132 Oct-07-2019, 03:30 PM
Last Post: perfringo
  List sort - alphanumeric? papsphilip 2 2,403 Oct-05-2019, 09:27 PM
Last Post: papsphilip
  syntax error on list.sort() jjordan33 3 3,146 Jul-10-2019, 04:57 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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