类型错误:字符串索引必须是整数,而不是str 4

时间:2023-02-04 17:02:53

I'm trying to make a code that simply puts an "o" in front of every consonant in any word given and I don't know where to go from here, I just get the error "TypeError: string indices must be integers, not str"

我试着编写一个代码在每个单词的辅音前面加上一个"o"我不知道从哪里开始,我只得到了错误的"TypeError:字符串指标必须是整数,而不是str"

k = list('b' + 'c' + 'd' + 'f' + 'g' + 'h' + 'j' + 'k' + 'l' + 'm' + 'n' + 'p' + 'q' + 'r' + 's' + 't' + 'v' + 'w' + 'x' + 'z')
for bok in k:
    text = list(raw_input("Give a phrase to code: "))
    print bok["0"]

3 个解决方案

#1


1  

An easier approach would be to use a regular expression, eg:

更简单的方法是使用正则表达式,例如:

import re

print re.sub('([bcdfghjklmnpqrstvwxyz])', r'o\1', 'tobias')
# otoobiaos

This looks for any of the inbetween the [] (the consonants) and replaces it with o followed by the letter it found.

它查找任何中间的[](辅音),并用o替换它,后面跟着它找到的字母。

Eg, getting user input:

例如,用户输入:

word = raw_input('Enter a word: ')
print re.sub('([bcdfghjklmnpqrstvwxyz])', r'o\1', word)

#2


2  

You are using print bok["0"], using the string "0" as the index. You need to use an integer, so replace like so:

您正在使用print bok["0"],使用字符串"0"作为索引。您需要使用一个整数,所以要这样替换:

  print bok[0]

I'm not sure I understand your overall goal here, but this will resolve the error for which you posted this question.

我不确定我是否理解您的总体目标,但这将解决您发布这个问题时的错误。

#3


2  

Firstly, array indexing is done with integers, not strings:

首先,数组索引是用整数而不是字符串完成的:

>>> a = [1, 2, 3]
>>> a["0"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
>>> a[0]
1

Secondly, you can make a string without using so many +es:

第二,你不用那么多的+es就可以做一个字符串:

>>> 'b' + 'c' + 'd'
'bcd'
>>> 'bcd'
'bcd'

Thirdly, you can iterate through a string without having to turn it into a list:

第三,你可以遍历一个字符串而不用把它变成一个列表:

>>> for c in list("abc"):
...     print c
... 
a
b    
c
>>> for c in "abc":
...     print c
... 
a
b        
c

Fourthly, iterating through a string gives you a string with each character in turn. You don't have to get the 0th index of that string - that's the same thing:

第四,遍历一个字符串,依次为每个字符提供一个字符串。你不需要得到那个字符串的第0个索引,这是一样的:

>>> "b"
'b'
>>> "b"[0]
'b'
>>> "b"[0] == "b"
True

#1


1  

An easier approach would be to use a regular expression, eg:

更简单的方法是使用正则表达式,例如:

import re

print re.sub('([bcdfghjklmnpqrstvwxyz])', r'o\1', 'tobias')
# otoobiaos

This looks for any of the inbetween the [] (the consonants) and replaces it with o followed by the letter it found.

它查找任何中间的[](辅音),并用o替换它,后面跟着它找到的字母。

Eg, getting user input:

例如,用户输入:

word = raw_input('Enter a word: ')
print re.sub('([bcdfghjklmnpqrstvwxyz])', r'o\1', word)

#2


2  

You are using print bok["0"], using the string "0" as the index. You need to use an integer, so replace like so:

您正在使用print bok["0"],使用字符串"0"作为索引。您需要使用一个整数,所以要这样替换:

  print bok[0]

I'm not sure I understand your overall goal here, but this will resolve the error for which you posted this question.

我不确定我是否理解您的总体目标,但这将解决您发布这个问题时的错误。

#3


2  

Firstly, array indexing is done with integers, not strings:

首先,数组索引是用整数而不是字符串完成的:

>>> a = [1, 2, 3]
>>> a["0"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
>>> a[0]
1

Secondly, you can make a string without using so many +es:

第二,你不用那么多的+es就可以做一个字符串:

>>> 'b' + 'c' + 'd'
'bcd'
>>> 'bcd'
'bcd'

Thirdly, you can iterate through a string without having to turn it into a list:

第三,你可以遍历一个字符串而不用把它变成一个列表:

>>> for c in list("abc"):
...     print c
... 
a
b    
c
>>> for c in "abc":
...     print c
... 
a
b        
c

Fourthly, iterating through a string gives you a string with each character in turn. You don't have to get the 0th index of that string - that's the same thing:

第四,遍历一个字符串,依次为每个字符提供一个字符串。你不需要得到那个字符串的第0个索引,这是一样的:

>>> "b"
'b'
>>> "b"[0]
'b'
>>> "b"[0] == "b"
True