Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'filter' object is not subscriptable

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.

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.

like image 807
Christopher John Scott Avatar asked Apr 08 '13 09:04

Christopher John Scott


People also ask

How do I fix TypeError method object is not Subscriptable?

Methods are not subscriptable objects and therefore cannot be accessed like a list with square brackets. To solve this error, replace the square brackets with the round brackets after the method's name when you are calling it.

How do I fix error int object is not Subscriptable?

The “typeerror: 'int' object is not subscriptable” error is raised when you try to access an integer as if it were a subscriptable object, like a list or a dictionary. To solve this problem, make sure that you do not use slicing or indexing to access values in an integer.

How do you filter a list of objects in Python?

Filter a Python List with filter() The filter(function, iterable) function takes a function as input that takes on argument (a list element) and returns a Boolean value whether this list element should pass the filter. All elements that pass the filter are returned as a new iterable object (a filter object).

How do I fix TypeError NoneType object is not Subscriptable?

TypeError: 'NoneType' object is not subscriptable Solution The best way to resolve this issue is by not assigning the sort() method to any variable and leaving the numbers. sort() as is.


Video Answer


2 Answers

filter() in python 3 does not return a list, but an iterable filter object. Use the next() function on it to get the first filtered item:

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.

Iterable objects like filter() produce results on demand rather than all in one go. If your sheet list is very large, it might take a long time and a lot of memory to put all the filtered results into a list, but filter() only needs to evaluate your lambda condition until one of the values from sheet produces a True result to produce one output. You tell the filter() object to scan through sheet for that first value by passing it to the next() function. You could do so multiple times to get multiple values, or use other tools that take iterables to do more complex things; the itertools library is full of such tools. The Python for loop is another such a tool, it too takes values from an iterable one by one.

If you must have access to all filtered results together, because you have to, say, index into the results at will (e.g. because this time your algorithm needed to access index 223, index 17 then index 42) only then convert the iterable object to a list, by using list():

image = list(filter(lambda i: ..., sheet)) 

The ability to access any of the values of an ordered sequence of values is called random access; a list is such a sequence, and so is a tuple or a numpy array. Iterables do not provide random access.

like image 83
Martijn Pieters Avatar answered Sep 17 '22 19:09

Martijn Pieters


Use list before filter condtion then it works fine. For me it resolved the issue.

For example

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

instead of

filter(lambda x: x%2!=0, mylist) 
like image 45
K Kotagaram Avatar answered Sep 20 '22 19:09

K Kotagaram