"How do I extract multiple integers from a string input?" is a very common question.
The most common answer is "use map( )."
Nope. There are many ways to extract multiple integers from a string, like using map( ) or the ast module or writing your own function. If you use these methods, it's cool because all of them work.
The problem is that these are not good answers for new Python learners. Functions like map( ), filter( ) and reduce( ) belong to a different style of programming called functional programming. The ast module depends on the user to provide input in the correct format. And writing your own function is redundant because Python already has elegant, built-in solutions.
Introducing LIST COMPREHENSIONS and GENERATOR EXPRESSIONS, to those who didn't already know.
These are built-in Python tools to process items in a list/tuple/dict/set or other iterators. I won't go into details; you can read about them on the internet. Instead, I'd like to show three examples.
A list comprehension can process a string of integers and return a list of those integers.
```
>>> s = input( )
1 2 3
>>> x = [ int( i ) for i in s.split( ) ]
>>> x
[ 1, 2, 3 ]
```
A generator expression can be used to process a string of integers and get a tuple.
```
>>> s = input( )
1 2 3
>>> x = ( int( i ) for i in s.split( ) )
>>> x
( 1, 2, 3 )
```
You can also use a generator expression to assign the extracted integers to individual variables.
```
>>> s = input( )
1 2 3
>>> a, b, c = ( int( i ) for i in s.split( ) )
>>> a
1
>>> b
2
>>> c
3
>>> a + b + c
6
```
That's all. Note that these methods work for any numerical data type, not just integers.
If you're used to map( ), then by all means continue using it because it's a great tool. But please, let us not propagate the use of map( ) to new Python learners, when a simple 'for' loop is more Pythonic and can be used to do the job neatly and efficiently.
- Sharif Muhaha
The most common answer is "use map( )."
Nope. There are many ways to extract multiple integers from a string, like using map( ) or the ast module or writing your own function. If you use these methods, it's cool because all of them work.
The problem is that these are not good answers for new Python learners. Functions like map( ), filter( ) and reduce( ) belong to a different style of programming called functional programming. The ast module depends on the user to provide input in the correct format. And writing your own function is redundant because Python already has elegant, built-in solutions.
Introducing LIST COMPREHENSIONS and GENERATOR EXPRESSIONS, to those who didn't already know.
These are built-in Python tools to process items in a list/tuple/dict/set or other iterators. I won't go into details; you can read about them on the internet. Instead, I'd like to show three examples.
A list comprehension can process a string of integers and return a list of those integers.
```
>>> s = input( )
1 2 3
>>> x = [ int( i ) for i in s.split( ) ]
>>> x
[ 1, 2, 3 ]
```
A generator expression can be used to process a string of integers and get a tuple.
```
>>> s = input( )
1 2 3
>>> x = ( int( i ) for i in s.split( ) )
>>> x
( 1, 2, 3 )
```
You can also use a generator expression to assign the extracted integers to individual variables.
```
>>> s = input( )
1 2 3
>>> a, b, c = ( int( i ) for i in s.split( ) )
>>> a
1
>>> b
2
>>> c
3
>>> a + b + c
6
```
That's all. Note that these methods work for any numerical data type, not just integers.
If you're used to map( ), then by all means continue using it because it's a great tool. But please, let us not propagate the use of map( ) to new Python learners, when a simple 'for' loop is more Pythonic and can be used to do the job neatly and efficiently.
- Sharif Muhaha
No comments:
Post a Comment