byyou
Posts: 3
Joined: Tue Sep 10, 2019 4:33 am

how to filter certain values ​​from a list

Sat Nov 16, 2019 11:40 am

Hello everyone...
greetings from me ....

I want to ask everyone of your help ...

I want to filter the value of a list. For example I have the following list :
--> ['1' , '2', '2', '2', '3', '4', '5']

then I want to filter the list so that it only shows the value '2'. The list I want will be like this:
-->['2', '2', '2']

please help me to solve the problem ... thank you...

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12411
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: how to filter certain values ​​from a list

Sat Nov 16, 2019 8:41 pm

assuming you mean to do this with python, so moved to python.

User avatar
rpiMike
Posts: 981
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: how to filter certain values ​​from a list

Sat Nov 16, 2019 8:58 pm

Assuming Python this is one method:

Code: Select all

chars = ['1','2','2','2','3','4','5']

filtered = []

for char in chars:
    if char=='2':
        filtered.append(char)
        
print(filtered)

kaksi
Posts: 94
Joined: Tue Mar 10, 2015 6:19 am

Re: how to filter certain values ​​from a list

Sat Nov 16, 2019 9:00 pm

Code: Select all

list_1=['1' , '2', '2', '2', '3', '4', '5']
print(list_1)
list_1=list(filter(lambda x:x=='2',list_1))
print(list_1)

User avatar
Paeryn
Posts: 2746
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: how to filter certain values ​​from a list

Sat Nov 16, 2019 9:17 pm

byyou wrote:
Sat Nov 16, 2019 11:40 am
Hello everyone...
greetings from me ....

I want to ask everyone of your help ...

I want to filter the value of a list. For example I have the following list :
--> ['1' , '2', '2', '2', '3', '4', '5']

then I want to filter the list so that it only shows the value '2'. The list I want will be like this:
-->['2', '2', '2']

please help me to solve the problem ... thank you...
Untested but this should do it.

Code: Select all

original_list = ['1' , '2', '2', '2', '3', '4', '5']

filtered_list = [x for x in original_list if x == '2']
She who travels light — forgot something.

User avatar
scruss
Posts: 2628
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: how to filter certain values ​​from a list

Sat Nov 16, 2019 9:33 pm

or, by TIMTOWTDI:

Code: Select all

>>> list = ['1' , '2', '2', '2', '3', '4', '5']
>>> list
['1', '2', '2', '2', '3', '4', '5']
>>> filtered = [f for f in list if '2' in f]
>>> filtered
['2', '2', '2']
List comprehensions are cool and all, but that language wants map() and grep() .
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

hippy
Posts: 6258
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: how to filter certain values ​​from a list

Sat Nov 16, 2019 9:43 pm

And another way to skin the cat ...

Code: Select all

list = ['1', '2', '2', '2', '3', '4', '5']
filtered = ['2'] * list.count('2')

User avatar
paddyg
Posts: 2439
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: how to filter certain values ​​from a list

Sat Nov 16, 2019 10:31 pm

Out of curiosity

Code: Select all

import timeit

setup = '''
import random
import numpy as np

list_1 = ['1' , '2', '2', '2', '3', '4', '5']
list_2 = list_1 * 1000
random.shuffle(list_2)
list_1n = np.array(list_1)
list_2n = np.array(list_2)
'''

fns = [
'''
#### vanilla loop len == 7   ##### 0.37 ms
temp = []
for c in list_1:
    if c == '2':
        temp.append(c)
list_1 = temp
''','''
#### vanilla loop len == 7000 ##### 219 ms
temp = []
for c in list_2:
    if c == '2':
        temp.append(c)
list_2 = temp
''','''
#### list comprehension len == 7 # 0.28 ms
list_1 = [i for i in list_1 if i == '2']
''','''
#### list comprehension len 7000 ## 111 ms
list_2 = [i for i in list_2 if i == '2']
''','''
#### filter lambda len 7 ######### 0.63 ms
list_1 = list(filter(lambda x:x=='2', list_1))
''','''
#### filter lambda len 7000 ####### 280 ms
list_2 = list(filter(lambda x:x=='2', list_2))
''','''
#### numpy len 7 ################# 1.71 ms
list_1n = list_1n[list_1n=='2']
''','''
#### numpy len 7000 ################ 29 ms
list_2n = list_2n[list_2n=='2']
''','''
#### lateral thinking len 7 ###### 0.14 ms
list_1 = ['2'] * list_1.count('2')
''','''
#### lateral thinking len 7000 #### 9.4 ms
list_2 = ['2'] * list_2.count('2')
''']

for f in fns:
    print(timeit.timeit(stmt=f, setup=setup, number=10000))
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

RossDv8
Posts: 243
Joined: Thu Jan 21, 2016 12:35 pm

Re: how to filter certain values ​​from a list

Sat Nov 16, 2019 11:19 pm

You might find some variations on the Python Tips site too. It has a lot of interesting stuff. This bit includes some bits about filtering:

https://book.pythontips.com/en/latest/map_filter.html
Remember, nobody is listening to you
until you fart ...

Return to “Python”