problem with number extraction

This is the place for queries that don't fit in any of the other categories.

problem with number extraction

Postby Larry » Wed Jul 10, 2013 6:22 pm

Hello !

I created a test and I have a problem.
Here is the code :

Code: Select all
mylist = ['32', '32', '32', '32', '32', '32', '3', '2', '1']

switch = {'2': '8', '32': '33', '3': '9'}

res = [d for d in mylist if d in "32" ]

print(res)
#prints ['32', '32', '32', '32', '32', '32', '3', '2']


Why the hell do I get ['32', '32', '32', '32', '32', '32', '3', '2'], ie all the part of the string matching ?

32 3 2

Is there a way to tell Python : i only want the whole sentence and not the subparts of it ?

I should get :
['32', '32', '32', '32', '32', '32']

Any idea ?

Many thanks !
Larry
 
Posts: 9
Joined: Sat Jul 06, 2013 7:33 am

Re: problem with number extraction

Postby Crimson King » Wed Jul 10, 2013 6:33 pm

Larry wrote:Hello !

I created a test and I have a problem.
Here is the code :

Code: Select all
mylist = ['32', '32', '32', '32', '32', '32', '3', '2', '1']

switch = {'2': '8', '32': '33', '3': '9'}

res = [d for d in mylist if d in "32" ]

print(res)
#prints ['32', '32', '32', '32', '32', '32', '3', '2']


Why the hell do I get ['32', '32', '32', '32', '32', '32', '3', '2'], ie all the part of the string matching ?

32 3 2

Is there a way to tell Python : i only want the whole sentence and not the subparts of it ?

I should get :
['32', '32', '32', '32', '32', '32']

Any idea ?

Many thanks !


You're the one telling Python that you want the subparts of that string (the keyword in does this), if you want the whole string try using "==" instead.
User avatar
Crimson King
 
Posts: 131
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: problem with number extraction

Postby Larry » Wed Jul 10, 2013 6:46 pm

Yes !

Another solution I found : adding a leading zero for the one-digit numbers -> 03 02 32

Both work,

Many thanks Crimson :)
Larry
 
Posts: 9
Joined: Sat Jul 06, 2013 7:33 am

Re: problem with number extraction

Postby Yoriz » Wed Jul 10, 2013 6:48 pm

Because "32" is iterable, when asking if d is in "32" it checks for 3 and 2 not 32
Code: Select all
for d in "32":
    print d

Outputs
Code: Select all
3
2

What you want to do to ensure "32" is checked as a whole is to make the whole value part of an iterable
Code: Select all
for d in ("32",):
    print d

Outputs
Code: Select all
32

using this your code now looks like this
Code: Select all
mylist = ['32', '32', '32', '32', '32', '32', '3', '2', '1']
res = [d for d in mylist if d in ("32",)]
print(res)

and Outputs
Code: Select all
['32', '32', '32', '32', '32', '32']
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1031
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: problem with number extraction

Postby Larry » Wed Jul 10, 2013 7:04 pm

Aaaaah !

Well done... I had absolutely no idea of this tip ;)

You guys on this forum are really technical and knowledgeable as well as nice.

Always a pleasure :) Really

Thanks,

Larry
Larry
 
Posts: 9
Joined: Sat Jul 06, 2013 7:33 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 3 guests