python对列表单词排序_如何按长度对单词列表排序

我想根据长度对列表中的单词进行排序。

测试用例是{'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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值