Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Basic question

Reply
Thread Tools

Basic question

 
 
Dmitry Dzhus
Guest
Posts: n/a
 
      05-12-2007

> Actually I'm trying to convert a string to a list of float numbers:
> str = '53,20,4,2' to L = [53.0, 20.0, 4.0, 2.0]


str="53,20,4,2"
map(lambda s: float(s), str.split(','))

Last expression returns: [53.0, 20.0, 4.0, 2.0]
--
Happy Hacking.

Dmitry "Sphinx" Dzhus
http://sphinx.net.ru
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      05-12-2007
On 2007-05-12, Cesar G. Miguel <> wrote:

> Actually I'm trying to convert a string to a list of float numbers:
> str = '53,20,4,2' to L = [53.0, 20.0, 4.0, 2.0]


>>> str = '53,20,4,2'


>>> [float(w) for w in str.split(',')]


[53.0, 20.0, 4.0, 2.0]

>>> map(float,str.split(','))


[53.0, 20.0, 4.0, 2.0]

--
Grant Edwards grante Yow! I want you to
at MEMORIZE the collected
visi.com poems of EDNA ST VINCENT
MILLAY... BACKWARDS!!
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      05-12-2007
On 2007-05-12, Dmitry Dzhus <> wrote:

> str="53,20,4,2"
> map(lambda s: float(s), str.split(','))


There's no need for the lambda.

map(float,str.split(','))

Does exactly the same thing.

--
Grant Edwards grante Yow! I feel like I am
at sharing a "CORN-DOG" with
visi.com NIKITA KHRUSCHEV...
 
Reply With Quote
 
Cesar G. Miguel
Guest
Posts: n/a
 
      05-12-2007
On May 12, 3:40 pm, Dmitry Dzhus <m...@sphinx.net.ru> wrote:
> > Actually I'm trying to convert a string to a list of float numbers:
> > str = '53,20,4,2' to L = [53.0, 20.0, 4.0, 2.0]

>
> str="53,20,4,2"
> map(lambda s: float(s), str.split(','))
>
> Last expression returns: [53.0, 20.0, 4.0, 2.0]
> --
> Happy Hacking.
>
> Dmitry "Sphinx" Dzhushttp://sphinx.net.ru


Nice!

The following also works using split and list comprehension (as
suggested in a brazilian python forum):

-------------------
L = []
file = ['5,1378,1,9', '2,1,4,5']
str=''
for item in file:
L.append([float(n) for n in item.split(',')])
-------------------

Thank you for all suggestions!

 
Reply With Quote
 
Kirk Job Sluder
Guest
Posts: n/a
 
      05-12-2007
"Cesar G. Miguel" <> writes:

> I've been studying python for 2 weeks now and got stucked in the
> following problem:
>
> for j in range(10):
> print j
> if(True):
> j=j+2
> print 'interno',j
>
> What happens is that "j=j+2" inside IF does not change the loop
> counter ("j") as it would in C or Java, for example.


Granted this question has already been answered in parts, but I just
wanted to elaborate.

Although the python for/in loop is superficially similar to C and Java
for loops, they work in very different ways. Range creates a list
object that can create an iterator, and the for/in construct under the
hood sets j to the results of iterator.next(). The equivalent
completely untested java would be something like:

public ArrayList<Object> range(int n){
a = new ArrayList<Object>; //Java 1.5 addition I think.
for(int x=0,x<n,x++){
a.add(new Integer(x));
}
return a;
}


Iterator i = range(10).iterator();

Integer j;
while i.hasNext(){
j = i.next();
system.out.println(j.toString());
j = j + 2;
system.out.println("interno" + j.toString());
}

This probably has a bunch of bugs. I'm learning just enough java
these days to go with my jython.

1: Python range() returns a list object that can be expanded or
modified to contain arbitrary objects. In java 1.5 this would be one
of the List Collection objects with a checked type of
java.lang.Object. So the following is legal for a python list, but
would not be legal for a simple C++ or Java array.

newlist = range(10)
newlist[5] = "foo"
newlist[8] = open("filename",'r')

2: The for/in loop takes advantage of the object-oriented nature of
list objects to create an iterator for the list, and then calls
iterator.next() until the iterator runs out of objects. You can do
this in python as well:

i = iter(range(10))
while True:
try:
j = i.next()
print j
j = j + 2
print j
except StopIteration:
break

Python lists are not primitive arrays, so there is no need to
explicitly step through the array index by index. You can also use an
iterator on potentially infinite lists, streams, and generators.

Another advantage to for/in construction is that loop counters are
kept nicely separate from the temporary variable, making it more
difficult to accidentally short-circuit the loop. If you want a loop
with the potential for a short-circuit, you should probably use a
while loop:

j = 0
while j < 10:
if j == 5:
j = j + 2
else:
j = j + 1
print j


>
> Am I missing something?
>
> []'s
> Cesar
>


--
Kirk Job Sluder
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      05-12-2007
Cesar G. Miguel <> wrote:

> On May 12, 3:40 pm, Dmitry Dzhus <m...@sphinx.net.ru> wrote:
> > > Actually I'm trying to convert a string to a list of float numbers:
> > > str = '53,20,4,2' to L = [53.0, 20.0, 4.0, 2.0]

> >
> > str="53,20,4,2"
> > map(lambda s: float(s), str.split(','))
> >
> > Last expression returns: [53.0, 20.0, 4.0, 2.0]
> > --
> > Happy Hacking.
> >
> > Dmitry "Sphinx" Dzhushttp://sphinx.net.ru

