简体   繁体   English

TypeError: 'type' object 不可下标。 我怎样才能让它从二维数组中删除一个数组?

[英]TypeError: 'type' object is not subscriptable. How can I get this to remove an array from a 2d array?

I have had a look at answers to similar questions but I just can't make this work.我看过类似问题的答案,但我无法完成这项工作。 I am quite new to python.我对 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)

The error is line 37, check = set[x] I'm unsure of what is actually causing the error错误是第 37 行,check = set[x] 我不确定究竟是什么导致了错误

In the remove function, you have not defined set .remove function 中,您还没有定义set So, python thinks it's the built-in object set , which is actually not subscriptable.所以, python 认为是内置的 object set ,实际上是不可下标的。

Pass your object to the remove function, and, preferably, give it another name.将您的 object 传递给remove function,并且最好给它另一个名称。

Your remove function cant "see" your set variable (which is list, avoid using reserved words as variable name), because its not public, its defined only inside read function.您删除 function 无法“看到”您的设置变量(这是列表,避免使用保留字作为变量名),因为它不是公共的,它仅在读取 function 内部定义。 Define this variable before read function or send it as input to remove function, and it should be working.在读取 function 或将其作为输入发送以删除 function 之前定义此变量,它应该可以工作。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM