TypeError:“类型”对象不可下标。我怎样才能从二维数组中删除一个数组?

我已经看过类似问题的答案,但是我无法完成这项工作。我对python很陌生。

def read():

    set = []
    f = open("error set 1.txt","r")
    replace = f.read()
    f.close()

    f = open("Test1_Votes.txt","w")
    replaced = replace.replace(",","")
    f.write(replaced)
    f.close()



    f = open("Test1_Votes.txt","r")
    for line in f:
        ballot = []


        for ch in line:

            vote = ch

            ballot.append(vote)

        print (ballot)

        set.append(ballot)


    """print(set)"""
    remove()

def remove():
    for i in range (70):
        x = i - 1
        check = set[x]
        if 1 not in check:
            set.remove[x]
    print(set)

错误是第37行,检查= set [x] 我不确定导致错误的原因

zyyzyyzhaoyiying 回答:TypeError:“类型”对象不可下标。我怎样才能从二维数组中删除一个数组?

remove函数中,您尚未定义set。因此,python认为它是内置对象set,实际上是无法下标的。

将您的对象传递给remove函数,并最好为其命名。

,

您的删除函数无法“看到”您设置的变量(列表,请避免使用保留字作为变量名),因为它不是公共的,仅在内部读取函数中定义。 在读取函数之前定义此变量,或者将其作为删除函数的输入发送,它应该可以正常工作。

def read():
    set = []
    f = open("error set 1.txt","r")
    replace = f.read()
    f.close()

    f = open("Test1_Votes.txt","w")
    replaced = replace.replace(",","")
    f.write(replaced)
    f.close()

    f = open("Test1_Votes.txt","r")
    for line in f:
        ballot = []


    for ch in line:
        vote = ch
        ballot.append(vote)

    print (ballot)

    set.append(ballot)


    """print(set)"""
    remove(set)

def remove(set):
    for i in range (70):
        x = i - 1
        check = set[x]
        if 1 not in check:
            set.remove(x)
    print(set)
本文链接:https://www.f2er.com/3144332.html

大家都在问