python - 索引时 int 对象不可下标
问题描述
我只是想编写一个简单的代码来找出具有最大相似元素数量的字符串,但是在将字符串的各个元素与目标字符串进行比较时,我得到了错误 int object is not subscriptable。编辑:错误在行中:如果 a[b]==t[b]: please help 。在代码中,我获取了一个字符串列表,然后将其与目标字符串字母表进行比较,并打印具有最多相似字母表(具有相同索引号)的字符串
n=int(input('enter the number of elements in the list'))
t=input('enter the target string')
l=[]
for x in range(0,n):
st=input('enter the string')
l.append (st)
length=len(t)
high=0
for a in l:
score=0
for b in range(0,length):
if a[b]==t[b]:
score+=1
if score>high:
high=score
word=a
print('the word with the maximum score is :',word)
解决方案
您将整数附加到 l
for x in range(0,n):
st=input('enter the string')
l.append (x) #here
和你循环 l
for a in l:
score=0
for b in range(0,length):
if a[b]==t[b]: <--------- #here you say the b'th value of the integer a
整数不是数组,它只有一个值
推荐阅读
- java - 定义变量时调用类函数
- angular - ionic 4 中的社交媒体实施不起作用
- c++ - 为什么 C++ 中的函数重载解析会考虑静态调用的非静态成员函数?
- windows - 无法在 Windows 10 上启动 Spyder
- python - 无法使用 docker 运行客户端-服务器应用程序
- eclipse - 当我尝试输入 Jdatechooser 选择的日期时,我收到错误“数据异常:无效的日期时间格式”
- python - 无法在 Raspbian 上的 wx.Panel 上设置(某些类型的)边框
- html - 折叠时如何对齐导航栏切换右侧的导航栏?
- python - For 循环未正确执行
- r - 如何在循环中生成多个数据帧