You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python按元素长度对country_list列表排序?

根据元素长度对国家列表排序的Python实现方法

嘿,要给这个国家列表按元素长度排序其实挺简单的,Python里有两种常用的方式,我给你一步步讲清楚:

方法一:使用内置的sorted()函数(返回新列表,不修改原列表)

sorted()函数会生成一个全新的排序后的列表,完全不会改动原来的country_list,非常适合需要保留原数据的场景。我们只需要通过key参数指定排序依据为元素的长度(用len()函数获取即可):

country_list = ['Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Angola', 'Antigua and Barbuda', 'Argentina', 'Armenia', 'Australia', 'Austria', 'Austrian Empire', 'Azerbaijan', 'Baden*', 'Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Bavaria*', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bolivia', 'Bosnia and Herzegovina', 'Botswana', 'Brazil', 'Brunei', 'Brunswick', 'Bulgaria', 'Burkina Faso', 'Burma', 'Burundi', 'Cabo Verde', 'Cambodia', 'Cameroon', 'Canada', 'Cayman Islands, The', 'Central African Republic', 'Central American Federation*', 'Chad', 'Chile', 'China', 'Colombia', 'Comoros', 'Congo Free State', 'Costa Rica', 'Ivory Coast', 'Croatia', 'Cuba', 'Cyprus', 'Czechia', 'Czechoslovakia', 'Democratic Republic of the Congo', 'Denmark', 'Djibouti', 'Dominica', 'Dominican Republic', 'Duchy of Parma, The*', 'East Germany (German Democratic Republic)', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia', 'Eswatini', 'Ethiopia', 'Federal Government of Germany (1848-49)*', 'Fiji', 'Finland', 'France', 'Gabon', 'Gambia, The', 'Georgia', 'Germany', 'Ghana', 'Grand Duchy of Tuscany', 'Greece', 'Grenada', 'Guatemala', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti', 'Hanover*', 'Hanseatic Republics*', 'Hawaii*', 'Hesse*', 'Holy See', 'Honduras', 'Hungary', 'Iceland', 'India', 'Indonesia', 'Iran', 'Iraq', 'Ireland', 'Israel', 'Italy', 'Jamaica', 'Japan', 'Jordan', 'Kazakhstan', 'Kenya', 'Kingdom of Serbia/Yugoslavia*', 'Kiribati', 'Korea', 'Kosovo', 'Kuwait', 'Kyrgyzstan', 'Laos', 'Latvia', 'Lebanon', 'Lesotho', 'Lew Chew (Loochoo)*', 'Liberia', 'Libya', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Marshall Islands', 'Mauritania', 'Mauritius', 'Mecklenburg-Schwerin*', 'Mecklenburg-Strelitz*', 'Mexico', 'Micronesia', 'Moldova', 'Monaco', 'Mongolia', 'Montenegro', 'Morocco', 'Mozambique', 'Namibia', 'Nassau*', 'Nauru', 'Nepal', 'Netherlands, The', 'New Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'North German Confederation*', 'North German Union*', 'North Macedonia', 'Norway', 'Oldenburg*', 'Oman', 'Orange Free State*', 'Pakistan', 'Palau', 'Panama', 'Papal States*', 'Papua New Guinea', 'Paraguay', 'Peru', 'Philippines', 'Piedmont-Sardinia*', 'Poland', 'Portugal', 'Qatar', 'Republic of Genoa*', 'Republic of Korea (South Korea)', 'Republic of the Congo', 'Romania', 'Russia', 'Rwanda', 'Saint Kitts and Nevis', 'Saint Lucia', 'Saint Vincent and the Grenadines', 'Samoa', 'San Marino', 'Sao Tome and Principe', 'Saudi Arabia', 'Schaumburg-Lippe*', 'Senegal', 'Serbia', 'Seychelles', 'Sierra Leone', 'Singapore', 'Slovakia', 'Slovenia', 'Solomon Islands, The', 'Somalia', 'South Africa', 'South Sudan', 'Spain', 'Sri Lanka', 'Sudan', 'Suriname', 'Sweden', 'Switzerland', 'Syria', 'Tajikistan', 'Tanzania', 'Texas*', 'Thailand', 'Timor-Leste', 'Togo', 'Tonga', 'Trinidad and Tobago', 'Tunisia', 'Turkey', 'Turkmenistan', 'Tuvalu', 'Two Sicilies*', 'Uganda', 'Ukraine', 'Union of Soviet Socialist Republics*', 'United Arab Emirates, The', 'United Kingdom', 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Venezuela', 'Vietnam', 'Yemen', 'Zambia', 'Zimbabwe']

# 按元素长度升序排序(短的在前,长的在后)
sorted_by_length_asc = sorted(country_list, key=len)
print(sorted_by_length_asc[:10])  # 打印前10个示例:['Chad', 'Cuba', 'Laos', 'Mali', 'Nepal', 'Oman', 'Qatar', 'Togo', 'Yemen', 'Benin']

# 按元素长度降序排序(长的在前,短的在后),只需添加reverse=True参数
sorted_by_length_desc = sorted(country_list, key=len, reverse=True)
print(sorted_by_length_desc[:10])  # 打印前10个示例:['East Germany (German Democratic Republic)', 'Federal Government of Germany (1848-49)*', 'Democratic Republic of the Congo', 'Central American Federation*', 'Kingdom of Serbia/Yugoslavia*', 'Central African Republic', 'Cayman Islands, The', 'United Arab Emirates, The', 'Republic of Korea (South Korea)', 'Dominican Republic']

方法二:使用列表的.sort()方法(原地修改列表)

如果你不需要保留原列表,可以直接用列表自带的.sort()方法,它会在原列表上直接完成排序,能节省一些内存:

# 原地升序排序
country_list.sort(key=len)
print(country_list[:10])  # 同样能得到升序的前10个元素

# 原地降序排序,同样添加reverse=True参数
country_list.sort(key=len, reverse=True)
print(country_list[:10])

补充说明

  • key=len的作用是告诉排序函数:不要直接比较元素本身,而是用每个元素的长度(len(element)的计算结果)作为排序的判断依据。
  • 如果有多个元素长度相同,它们的相对顺序会和原列表里的保持一致(这叫稳定排序),Python的排序默认都是稳定的。

内容的提问来源于stack exchange,提问作者user13750521

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 12:27:39