python - 如何根据一个变量的长度使列表中有正确数量的变量
问题描述
我对编码很陌生,正在努力编写刽子手来提高我的技能
我希望代码能够检测单词的长度,然后将该长度放入数组中正确数量的变量中。我真的不知道如何描述它,但是例如,如果单词是 3 个字母长,那么数组中就会存储字母 1、字母 2 和字母 3。类似于 word[0-2] 的东西。
mainword = input("Enter your word")
lengthOfMainword = len(mainword)
number = list(mainword)
correctLetters =[number[0], number[1],number[2],number[3],number[4]#... on until lengthofmainword is met or something like number[0-lenghthOfMainword]
]
解决方案
你可以试试这个
mainword = input("Enter your word")
lengthOfMainword = len(mainword)
number = list(mainword)
#number contains all the letters in the input
correctLetters = list(set(number))
这里correctLetters
将所有字母从用户输入的输入中精确一次
推荐阅读
- flume - Flume 无法访问 s3 来写入文件 java.lang.IllegalArgumentException: Invalid hostname in URI s3://ACCESSKEY:SECRETKEY/@bucket
- postgresql - 如何像在 MySQL 中一样在 postgresql 中设置“wait_timeout”值?
- swift - 如何在 Realm 中对 WBS 格式字符串进行排序?
- nexus - 连结 3 | 如何使用 Nexus 3 API 创建(外部)用户?
- python - 实现 dotProduct 的最 Pythonic 方式是什么?
- ruby-on-rails - 即使对于非 CRUD 路由(Rails),我也应该使用资源路由吗?
- c# - 如何实现 ParallelForEach
(IEnumerable 源,ParallelOptions 并行选项,动作 身体)? - sonarqube - 无法将 sonarqube 从 7.4 迁移到 7.7
- amazon-s3 - Boto3 ContentDisposition 内联不起作用
- javascript - Pixi.js 使浏览器在使用 PIXI ticker 时崩溃