how to apply filter function to items in multiple list?

Roy

I wanna get values from multiple(?) list, which is not 0(I think filter can be one of solutions).

list as below:

>>ls = [[i for i in np.random.randint(0, 5, 5)] for _ in range(7)]
>>ls
>>
[[2, 3, 3, 0, 0],
 [4, 2, 4, 3, 2],
 [1, 2, 4, 2, 4],
 [2, 3, 4, 3, 1],
 [0, 1, 0, 3, 0],
 [3, 4, 4, 4, 3],
 [3, 4, 3, 3, 2]]

Expected result is:

[2,3,3,4,2,4,3,2,1,2,4,2,4,2,3,4,3,1,1,3,3,4,4,4,3,3,4,3,3,2]

I tried using filter function, wanted expand this idea, but I failed:

>> [elem for elem in filter(lambda x: x if x != 0 else False, ls[0])]
>> 
[2, 3, 3]

I wanna find fastest way to get expected result, not using for loop. Would you suggest any good idea?

Edit:

Oops, Sorry for confusing you. I saying 'not usting for loop' means, I wanna use list comprehension instead of for loop, because I heard list comprehension fater than for loop.

yatu

Use a nested list comprehension:

[j for i in ls for j in i if j != 0]

ls = [[i for i in np.random.randint(0, 5, 5)] for _ in range(7)]

[[1, 0, 3, 0, 0],
 [1, 2, 2, 3, 0],
 [1, 1, 1, 4, 3],
 [1, 0, 3, 0, 4],
 [2, 0, 3, 0, 2],
 [1, 0, 4, 4, 0],
 [2, 4, 1, 1, 2]]

[j for i in ls for j in i if j != 0]
# [1, 3, 1, 2, 2, 3, 1, 1, 1, 4, 3, 1, 3, 4, 2, 3, 2, 1, 4, 4, 2, 4, 1, 1, 2]

If you want to avoid any explicit looping here's an option using itertools.chain and filter:

from itertools import chain

list(filter(lambda x: x != 0, chain(*ls)))
# [1, 3, 1, 2, 2, 3, 1, 1, 1, 4, 3, 1, 3, 4, 2, 3, 2, 1, 4, 4, 2, 4, 1, 1, 2]

Looks like you're also using NumPy for creating the list. Note that this would be way simpler and more efficient using np.nonzero:

import numpy as np
a = np.random.randint(0, 5, (7,5))
a[np.nonzero(a)]
# [1, 3, 1, 2, 2, 3, 1, 1, 1, 4, 3, 1, 3, 4, 2, 3, 2, 1, 4, 4, 2, 4, 1, 1, 2]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to apply dictionary to list of items with Jinja filter?

How to search/filter list by multiple items?

How to apply jinja2 filter to ansible list items?

How to filter measure in the Filter function by multiple items from the same hierarchy

How to apply on the draggable jQuery UI function on multiple items?

How to apply filter function on 2D array/list in python

How to apply animations to items in a list

How to apply a filter with multiple options

Pandas apply multiple function with list

Apply function to all items in a list Python

VB.net Apply function to items in a list

How to filter multiple elements/items

How to list items in a function?

How to use apply function with list of functions with multiple argument in r?

How to return multiple values including a list in pandas apply function?

How do I pass multiple dictionary items into a function and return a list?

Filter list items based on multiple conditions

How filter list of items depends on another list

How to filter out items that are in a list of a list?

How to apply org-todo to multiple items

How do I apply a function to all items in a list in a text or csv file with Python?

How to modify multiple items in a list?

How to apply a function in an iteratable list

How to apply a function in an iterable list

How to filter one list of items from another list of items?

how to apply same filter to multiple dataframes

How to apply multiple column filter(String) in python

How to apply filter to DataView with Multiple "AND" conditions

R apply() function returning list of multiple elements