如何在Python中按字母顺序对字符串列表进行排序?

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

How to sort a list of strings alphabetically in Python?

问题

你想要对Python中的字符串列表进行字母排序。你已经尝试使用sorted()函数,但似乎没有起作用。以下是你尝试的示例:

my_list = ['banana', 'apple', 'orange']
sorted_list = sorted(my_list)
print(sorted_list)

但是你得到的输出与原始列表仍然相同:

['banana', 'apple', 'orange']

你做错了什么?如何对字符串列表进行字母排序?

英文:

I have a list of strings in Python that I want to sort alphabetically. I've tried using the sorted() function, but it doesn't seem to be working. Here's an example of what I've tried:

my_list = ['banana', 'apple', 'orange']
sorted_list = sorted(my_list)
print(sorted_list)

But the output I get is still the same as the original list:

['banana', 'apple', 'orange']

What am I doing wrong? How can I sort my list of strings alphabetically?

答案1

得分: -1

你可以使用以下代码创建一个新的已排序列表:

sorted(my_list)

但如果你想要原地排序你的列表,可以使用以下代码:

my_list.sort()
英文:

Given the following list:

my_list = ['banana', 'apple', 'orange']

...you can create a new sorted list with:

sorted(my_list)

However, if you want your list sorted in situ then:

my_list.sort()

...is what you want

答案2

得分: -2

Code1:

my_list = ['banana', 'apple', 'orange']
my_list.sort()
print(my_list)
my_list = ['banana', 'apple', 'orange']
my_list.sort()

Code 1将给你输出结果如下:[apple, banana, orange],因为.sort()方法简单地按照字典顺序对列表进行排序。

Code2:

my_list = ['banana', 'apple', 'orange']
my_list.sort()
newSortedList=[]
for i in my_list:
    s="".join(sorted(i))
    newSortedList.append(s)
print(newSortedList)

Code 2将给你输出结果如下:['aelpp', 'aaabnn', 'aegnor']。首先,我使用.sort()方法对列表进行了排序,将my_list变成了:[apple, banana, orange]。然后,我分别对列表中的每个字符串使用sorted函数进行了排序,得到的排序后的字符列表如下:['a', 'e', 'l', 'p', 'p']。然后,我使用"".join()将这个列表连接成一个字符串:"aelpp",并将它添加到newSortedList中。上述操作对列表中的每个字符串都进行了类似的处理。希望这对你有帮助。

英文:

Code1:

  my_list = ['banana', 'apple', 'orange']
  my_list.sort()
  print(my_list)
  my_list = ['banana', 'apple', 'orange']
  my_list.sort()

Code 1 will give you output like [apple, banana, orange]
as .sort() simply sorts just like words are sorted in dictionary.

Code 2:

 my_list = ['banana', 'apple', 'orange']
 my_list.sort()
 newSortedList=[]
 for i in my_list:
     s="".join(sorted(i))
     newSortedList.append(s)
 print(newSortedList)

Code 2 will give output like ['aelpp', 'aaabnn', 'aegnor']
in code first i have sorted the list using .sort() which changes the my_list to:

[apple, banana, orange]

After which i have individually sorted each string in list with sorted function which returns list of sorted character which will look like:

['a', 'e', 'l', 'p', 'p']

then using "".join() i have joined the list and final output is a string:

"aelpp"

which i have appended to newSortedList

The above is done for each string in List.

Hope this was helpful.

huangapple
  • 本文由 发表于 2023年5月13日 19:08:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242395.html
匿名

发表评论

匿名网友

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

确定