在星期几的基础上,对Python中的列表进行排序。

huangapple go评论79阅读模式
英文:

Sort List of List in Python based on days of the week

问题

我有一个每日温度列表,如下所示:

weekly_temperature = [['Saturday', 100], ['Wednesday', 95], ['Friday', 80],
['Monday', 95], ['Sunday', 90], ['Tuesday', 100], ['Thursday', 85]]

我该如何根据星期几对此列表进行排序,从星期一到星期日?

我希望排序后的输出如下所示:

weekly_temperature = [['Monday', 95], ['Tuesday', 100], ['Wednesday', 95], 
['Thursday', 85], ['Friday', 80], ['Saturday', 100], ['Sunday', 90]]
英文:

I have a list of daily temperature, shown below:

weekly_temperature = [['Saturday', 100], ['Wednesday', 95], ['Friday', 80],
['Monday', 95], ['Sunday', 90], ['Tuesday', 100], ['Thursday', 85]]

How do I sort this list based on days of the week, starting from Monday to Sunday?

I would like the sorted output to look like below:

weekly_temperature = [['Monday', 95], ['Tuesday', 100], ['Wednesday', 95], 
['Thursday', 85], ['Friday', 80], ['Saturday', 100], ['Sunday', 90]]

答案1

得分: 2

你可以使用time.strptime来解析星期几,以便将其用作排序的键:

import time

weekly_temperature = [['Saturday', 100], ['Wednesday', 95], ['Friday', 80],
['Monday', 95], ['Sunday', 90], ['Tuesday', 100], ['Thursday', 85]]
print(sorted(weekly_temperature, key=lambda t: time.strptime(t[0], '%A')))

这将输出:

[['Monday', 95], ['Tuesday', 100], ['Wednesday', 95], ['Thursday', 85], ['Friday', 80], ['Saturday', 100], ['Sunday', 90]]

请注意,time.strptime取决于您的区域设置,如果要确保安全,可以保存当前的时间区域设置,将其设置为标准的C区域设置,解析您的星期几,然后恢复您的本地设置:

import time
import locale

orig_locale = locale.setlocale(locale.LC_TIME)
locale.setlocale(locale.LC_TIME, 'C')
weekly_temperature = [['Saturday', 100], ['Wednesday', 95], ['Friday', 80],
['Monday', 95], ['Sunday', 90], ['Tuesday', 100], ['Thursday', 85]]
print(sorted(weekly_temperature, key=lambda t: time.strptime(t[0], '%A')))
locale.setlocale(locale.LC_TIME, orig_locale)
英文:

You can use time.strptime to parse the day of the week, so you can use it as a key for sorting:

import time

weekly_temperature = [['Saturday', 100], ['Wednesday', 95], ['Friday', 80],
['Monday', 95], ['Sunday', 90], ['Tuesday', 100], ['Thursday', 85]]
print(sorted(weekly_temperature, key=lambda t: time.strptime(t[0], '%A')))

This outputs:

[['Monday', 95], ['Tuesday', 100], ['Wednesday', 95], ['Thursday', 85], ['Friday', 80], ['Saturday', 100], ['Sunday', 90]]

Note that time.strptime depends on your locale settings, and if you want to be on the safe side you can save your current time locale, set it to the standard C locale, parse your day of the week, and then restore your local setting:

import time
import locale

orig_locale = locale.setlocale(locale.LC_TIME)
locale.setlocale(locale.LC_TIME, 'C')
weekly_temperature = [['Saturday', 100], ['Wednesday', 95], ['Friday', 80],
['Monday', 95], ['Sunday', 90], ['Tuesday', 100], ['Thursday', 85]]
print(sorted(weekly_temperature, key=lambda t: time.strptime(t[0], '%A')))
locale.setlocale(locale.LC_TIME, orig_locale)

答案2

得分: 0

weekly_temperature = [['星期六', 100], ['星期三', 95], ['星期五', 80], ['星期一', 95], ['星期天', 90], ['星期二', 100], ['星期四', 85]] week = sorted(weekly_temperature, key=lambda x: d[x[0]])

英文:
weekly_temperature = [['Saturday', 100], ['Wednesday', 95], ['Friday', 80], ['Monday', 95], ['Sunday', 90], ['Tuesday', 100], ['Thursday', 85]]
d = {
    "Monday": 1,
    "Tuesday": 2,
    "Wednesday": 3,
    "Thursday": 4,
    "Friday": 5,
    "Saturday": 6,
    "Sunday": 7
}

week = sorted(weekly_temperature, key=lambda x: d[x[0]])

答案3

得分: 0

你所需要的是一个将星期几转换成相应排序键的结构间接引用:

例如:

days = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday"

weekly_temperature = [['Saturday', 100], ['Wednesday', 95], ['Friday', 80], 
                      ['Monday', 95], ['Sunday', 90], ['Tuesday', 100],
                      ['Thursday', 85]]

weekly_temperature.sort(key=lambda t:days.index(t[0]))

[['Monday', 95], ['Tuesday', 100], ['Wednesday', 95], ['Thursday', 85],
 ['Friday', 80], ['Saturday', 100], ['Sunday', 90]]

你可以使用字典或列表来代替字符串,只要你成功将星期几转换为相应的排序键即可。

英文:

What you need is an indirection to a structure that will convert day names into a sort key in the appropriate order:

For example:

days = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday"


weekly_temperature = [['Saturday', 100], ['Wednesday', 95], ['Friday', 80], 
                      ['Monday', 95], ['Sunday', 90], ['Tuesday', 100],
                      ['Thursday', 85]]

weekly_temperature.sort(key=lambda t:days.index(t[0]))

[['Monday', 95], ['Tuesday', 100], ['Wednesday', 95], ['Thursday', 85],
 ['Friday', 80], ['Saturday', 100], ['Sunday', 90]]

You can use a dictionary or a list instead of a string as long as you manage to turn the day name into the appropriate sort key.

huangapple
  • 本文由 发表于 2023年6月29日 10:33:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定