我有一个我认为可能是一个基本的问题,我是Python的新手,但是我已经搜索过了,但是我没有找到任何足够相似的问题。当我运行下面的Python代码时,我得到以下错误:
TypeError: string indices must be integers
我不太确定为什么我会得到这个类型错误,sentence.labels.score代码应该返回一个浮点数,但是其余的代码应该返回一个字符串,Python告诉我这个错误发生在"for i in tweets‘’text‘.to_list():“行。
sentiment = []
confidence = []
for i in tweets
我试图制作一个程序来检测你输入的单词中有多少个元音。这是我的源代码(我有多个代码):
a = input("word - ").lower()
for i in range(1, len(a)+1):
if a[str(i)] == "a" or "e" or "i" or "o" or "u":
print("ok")
else:
print("no")`
我得到了一个错误:
TypeError: string indices must be int
当我尝试用Python语言读取这个yaml文件时,我得到了一个用于索引的TypeError。 vehicles:
fourwheeler:
- car
- truck def main():
with open('veh_info.yaml') as file:
info = yaml.load(file, Loader=yaml.FullLoader)
for veh in info:
for fourwheeler in veh[
在做了几年的C代码之后,我又回到了Python,在训练自己恢复Python编码习惯时,我有点困惑。
我试着运行这个非常简单的小代码,但是我一直得到一个TypeError,如标题中所描述的。我已经搜索了很多,但不知道这有什么问题:
def toLower(pStr):
i = 0
for i in pStr:
if ord(pStr[i]) >= 65 and ord(pStr[i]) <= 90:
pStr[i] = chr(ord(pStr[i])+28)
return pStr
testStr = "TEST STRING"
p
我写了一些代码:
def ICP(x):
numofrepeat=0
warning=0
while numofrepeat<len(str(x)) or (x[numofrepeat]==2) or (x[numofrepeat]==3) or (x[numofrepeat]==5) or (x[numofrepeat]==7):
if (x[numofrepeat]==0):
warning=warning+1
if warning>1:
numofrepeat=len(x)
我尝试使用Python 3迭代某个数字,为此,我考虑将数字转换为string。简化后的版本如下:
def printValue(num):
value = str(num)
for i in len(value):
print(value[i])
return;
然而,尽管我得到了int不可迭代的信息:
Traceback (most recent call last):
def addUpvoteRecord(userid):
with open("vote.json") as f:
vote_list = json.load(f)
if not userid in vote_list:
vote_list[userid] = []
with open("vote.json", "w+") as f:
json.dump(vote_list, f)
@client.event
async def on_message(message) :
if me
我正在尝试做一些类似于下面的Python代码的事情
PHP代码:
if(!$my_data[$n_vertex]['visited']){
$my_data[$n_vertex-1]['visited'] = true;
}
我到底在用Python做什么
def bfs(my_data):
my_queue = [] #array to store vertices
my_queue.insert(0, my_data[0]); #pass the first value to the first index of
我正在尝试比较两个列表列表,如果它们相等。
if grafo.node[va,vb] == grafo.node[va,vb]:
我得到了这个错误:
Traceback (most recent call last):
File "C:/Python33/Archive/PythonGrafos/Alpha.py", line 85, in <module>
menugrafos()
File "C:/Python33/Archive/PythonGrafos/Alpha.py", line 55, in menugrafos
Beta.criara
TypeError:字符串索引必须是整数。例如: 20是适用的,但不是3550。 i=0
for key in dict1:
for keys in dict2:
if (dict1[key]['TITLE']==dict2[keys]['TITLE']):
if pd.isna(d1.loc[i,'DB']) is True:
d1.iloc[i]=dict2[keys]
i+=1 dict1和2看起来像这样: {'DB&
我正在尝试用Python做一个简单的子字符串匹配,虽然我记得昨晚它工作得很好,但从早上开始,这段代码就给出了以下错误。这似乎是一个已知的错误,谁能建议为什么会发生这种情况?
if string.find(row[1],drug) != -1:
print "abstract id = ", row[0],"Drug found=", drug
错误:
File "./substring.py", line 31, in <module>
if string.find(row[1],drug) != -1:
TypeE
id_list=[['ABC'], ['ABC'], ['ABC-D', 'YT', 'ITS'], ['ABCDEF'], ['ABC', 'DEF'], ['ZYZA']]
TypeError:列表索引必须是整数,而不是列表我尝试将id_list转换为元组,因为我只能将不可变作为键传递给字典,但这也不起作用。
model_dict = {}
id_list=[]
for line in fr:
for item in id_list:
matc
试图编写代码来替换/检查句子/字符串中的一个单词。当我奔跑时,它会抛出
追溯(最近一次调用):文件"python",第21行,文件“python”中的第10行,检查器TypeError:列表索引必须是整数,而不是str
这是我的密码:
def censor(text, word):
censored = text.split()
censoredword ="*"*len(word)
for n in censored:
if n == word:
censored[n] = censore
我在一个字典中有两个列表,我正在尝试将元素拼接成一个字符串
以下是我的Python代码:
a = {'A': {'A.A': [1.0, 2.0, 3.0], 'A.B': [4.0, 5.0, 6.0]}}
for i in range(len(a['A']['A.A'])):
for j in range(len(a['A']['A.B'])):
name = str(a['A']['A.A'][i]) + '+&
我的Django项目(python==3.10.3,Django==4.0.4)通过pymssql (pymssql==2.2.5)访问MSSQL服务器,抓取我需要的内容,并获得一个dicts列表。然后我保存到MongoDB (pymongo==4.2.0),但是有一些小数值需要是Decimal128。我发现和我的目标相似。所以我适应了像这样处理这个字典的列表:
def convert_decimal_iterdict(list_of_dict):
if list_of_dict is None:
return None
for i in list_of_di
我需要用Python 3使用xor加密/解密一个文件,我有一个在Python 2中运行良好的代码,但是当试图将它修改到Python 3时,会给我一些我无法解决的错误。
这段代码在Python2.7中运行得很好:
from itertools import cycle
def xore(data, key):
return ''.join(chr(ord(a) ^ ord(b)) for (a, b) in zip(data, cycle(key)))
with open('inputfile.jpg', 'rb') as encry
我有这样的代码:
import os
import pprint
print("Hello")
ModList = os.listdir("C:/Users/Dat/AppData/Roaming/.minecraft/mods")
print ("The current mods you have installed are:\n")
pprint.pprint(ModList)
DisableNumber = input("To disable a mod, type it's number in the list.\n
我正在为密码系统创建一个代码,tahe把字母转换成数字,我做错了什么?
abc = "....abcdefg......hijklm........nop.qrst......uvwx.yzæøå"
def encode(letter):
tall = abc.find(letter)
return tall
pw1 = "bb"
output = ""
for num in range(28):
output = ""
for character in pw1:
if
我有这个错误
TypeError:字符串索引必须是整数
我试着叫这个函数
def encrypt():
text = input("Type your message:\n").lower()
for i in alphabet:
if i == text[range(len(text)]:
print(i)
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', '
我正在尝试用python编写一个算法,它要求你插入一个数字,然后它会打印出插入数字中的百、几十和单位。
我想要做的是从对应于百位的数字中提取字符,并将其打印为百位,以及数十和单位。
例如,到目前为止,我尝试做的是:
number = 321
print 'Hundreds: ', number[1]
然而,当我尝试运行它时,我得到了这样的消息:
TypeError: 'int' object is not subscriptable
做我想做的事是不可能的吗?
我是Python的新手,在学习的过程中我学到了很多东西。
我正在尝试将数据从Excel文件导入到我的Python程序中。我需要能够在Python程序而不是Excel中对这些数字进行简单的计算。下面是代码现在的样子:
import csv
with open ('vals.csv','rb') as csvfile:
reader = csv.reader(csvfile)
x = next(reader)
y = next(reader)
z = next(reader)
无论出于什么原因(我应该使用JS),我都使用Python来创建加密/解密程序。在加密程序中,空格是胡萝卜(^)。在解密程序中:
msg = raw_input("Msg: ")
dmsg = list(msg)
for i in dmsg:
if i == "^":
dmsg[i] = " "
print dmsg
当给定字符串“^”时,输出是."TypeError:列表索引必须是整数,而不是Unicode。“我要找的只是一个函数或语句。
我正在使用Python 3和熊猫,我想在列表中创建一个列表,方法是迭代一个数据帧列表,并提取符合我的标准的索引。我得到的错误“列表索引必须是整数或切片,而不是str”。我认为函数工作得很好,但是for循环不工作。我需要函数在dataframe中读取列,查找小于或等于0.05的值(在第5栏中标题为“P>|z|”),并创建符合该标准的索引列表。
def list_of_new_variables(maker_df):
# Create a list with only significant p values
newlist = list(make_results[make_
当调用test_model时,它会给我以下错误:
TypeError: slice indices must be integers or None or have an __index__ method
但是我使用一个整数(一个特定的批处理)来调用test_model。Inputtest是浮点数的列表,labeltes是整数的向量。我不确定问题出在哪里。
def optimize(learning_rate = 0.1,n_epochs = 1000, batch_size = 600):
n_train_batches = len(inputt)//batch_size
n
我正在用Python尝试一些东西。我想将一个列表(平台)分成几个列表(Li),但我收到了以下错误消息:
File "C:\Users\adescamp\Skycraper\skycraper.py", line 20, in <module>
item = plateau[debut:fin]
TypeError: slice indices must be integers or None or have an __index__ method
相关的行是带有item = plateau[debut:fin]的那行
from math import s
print("<<== program to select and display words of an user input string ==>>")
s=str(input("enter a string: "))
split1=s.split()
len1=len(split1)
header1="----------------------------------------------"
heading="printing words and ther indexes"
print(he