>
> Nice!


As somebody else alredy pointed out, the lambda is supererogatory (to
say the least).


> The following also works using split and list comprehension (as
> suggested in a brazilian python forum):
>
> -------------------
> L = []
> file = ['5,1378,1,9', '2,1,4,5']
> str=''
> for item in file:
> L.append([float(n) for n in item.split(',')])


The assignment to str is useless (in fact potentially damaging because
you're hiding a built-in name).

L = [float(n) for item in file for n in item.split(',')]

is what I'd call Pythonic, personally (yes, the two for clauses need to
be in this order, that of their nesting).


Alex
 
Reply With Quote
 
sturlamolden
Guest
Posts: n/a
 
      05-12-2007
On May 12, 6:18 pm, "Cesar G. Miguel" <cesar.go...@gmail.com> wrote:

> Am I missing something?


Python for loops iterates over the elements in a container. It is
similar to Java's "for each" loop.

for j in range(10):
print j
if(True):
j=j+2
print 'interno',j

Is equivalent to:

int[] range = {0,1,2,3,4,5,6,7,8,9};
for (int j : range) {
system.out.writeln(j);
if (true) {
j += 2;
system.out.writeln("iterno" + j);
}
}

If I remember Java correctly...




 
Reply With Quote
 
Paddy
Guest
Posts: n/a
 
      05-13-2007
On May 13, 12:13 am, a...@mac.com (Alex Martelli) wrote:

> As somebody else alredy pointed out, the lambda is supererogatory (to
> say the least).


What a wonderful new word!
I did not know what supererogatory meant, and hoped it had nothing to
do with Eros
Answers.com gave me a meaning synonymous with superfluous, which
I think is what was meant here, but Chambers gave a wonderful
definition where they say it is from the RC Church practice of doing
more
devotions than are necessary so they can be 'banked' for distribution
to others (I suspect, that in the past it may have been for a fee or
a
favour).

Supererogatory, my word of the day.

- Paddy

P.S; http://www.chambersharrap.co.uk/cham...ry+&title=21st


 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      05-13-2007
Paddy <> wrote:

> On May 13, 12:13 am, a...@mac.com (Alex Martelli) wrote:
>
> > As somebody else alredy pointed out, the lambda is supererogatory (to
> > say the least).

>
> What a wonderful new word!
> I did not know what supererogatory meant, and hoped it had nothing to
> do with Eros
> Answers.com gave me a meaning synonymous with superfluous, which
> I think is what was meant here,


Kind of, yes, cfr <http://www.bartleby.com/61/60/S0896000.html> .

> but Chambers gave a wonderful
> definition where they say it is from the RC Church practice of doing
> more
> devotions than are necessary so they can be 'banked' for distribution
> to others (I suspect, that in the past it may have been for a fee or
> a favour).


"Doing more than necessary" may be wonderful in a devotional context,
but not necessarily in an engineering one (cfr also, for a slightly
different slant on "do just what's needed",
<http://en.wikipedia.org/wiki/You_Ain't_Gonna_Need_It>).

> Supererogatory, my word of the day.


Glad you liked it!-)


Alex
 
Reply With Quote
 
Cesar G. Miguel
Guest
Posts: n/a
 
      05-13-2007
On May 12, 8:13 pm, a...@mac.com (Alex Martelli) wrote:
> Cesar G. Miguel <cesar.go...@gmail.com> wrote:
>
> > On May 12, 3:40 pm, Dmitry Dzhus <m...@sphinx.net.ru> wrote:
> > > > Actually I'm trying to convert a string to a list of float numbers:
> > > > str = '53,20,4,2' to L = [53.0, 20.0, 4.0, 2.0]

>
> > > str="53,20,4,2"
> > > map(lambda s: float(s), str.split(','))

>
> > > Last expression returns: [53.0, 20.0, 4.0, 2.0]
> > > --
> > > Happy Hacking.

>
> > > Dmitry "Sphinx" Dzhushttp://sphinx.net.ru

>
> > Nice!

>
> As somebody else alredy pointed out, the lambda is supererogatory (to
> say the least).
>
> > The following also works using split and list comprehension (as
> > suggested in a brazilian python forum):

>
> > -------------------
> > L = []
> > file = ['5,1378,1,9', '2,1,4,5']
> > str=''
> > for item in file:
> > L.append([float(n) for n in item.split(',')])

>
> The assignment to str is useless (in fact potentially damaging because
> you're hiding a built-in name).
>
> L = [float(n) for item in file for n in item.split(',')]
>
> is what I'd call Pythonic, personally (yes, the two for clauses need to
> be in this order, that of their nesting).
>
> Alex


Yes, 'str' is unnecessary. I just forgot to remove it from the code.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TurboTax Basic vs. Taxcut Basic? Sharp Dressed Man Computer Support 1 01-12-2009 12:52 PM
What is the difference between Visual Basic.NET and Visual Basic 6? Jimmy Dean Computer Support 3 07-25-2005 07:05 AM
Re: Python interpreter in Basic or a Python-2-Basic translator. rrr@ronadam.com Python 0 05-02-2005 01:48 PM
Python interpreter in Basic or a Python-2-Basic translator. Engineer Python 6 05-01-2005 10:16 PM
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET Jaime MCSD 2 09-20-2003 05:16 AM



Advertisments