代码:

count = 0
oldcount = 0
for char in inwords:
    if char == " ":
        anagramlist.append(inwords[oldcount, count])
        oldcount = count
        count = 0
    else:
        count += 1

错误:
Traceback (most recent call last):
  File "C:/Users/Knowhaw/Desktop/Python Programs/Anagram solver/HTS anagram.py", line 14,
in <module>
    anagramlist.append(inwords[oldcount, count])
TypeError: string indices must be integers

到底怎么回事?
count和oldcount显然是int,但错误表明它们不是
我甚至会写字
anagramlist.append(inwords[int(oldcount), int(count)])

得到同样的错误

最佳答案

您试图使用(oldcount, count)作为列表的索引。这是元组,不是int。
你是说:

anagramlist.append(inwords[oldcount:count])

?

关于python - Python字符串切片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8948867/

10-12 19:25