python - 使用组合索引获取列表中的值
问题描述
coded = ['','','','','','','','','','',
'a','b','c','d','e','f','g','h','i',
'j','k','l','m','n','o','p','q','r',
's','t','u','v','w','x','y','z',' ']
def decode(decode):
out = ''
for x in range(1, len(str(decode)) // 2):
out += coded[int(str(decode[x * 2 - 2]) + str(decode[x * 2 - 1]))] #here is the issue
return out
print(decode(16133335202320))
我试图从输入值中每 2 个字符从列表中获取一个值。但是,在我评论的地方,它不断提出“int' object is not subscriptable”。
我该如何解决?
解决方案
您必须先将您的号码转换为字符串:
>>> print(decode(16133335202320))
...
TypeError: 'int' object is not subscriptable
# HERE ---v
>>> print(decode(str(16133335202320)))
gdxzkn
或者
你可以重写你的函数,如下所示:
def decode(decode):
decode = str(decode)
out = ''
for x in range(1, len(decode) // 2):
out += coded[int(decode[x * 2 - 2] + decode[x * 2 - 1])]
return out
>>> print(decode(16133335202320))
gdxzkn
推荐阅读
- json - Web2py 将 CSV 文件作为服务参数传递
- docker - 为每个访问者按需启动一个独特的 Docker 容器
- css - Yii2 activeCheckboxList 样式
- arrays - 为什么我的聊天没有根据带有 react-native-gifted 聊天的日期进行序列化?
- c# - 清除数据库表
- oracle - 我正在尝试在 shellscript 中执行 PL/SQL 块。但是在执行脚本时,out 变量会抛出 DB 结果和垃圾值
- php - 如何使用PHP和html检查句子SQL中单词的出现
- amazon-web-services - 我应该如何在 Postman 中测试使用 AWS API Gateway 制作的 DynamoDB PutItem API?
- python - 如何检查两个时间戳是否在允许的错误范围内
- django - 如何将 www 重定向到无 www 的 URL - Nginx