我想根据长度对列表中的单词进行排序。
测试用例是{'cat','jump','blue','balloon'}
当运行此方法时,它将沿以下行打印一些内容:{'balloon','jump','blue','balloon'}
我遇到的另一个问题是,我们使用的python类型运行得很好,但是当我尝试在python 3.5.2shell中运行.py时,它会出现错误。
我只需要知道我做错了什么,我能做什么来修复它,让它工作。感谢您的帮助!
可以找到.py文件here:def wordSort():
words = []
minimum = 'NaN'
index = 0
x = 0
y = 0
z = 1
#How many words to be entered
length = input('How many words would you like to enter? ')
#Put words in the number of times you entered previously
while not z >= length + 1:
words.append(raw_input('Enter word #' + str(z) + ': '))
z += 1
while x < length:
minimum = words[x]
#Goes through the list and finds a smaller word
while y < length:
if len(words[y]) < len(minimum):
minimum = words[y]
index = y
y += 1
words[index] = words[x]
words[x] = minimum
x += 1
print words
最后
以上就是感动盼望最近收集整理的关于python对列表单词排序_如何按长度对单词列表排序的全部内容,更多相关python对列表单词排序_如何按长度对单词列表排序内容请搜索靠谱客的其他文章。
发表评论 取消回复