Calling Filter Returns <Filter Object at ... >

Why does foo = filter(...) return a filter object, not a list?

Have a look at the python documentation for filter(function, iterable) (from here):

Construct an iterator from those elements of iterable for which function returns true.

So in order to get a list back you have to use list class:

shesaid = list(filter(greetings(), ["hello", "goodbye"]))

But this probably isn't what you wanted, because it tries to call the result of greetings(), which is "hello", on the values of your input list, and this won't work. Here also the iterator type comes into play, because the results aren't generated until you use them (for example by calling list() on it). So at first you won't get an error, but when you try to do something with shesaid it will stop working:

>>> print(list(shesaid))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

If you want to check which elements in your list are equal to "hello" you have to use something like this:

shesaid = list(filter(lambda x: x == "hello", ["hello", "goodbye"]))

(I put your function into a lambda, see Randy C's answer for a "normal" function)

Calling filter returns filter object at ...

It looks like you're using python 3.x. In python3, filter, map, zip, etc return an object which is iterable, but not a list. In other words,

filter(func,data) #python 2.x

is equivalent to:

list(filter(func,data)) #python 3.x

I think it was changed because you (often) want to do the filtering in a lazy sense -- You don't need to consume all of the memory to create a list up front, as long as the iterator returns the same thing a list would during iteration.

If you're familiar with list comprehensions and generator expressions, the above filter is now (almost) equivalent to the following in python3.x:

( x for x in data if func(x) ) 

As opposed to:

[ x for x in data if func(x) ]

in python 2.x

filter object at python 3.X

The following line creates a filter for each
sentence_tokens in token_list:

filtered_list_1 = list(filter(None, [remove_characters_after_tokenization(tokens)
for tokens in sentence_tokens])
for sentence_tokens in token_list)

Perhaps you wanted to create a list of lists:

filtered_list_1 = list(filter(None, ([remove_characters_after_tokenization(tokens)
for tokens in sentence_tokens]
for sentence_tokens in token_list)))

Python: list(filter object) empties the object

A filter is a special iterable object, and like a generator, you can only iterate over it once. So essentially it returns an empty list when you run it a second time.

filter object becomes empty after iteration?

This is a classic python3 doh!.

A filter is a special iterable object you can iterate over. However, much like a generator, you can iterate over it only once. So, by calling list(people2), you are iterating over each element of the filter object to generate the list. At this point, you've reached the end of the iterable and nothing more to return.

So, when you call list(people2) again, you get an empty list.

Demo:

>>> l = range(10)
>>> k = filter(lambda x: x > 5, l)
>>> list(k)
[6, 7, 8, 9]
>>> list(k)
[]

I should mention that with python2, filter returns a list, so you don't run into this issue. The problem arises when you bring py3's lazy evaluation into the picture.

array.filter is returning entire object instead of just one value

Because filter() always return an array. you want filter from return array. using [0].header You can do it !

Try this code it's work

 const testing = Array2.filter((obj) => obj.HeaderIndex === 1)[0].header;
console.log(testing, 'testing')

How to use filter method to return another property inside the object that passed the filter test?

Your filterForMatchingNotes() is not returning what you expect it to return, you are returning the value of a function that returns undefined and not the object as a whole.

const potentialThunderSounds = [{    note: 'B',    functionCall: getSoundB('audio/34.wav', 1, false)  },  {    note: 'B',    functionCall: getSoundB('audio/35.wav', 1, false)  },  {    note: 'A',    functionCall: getSoundB('audio/36.wav', 1, false)  }];
function getSoundB(a, b, c) { // does stuff};
function filterForMatchingNotes(arrayName, note) { return arrayName .filter(obj => obj.note === note);}
let note = 'B';let tempFilterArray = filterForMatchingNotes(potentialThunderSounds, note);
console.log(tempFilterArray);

Filter object properties by key in ES6

If you have a list of allowed values, you can easily retain them in an object using:

const raw = {  item1: { key: 'sdfd', value:'sdfd' },  item2: { key: 'sdfd', value:'sdfd' },  item3: { key: 'sdfd', value:'sdfd' }};
const allowed = ['item1', 'item3'];
const filtered = Object.keys(raw) .filter(key => allowed.includes(key)) .reduce((obj, key) => { obj[key] = raw[key]; return obj; }, {});
console.log(filtered);


Related Topics



Leave a reply



Submit