7 Replies - 506 Views - Last Post: 04 January 2017 - 12:53 PM Rate Topic: -----

#1 pemby  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 62
  • Joined: 27-June 15

casting a string to int with respect to this sequence

Posted 04 January 2017 - 11:27 AM

Using for each loop. This is a requirement for my issue.
What is the best way to cast valid numbers in this sequence to ints.

str_case = ['_1_', '_10_','_100_'] # assuming this pattern  ...  



This issue arose when my program was using
 x[:1]
notation and the number
I was extracting was larger than 9. Honestly dealing with the 100th case is not going ever come up in
as a requirement for me. But I thought I should include it purely out of self-interest.

Is This A Good Question/Topic? 0
  • +

Replies To: casting a string to int with respect to this sequence

#2 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10686
  • View blog
  • Posts: 18,302
  • Joined: 19-March 11

Re: casting a string to int with respect to this sequence

Posted 04 January 2017 - 11:37 AM

I would probably just strip out the underscores and cast:
[int(s.strip("_") for s in string_case]

Was This Post Helpful? 0
  • +
  • -

#3 pemby  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 62
  • Joined: 27-June 15

Re: casting a string to int with respect to this sequence

Posted 04 January 2017 - 11:48 AM

View Postjon.kiparsky, on 04 January 2017 - 11:37 AM, said:

I would probably just strip out the underscores and cast:
[int(s.strip("_") for s in string_case]



I may have given a poor example.
The '_' is not really an underscore.
It is an unknown type. Or None.

All we know about that string it is:
There is an int in the string.
The int can be 1 or 2 places.
The int has fixed starting point. (in the case I gave it was the 1nth position. )
The surrounding characters are unknown and not an int.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10686
  • View blog
  • Posts: 18,302
  • Joined: 19-March 11

Re: casting a string to int with respect to this sequence

Posted 04 January 2017 - 12:05 PM

View Postpemby, on 04 January 2017 - 01:48 PM, said:

All we know about that string it is:
There is an int in the string.
The int can be 1 or 2 places.
The int has fixed starting point. (in the case I gave it was the 1nth position. )
The surrounding characters are unknown and not an int.


You can write a function to extract an int from a string of this description. I would start with something as simple as

def funny_string_to_int(funny_string):
  return int("".join([d for d in funny_string if d.isdigit()]))


which ignores two of your restrictions, so it's a little more general which might be fine.

Anyway, having that in hand you can then easily write the list comp.
Was This Post Helpful? 1
  • +
  • -

#5 pemby  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 62
  • Joined: 27-June 15

Re: casting a string to int with respect to this sequence

Posted 04 January 2017 - 12:09 PM

Thanks for your help. One more question.
Is there a way to make that a lambda?
as this code is already part of a return statement and I am still learning how to use lambdas practically.

This post has been edited by andrewsw: 04 January 2017 - 12:20 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: casting a string to int with respect to this sequence

Posted 04 January 2017 - 12:20 PM

Note that you don't have to quote the previous post in full, there is a Reply button further down the page, or use the Fast Reply box.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10686
  • View blog
  • Posts: 18,302
  • Joined: 19-March 11

Re: casting a string to int with respect to this sequence

Posted 04 January 2017 - 12:27 PM

Sure, you could do something like this:
>>> fn = lambda funny_string: int("".join([d for d in funny_string if d.isdigit()]))
>>> fn("__123as")
123
>>> 


Lambdas in python are generally a matter of style, so you should be careful when using them. Since they are always* optional in python, the question to ask yourself is, does this make the code easier to read, understand, and modify, or does it make it harder?


* it's possible there's an exception, so you could put an "almost" in there, but I have not come across the exception in real live code
Was This Post Helpful? 1
  • +
  • -

#8 pemby  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 62
  • Joined: 27-June 15

Re: casting a string to int with respect to this sequence

Posted 04 January 2017 - 12:53 PM

Here is what I came up with.

g = lambda x : int("".join([d for d in x if d.isdigit()]))


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1