按照第二个单字对双字组列表进行排序如何?

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

How to sort a list of bigrams according to the second unigram?

问题

我有一个二元组列表,像这样:

mylist = ["hello world", "this python", "black cat", "brown dog"]

我想按照每个元素中的第二个单词按字母顺序对该列表进行排序。结果如下:

mylist = ["black cat", "brown dog", "this python", "hello world"]

有人可以帮忙吗?
谢谢。

英文:

I have a list of bigrams like this:

mylist = ["hello world", "this python", "black cat", "brown dog"]

I want to sort this list according to the second word in each element alphabetically. The result is like this:

mylist = ["black cat", "brown dog", "this python", "hello world"]

Could anyone help?
Thanks.

答案1

得分: 2

以下是翻译好的部分:

You could do this:

    mylist.sort(key=lambda s: s.split()[1])

result

    ['黑猫', '棕狗', '这只蟒蛇', '你好世界']

请注意,中文翻译是根据英文单词的含义而不是字面翻译的。

英文:

You could do this:

mylist.sort(key=lambda s: s.split()[1])

result

['black cat', 'brown dog', 'this python', 'hello world']

huangapple
  • 本文由 发表于 2023年5月17日 15:20:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269463.html
匿名

发表评论

匿名网友

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

确定