How to parsing a sequence of integers

S

Steven Woody

Hi,

I am a newbie and is reading the python book. Could anyone tell me,
how to parsing the following string
"123 100 12 37 ..."
into a list of integers on which I can then apply max()/min()?

In additional to max/min, is there something like average()?

Thanks in advance.

-
narke
 
M

Marc 'BlackJack' Rintsch

Hi,

I am a newbie and is reading the python book. Could anyone tell me, how
to parsing the following string
"123 100 12 37 ..."
into a list of integers on which I can then apply max()/min()?

In [376]: '123 100 12 37'.split()
Out[376]: ['123', '100', '12', '37']

In [377]: map(int, '123 100 12 37'.split())
Out[377]: [123, 100, 12, 37]

In [378]: max(map(int, '123 100 12 37'.split()))
Out[378]: 123
In additional to max/min, is there something like average()?

No, but it's easy to implement with `sum()`, `len()` and ``/``.

Ciao,
Marc 'BlackJack' Rintsch
 
B

Bruno Desthuilliers

Steven Woody a écrit :
Hi,

I am a newbie and is reading the python book. Could anyone tell me,
how to parsing the following string
"123 100 12 37 ..."
> into a list of integers on which I can then apply max()/min()?

source = "123 100 12 37"
list_of_ints = [int(part) for part in source.strip().split()]
In additional to max/min, is there something like average()?

Not AFAIK, but it's really trivial

def average(lst):
""" assume lst is a list of numerics """
return sum(lst) / len(lst)
 
J

John Machin

Steven Woody a écrit :> Hi,
I am a newbie and is reading the python book.  Could anyone tell me,
how to parsing the following string
   "123 100 12 37 ..."

 > into a list of integers on which I can then apply max()/min()?

source = "123 100 12 37"
list_of_ints = [int(part) for part in source.strip().split()]

The purpose of the .strip() would be ...?
 
S

Steven Woody

Steven Woody a écrit :
Hi,

I am a newbie and is reading the python book. Could anyone tell me,
how to parsing the following string
"123 100 12 37 ..."
into a list of integers on which I can then apply max()/min()?

source = "123 100 12 37"
list_of_ints = [int(part) for part in source.strip().split()]
In additional to max/min, is there something like average()?

Not AFAIK, but it's really trivial

def average(lst):
""" assume lst is a list of numerics """
return sum(lst) / len(lst)

Cool! Thank all of you.
 
B

Bruno Desthuilliers

John Machin a écrit :
Steven Woody a écrit :> Hi,
I am a newbie and is reading the python book. Could anyone tell me,
how to parsing the following string
"123 100 12 37 ..."
into a list of integers on which I can then apply max()/min()?

source = "123 100 12 37"
list_of_ints = [int(part) for part in source.strip().split()]

The purpose of the .strip() would be ...?

Err... To check if you were paying attention ?-)
 
P

Peter Otten

J

Joe Strout

Peter said:
If you are using Python 2.x:
...
So you better throw in a float(...):

Or, add

from __future__ import division

at the top of the file. I put this at the top of all my Python files,
whether I expect to be dividing or not. It just saves grief.

Cheers,
- Joe
 
M

Mensanator

Or, add

� �from __future__ import division

at the top of the file. �I put this at the top of all my Python files,
whether I expect to be dividing or not. �It just saves grief.

If you want division to be floating point.
If, like me, you rarely do floating point
division and want the "/" to mean integer
division as God intended, then you don't
put from __future__ import division in your
source files.

That's one of the good things about Python,
you can have it either way.
 
J

Joe Strout

Mensanator said:
If you want division to be floating point.
If, like me, you rarely do floating point
division and want the "/" to mean integer
division as God intended, then you don't
put from __future__ import division in your
source files.

That's one of the good things about Python,
you can have it either way.

Until you someday move up to Python 3, at which point you'll have to go
back and change all your code to use the unambiguous // operator when
you mean integer division. Better to do it now, I think, at least in
any new code you write, to save you the hassle later.

For those not familiar with the topic:

<http://www.python.org/dev/peps/pep-0238/>

Best,
- Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top