英文:
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']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论