Python 类型错误:“过滤器”对象不可下标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15876259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:14:17  来源:igfitidea点击:

TypeError: 'filter' object is not subscriptable

pythonpython-idle

提问by Christopher John Scott

I am receiving the error

我收到错误

TypeError: 'filter' object is not subscriptable

When trying to run the following block of code

尝试运行以下代码块时

bonds_unique = {}
for bond in bonds_new:
    if bond[0] < 0:
        ghost_atom = -(bond[0]) - 1
        bond_index = 0
    elif bond[1] < 0:
        ghost_atom = -(bond[1]) - 1
        bond_index = 1
    else: 
        bonds_unique[repr(bond)] = bond
        continue
    if sheet[ghost_atom][1] > r_length or sheet[ghost_atom][1] < 0:
        ghost_x = sheet[ghost_atom][0]
        ghost_y = sheet[ghost_atom][1] % r_length
        image = filter(lambda i: abs(i[0] - ghost_x) < 1e-2 and
                       abs(i[1] - ghost_y) < 1e-2, sheet)
        bond[bond_index] = old_to_new[sheet.index(image[0]) + 1 ]
        bond.sort()
        #print >> stderr, ghost_atom +1, bond[bond_index], image
    bonds_unique[repr(bond)] = bond

# Removing duplicate bonds
bonds_unique = sorted(bonds_unique.values())

And

sheet_new = [] 
bonds_new = []
old_to_new = {}
sheet=[]
bonds=[] 

The error occurs at the line

错误发生在该行

bond[bond_index] = old_to_new[sheet.index(image[0]) + 1 ]

I apologise that this type of question has been posted on SO many times, but I am fairly new to Python and do not fully understand dictionaries. Am I trying to use a dictionary in a way in which it should not be used, or should I be using a dictionary where I am not using it? I know that the fix is probably very simple (albeit not to me), and I will be very grateful if someone could point me in the right direction.

我很抱歉这种类型的问题已经发布了很多次,但我对 Python 相当陌生并且不完全理解字典。我是在尝试以不应该使用的方式使用字典,还是应该在不使用它的地方使用字典?我知道修复可能非常简单(尽管对我来说不是),如果有人能指出我正确的方向,我将不胜感激。

Once again, I apologise if this question has been answered already

如果这个问题已经得到回答,我再次道歉

Thanks,

谢谢,

Chris.

克里斯。

I am using Python IDLE 3.3.1 on Windows 7 64-bit.

我在 Windows 7 64 位上使用 Python IDLE 3.3.1。

回答by Martijn Pieters

filter()in python 3 does notreturn a list, but a iterable filterobject. Call next()on it to get the firstfiltered item:

filter()在python 3中返回一个列表,而是一个可迭代的filter对象。调用next()它以获取第一个过滤的项目:

bond[bond_index] = old_to_new[sheet.index(next(image)) + 1 ]

There is no need to convert it to a list, as you only use the first value.

无需将其转换为列表,因为您只使用第一个值。

回答by DKZ

image = list(filter(lambda i: abs(i[0] - ghost_x) < 1e-2 and abs(i[1] - ghost_y) < 1e-2, sheet))

回答by K Kotagaram

Use listbefore filtercondtion then it works fine. For me it resolved the issue.

listfilter条件之前使用然后它工作正常。对我来说,它解决了这个问题。

For example

例如

list(filter(lambda x: x%2!=0, mylist))

instead of

代替

filter(lambda x: x%2!=0, mylist)