在将项目添加到列表之前,如何按字母顺序对其进行排序?
我有一个作业,我应该找到一行中的每个单词,并将该单词添加到列表中。然后还有另一个列表与单词列表相对应,但该列表将告诉该单词在文本中出现的次数。
我已经完成了那部分。但是,我找不到一种方法将新找到的单词与列表中的单词进行比较,并找到索引以按字母顺序将其插入列表中。我知道我应该编写一个函数来在列表中找到该索引,这样我就可以在两个列表中插入该项目。我不允许使用排序运算符,所以我遇到了一些麻烦。谁能帮我只使用条件运算符编写一个函数。
如果我不清楚,请告诉我。
I have an assignment that I am supposed to find each word in a line, and add that word to a list. Then there is also another list corresponding to the list of word, but that list will tell the amount of times the word appear in the text.
I have finished that part. However, I cannot find a way to compare the new found word to the word in the list, and find the index to insert it in the list in an alphabetical order. I know that I am supposed to write a function that will find that index in the list, so i can insert that item in both lists. I am not allowed to use the sort operator, so I am having a little trouble. Can anyone help me writing that one function using only conditions operators.
If I am not clear, please let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作业提示:查看 Python 的 bisect 模块的源代码。这展示了如何查找索引并在排序列表中进行插入。
Homework hint: look at Python's source code for the bisect module. That shows how to find indexes and make insertions in a sorted list.