oka*_*123 -1 python sorting string list
我需要代码按照它们在字符串(句子)中出现的顺序对(单词)列表进行排序。
例如:
list = ["small dog", "big dog", "medium dog"]
sentence = "Jack has a big dog not a small dog or medium dog."
Run Code Online (Sandbox Code Playgroud)
然后,预期的new_list将是:
new_list = ["big dog", "small dog", "medium dog"]
Run Code Online (Sandbox Code Playgroud)
先感谢您!
您可以使用sorted以下命令按句子中的位置排序:
_list = ['small dog', 'medium dog', 'big dog']
sentence = "Jack has a big dog not a small dog or medium dog."
new_list = sorted(_list, key=sentence.find)
['big dog', 'small dog', 'medium dog']
Run Code Online (Sandbox Code Playgroud)
你真的不希望使用内置的名字的变量,如list,dict,set,等
| 归档时间: |
|
| 查看次数: |
102 次 |
| 最近记录: |