为了账号安全,请及时绑定邮箱和手机立即绑定

如何获取一组单词并在输出中按字母顺序打印?

如何获取一组单词并在输出中按字母顺序打印?

婷婷同学_ 2023-08-15 16:37:54
import pickle`f=open('/home/nikan/Desktop/Linux os/text/word.txt', 'rb')pickle.load(f)`
查看完整描述

1 回答

?
千巷猫影

TA贡献1829条经验 获得超7个赞

问题是只输入了一个单词并且对其调用了排序方法。


让我们获取多个单词,将它们附加到列表中并对它们进行排序:


input_words = []


# Get input words

word = input('Please enter word\n')

# As long as word is not "stop"

while word != 'stop':

    # Get new input word

    input_words.append(word)

    # Append to list of words

    word = input('Please enter word\n')


# Sort words

input_words.sort()


# Print sorted list of words

print(input_words)


# Write to file

with open('your_file.txt', 'w') as f:

    for item in input_words:

        f.write("%s\n" % item)


查看完整回答
反对 回复 2023-08-15
  • 1 回答
  • 0 关注
  • 90 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信