how to detect logical and string index in sub-list and delete it
up vote
1
down vote
favorite
I have a list like this:
A = [[1,2,3,4],[1,1,2,4],[1,2,3,False],[1,False,2,3],[1,2,3,4],[1,2,3,'word'],[5,6,7,8],[1,4,3,4],[True,1,2,4],[0,1,0,1],[0,0,0,0],[False,False,False,False]]
and I want as output a list like this:
A = [[1,2,3,4],[1,1,2,4],[1,2,3,4],[5,6,7,8],[1,4,3,4],[0,1,0,1],[0,0,0,0]]
I just want to delete or remove a any list. it have member of string or logical.
how i can do it.
python python-3.x algorithm list types
add a comment |
up vote
1
down vote
favorite
I have a list like this:
A = [[1,2,3,4],[1,1,2,4],[1,2,3,False],[1,False,2,3],[1,2,3,4],[1,2,3,'word'],[5,6,7,8],[1,4,3,4],[True,1,2,4],[0,1,0,1],[0,0,0,0],[False,False,False,False]]
and I want as output a list like this:
A = [[1,2,3,4],[1,1,2,4],[1,2,3,4],[5,6,7,8],[1,4,3,4],[0,1,0,1],[0,0,0,0]]
I just want to delete or remove a any list. it have member of string or logical.
how i can do it.
python python-3.x algorithm list types
So you want to remove any lists that haveFalse
in them?
– Ayxan
Nov 10 at 18:16
yeap, I want to remove any type is not int or float from list.
– lyca
Nov 10 at 18:18
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a list like this:
A = [[1,2,3,4],[1,1,2,4],[1,2,3,False],[1,False,2,3],[1,2,3,4],[1,2,3,'word'],[5,6,7,8],[1,4,3,4],[True,1,2,4],[0,1,0,1],[0,0,0,0],[False,False,False,False]]
and I want as output a list like this:
A = [[1,2,3,4],[1,1,2,4],[1,2,3,4],[5,6,7,8],[1,4,3,4],[0,1,0,1],[0,0,0,0]]
I just want to delete or remove a any list. it have member of string or logical.
how i can do it.
python python-3.x algorithm list types
I have a list like this:
A = [[1,2,3,4],[1,1,2,4],[1,2,3,False],[1,False,2,3],[1,2,3,4],[1,2,3,'word'],[5,6,7,8],[1,4,3,4],[True,1,2,4],[0,1,0,1],[0,0,0,0],[False,False,False,False]]
and I want as output a list like this:
A = [[1,2,3,4],[1,1,2,4],[1,2,3,4],[5,6,7,8],[1,4,3,4],[0,1,0,1],[0,0,0,0]]
I just want to delete or remove a any list. it have member of string or logical.
how i can do it.
python python-3.x algorithm list types
python python-3.x algorithm list types
edited Nov 10 at 18:20
Daniel Mesejo
7,9741922
7,9741922
asked Nov 10 at 18:12
lyca
112
112
So you want to remove any lists that haveFalse
in them?
– Ayxan
Nov 10 at 18:16
yeap, I want to remove any type is not int or float from list.
– lyca
Nov 10 at 18:18
add a comment |
So you want to remove any lists that haveFalse
in them?
– Ayxan
Nov 10 at 18:16
yeap, I want to remove any type is not int or float from list.
– lyca
Nov 10 at 18:18
So you want to remove any lists that have
False
in them?– Ayxan
Nov 10 at 18:16
So you want to remove any lists that have
False
in them?– Ayxan
Nov 10 at 18:16
yeap, I want to remove any type is not int or float from list.
– lyca
Nov 10 at 18:18
yeap, I want to remove any type is not int or float from list.
– lyca
Nov 10 at 18:18
add a comment |
1 Answer
1
active
oldest
votes
up vote
5
down vote
We can do this with list comprehension where we perform a filter with any(..)
that checks if there is any element that is an instance of str
or bool
:
[sublist for sublist in A if not any(isinstance(e, (str, bool)) for e in sublist)]
this then yields:
>>> [sublist for sublist in A if not any(isinstance(e, (str, bool)) for e in sublist)]
[[1, 2, 3, 4], [1, 1, 2, 4], [1, 2, 3, 4], [5, 6, 7, 8], [1, 4, 3, 4], [0, 1, 0, 1], [0, 0, 0, 0]]
what is meaning of e ?
– lyca
Nov 10 at 18:21
@lyca: the element in the generator, it enumerates oversublist
with... for e in sublist
.
– Willem Van Onsem
Nov 10 at 18:22
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
We can do this with list comprehension where we perform a filter with any(..)
that checks if there is any element that is an instance of str
or bool
:
[sublist for sublist in A if not any(isinstance(e, (str, bool)) for e in sublist)]
this then yields:
>>> [sublist for sublist in A if not any(isinstance(e, (str, bool)) for e in sublist)]
[[1, 2, 3, 4], [1, 1, 2, 4], [1, 2, 3, 4], [5, 6, 7, 8], [1, 4, 3, 4], [0, 1, 0, 1], [0, 0, 0, 0]]
what is meaning of e ?
– lyca
Nov 10 at 18:21
@lyca: the element in the generator, it enumerates oversublist
with... for e in sublist
.
– Willem Van Onsem
Nov 10 at 18:22
add a comment |
up vote
5
down vote
We can do this with list comprehension where we perform a filter with any(..)
that checks if there is any element that is an instance of str
or bool
:
[sublist for sublist in A if not any(isinstance(e, (str, bool)) for e in sublist)]
this then yields:
>>> [sublist for sublist in A if not any(isinstance(e, (str, bool)) for e in sublist)]
[[1, 2, 3, 4], [1, 1, 2, 4], [1, 2, 3, 4], [5, 6, 7, 8], [1, 4, 3, 4], [0, 1, 0, 1], [0, 0, 0, 0]]
what is meaning of e ?
– lyca
Nov 10 at 18:21
@lyca: the element in the generator, it enumerates oversublist
with... for e in sublist
.
– Willem Van Onsem
Nov 10 at 18:22
add a comment |
up vote
5
down vote
up vote
5
down vote
We can do this with list comprehension where we perform a filter with any(..)
that checks if there is any element that is an instance of str
or bool
:
[sublist for sublist in A if not any(isinstance(e, (str, bool)) for e in sublist)]
this then yields:
>>> [sublist for sublist in A if not any(isinstance(e, (str, bool)) for e in sublist)]
[[1, 2, 3, 4], [1, 1, 2, 4], [1, 2, 3, 4], [5, 6, 7, 8], [1, 4, 3, 4], [0, 1, 0, 1], [0, 0, 0, 0]]
We can do this with list comprehension where we perform a filter with any(..)
that checks if there is any element that is an instance of str
or bool
:
[sublist for sublist in A if not any(isinstance(e, (str, bool)) for e in sublist)]
this then yields:
>>> [sublist for sublist in A if not any(isinstance(e, (str, bool)) for e in sublist)]
[[1, 2, 3, 4], [1, 1, 2, 4], [1, 2, 3, 4], [5, 6, 7, 8], [1, 4, 3, 4], [0, 1, 0, 1], [0, 0, 0, 0]]
answered Nov 10 at 18:16
Willem Van Onsem
140k16131222
140k16131222
what is meaning of e ?
– lyca
Nov 10 at 18:21
@lyca: the element in the generator, it enumerates oversublist
with... for e in sublist
.
– Willem Van Onsem
Nov 10 at 18:22
add a comment |
what is meaning of e ?
– lyca
Nov 10 at 18:21
@lyca: the element in the generator, it enumerates oversublist
with... for e in sublist
.
– Willem Van Onsem
Nov 10 at 18:22
what is meaning of e ?
– lyca
Nov 10 at 18:21
what is meaning of e ?
– lyca
Nov 10 at 18:21
@lyca: the element in the generator, it enumerates over
sublist
with ... for e in sublist
.– Willem Van Onsem
Nov 10 at 18:22
@lyca: the element in the generator, it enumerates over
sublist
with ... for e in sublist
.– Willem Van Onsem
Nov 10 at 18:22
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241969%2fhow-to-detect-logical-and-string-index-in-sub-list-and-delete-it%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So you want to remove any lists that have
False
in them?– Ayxan
Nov 10 at 18:16
yeap, I want to remove any type is not int or float from list.
– lyca
Nov 10 at 18:18