hi,
i'm doing some udp stuff and receive strings of the form '0.870000
0.250000 0.790000;\n'
what i'd need though is a list of the form [0.870000 0.250000 0.790000]
i got to the [0:-3] part to obtain a string '0.870000 0.250000
0.790000' but i can't find a way to convert this into a list. i tried
eval() but this gives me the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 1
.870000 0.250000 0.79000
and i have the same problem the other way round. e.g. i have a list
which i need to convert to a string in order to send it via udp.
btw: i cannot use pickle, since i'm sending stuff to a LISP programme.
thank you in advance for your help!
best,
robin 10 2922
robin wrote: hi,
i'm doing some udp stuff and receive strings of the form '0.870000 0.250000 0.790000;\n' what i'd need though is a list of the form [0.870000 0.250000 0.790000] i got to the [0:-3] part to obtain a string '0.870000 0.250000 0.790000' but i can't find a way to convert this into a list. i tried eval() but this gives me the following error:
check pydoc string about split
my_string = '0.870000 0.250000 0.790000;\n'
my_list = my_string.split()
print my_list
and i have the same problem the other way round. e.g. i have a list which i need to convert to a string in order to send it via udp.
my_new_string = ' '.join(my_list)
print my_new_string
Eric
Read about string split and join. E.g.:
l = '0.870000 0.250000 0.790000'
floatlist = [float(s) for s in l.split()]
In the other direction:
floatlist = [0.87, 0.25, 0.79000000000000004]
outstring = ' '.join(floatlist)
If you need to control the precision(i.e. suppress the 00004), read
about
the string formatting operator "%".
-- George Young
thanks for your answer. split gives me a list of strings, but i found a
way to do what i want:
input='0.1, 0.2, 0.3;\n'
input = list(eval(input[0:-2]))
print input [0.10000000000000001, 0.20000000000000001, 0.29999999999999999]
this does fine... but now, how do i convert this list to a string?
my_new_string = ' '.join(input)
gives me:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: sequence item 0: expected string, float found
thanks,
robin
On Wed, 12 Apr 2006 06:53:23 -0700, robin wrote: hi,
i'm doing some udp stuff and receive strings of the form '0.870000 0.250000 0.790000;\n' what i'd need though is a list of the form [0.870000 0.250000 0.790000] i got to the [0:-3] part to obtain a string '0.870000 0.250000 0.790000' but i can't find a way to convert this into a list. i tried eval() but this gives me the following error:
Traceback (most recent call last): File "<stdin>", line 1, in ? File "<string>", line 1 .870000 0.250000 0.79000
# untested!
def str2list(s):
"""Expects a string of the form '0.87 0.25 0.79;\n' and
returns a list like [0.87, 0.25, 0.79]
WARNING: this function has only limited error checking.
"""
s = s.strip() # ignore leading and trailing whitespace
if s.endswith(';'):
s = s[:-1]
L = s.split()
assert len(L) == 3, "too many or too few items in list."
return [float(f) for f in L]
and i have the same problem the other way round. e.g. i have a list which i need to convert to a string in order to send it via udp.
That's even easier.
def list2str(L):
"""Expects a list like [0.87, 0.25, 0.79] and returns a string
of the form '0.870000 0.250000 0.790000;\n'.
"""
return "%.6f %.6f %.6f;\n" % tuple(L)
--
Steven.
robin wrote: i'm doing some udp stuff and receive strings of the form '0.870000 0.250000 0.790000;\n' what i'd need though is a list of the form [0.870000 0.250000 0.790000] i got to the [0:-3] part to obtain a string '0.870000 0.250000
Actually, that's already a bug. You want [0:-2] if you're going to do
it that way. Unless you meant that your string actually has a backslash
and an "n" character, which is doubtful...
You should probably use something more like Steven's solution, although
I'd personally use s.strip(';') or at least s.rstrip(';') if I had
semicolons to remove, rather than a combined endswith(';') and slicing
the last character off.
0.790000' but i can't find a way to convert this into a list. i tried eval() but this gives me the following error:
Traceback (most recent call last): File "<stdin>", line 1, in ? File "<string>", line 1 .870000 0.250000 0.79000
(If you'd posted the full error (that's an incomplete traceback),
someone would probably have pointed out how to interpret what Python
told you so that you'd already have figured out what was wrong with that...)
-Peter
yo!
thank you everone! here's how i finally did it:
converting the string into a list:
input = net.receiveUDPData()
input = list(eval(input[0:-2]))
converting the list into a string:
sendout = "%.6f %.6f %.6f;\n" % tuple(winningvector)
maybe i'll even find a way to generalize the list2string bit, so it
accepts arbitrary sized lists...
thanks,
robin
robin wrote: thanks for your answer. split gives me a list of strings,
Of course, why should it be otherwise ?-)
More seriously : Python doesn't do much automagical conversions. Once
you've got your list of strings, you have to convert'em to floats.
but i found a way to do what i want:
input='0.1, 0.2, 0.3;\n' input = list(eval(input[0:-2]))
- eval() is potentially harmful. Using it on untrusted inputs is a bad
idea. In fact, using eval() (or exec) is a bad idea in most cases.
- removing trailing or leading chars is best done with str.strip()
Also, your code is not as readable as the canonical solution (split() +
float)
print input
[0.10000000000000001, 0.20000000000000001, 0.29999999999999999]
input = map(float, input.strip('\n;').split(","))
[0.10000000000000001, 0.20000000000000001, 0.29999999999999999] this does fine... but now, how do i convert this list to a string?
my_new_string = ' '.join(input)
gives me:
Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: sequence item 0: expected string, float found
Same as above - you need to do the conversion:
' '.join(map(str, input))
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
robin wrote: yo!
thank you everone! here's how i finally did it: converting the string into a list:
input = net.receiveUDPData() input = list(eval(input[0:-2]))
You'll run into trouble with this.
converting the list into a string:
sendout = "%.6f %.6f %.6f;\n" % tuple(winningvector)
maybe i'll even find a way to generalize the list2string bit, so it accepts arbitrary sized lists...
sendout = "%s;\n" % " ".join(["%.6f" % float(s) for s in winningvector])
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
robin wrote: yo!
thank you everone! here's how i finally did it: converting the string into a list:
input = net.receiveUDPData() input = list(eval(input[0:-2]))
no pun intented but as you did not know how to use split and join,
please please DON'T USE eval
Eric
On 12 Apr 2006 06:53:23 -0700 in comp.lang.python, "robin"
<ro*********@gmail.com> wrote: hi,
i'm doing some udp stuff and receive strings of the form '0.870000 0.250000 0.790000;\n' what i'd need though is a list of the form [0.870000 0.250000 0.790000] i got to the [0:-3] part to obtain a string '0.870000 0.250000 0.790000' but i can't find a way to convert this into a list. i tried eval() but this gives me the following error:
Traceback (most recent call last): File "<stdin>", line 1, in ? File "<string>", line 1 .870000 0.250000 0.79000
and i have the same problem the other way round. e.g. i have a list which i need to convert to a string in order to send it via udp. btw: i cannot use pickle, since i'm sending stuff to a LISP programme.
Here's a little playing around I did the the interactive interpreter.
Not guaranteed to be the most efficient or Pythonic, but it seems to
work... instr = '0.870000 0.250000 0.790000;\n' instr
'0.870000 0.250000 0.790000;\n' ell = [float(x) for x in instr[:-2].split()] print ell
[0.87, 0.25, 0.79000000000000004] outstr = " ".join(["%8.6f"%x for x in ell]) + ";\n" outstr
'0.870000 0.250000 0.790000;\n' instr == outstr
True
Regards,
-=Dave
--
Change is inevitable, progress is not. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Klaus Neuner |
last post by:
Hello,
I would like to understand the reason for the following difference
between dealing with lists and dealing with strings: What is this
difference good for? How it is accounted for in Python...
|
by: vdavidster |
last post by:
Hello everyone,
I want to convert a tuple to a list, and I expected this behavior:
list(('abc','def')) ->
list(('abc')) ->
But Python gave me this behavior:
list(('abc','def')) ->
|
by: maphew |
last post by:
Hello,
I have some lists for which I need to remove duplicates. I found the
sets.Sets() module which does exactly this, but how do I get the set
back out again?
# existing input: A,B,B,C,D
#...
|
by: rshepard |
last post by:
I start with a list of tuples retrieved from a database table. These
tuples are extracted and put into individual lists. So I have lists that
look like this: . When I concatenate lists, I end up...
|
by: hlubenow |
last post by:
Hello,
I really like Perl and Python for their flexible lists like @a (Perl) and
a (Python), where you can easily store lots of strings or even a whole
text-file.
Now I'm not a...
|
by: CoreyWhite |
last post by:
Problem:
You have numbers in string format, but you need to convert them to a
numeric type, such as an int or float.
Solution:
You can do this with the standard library functions. The...
|
by: Joeyeti |
last post by:
Hi fellow VB knowers (I am but a learner still).
I have a question for you which I struggle with. I need to convert nested Lists in MS WORD (whether numbered or bulleted or mixed) from their...
|
by: Bart Kastermans |
last post by:
| def __str__ (self):
I did some timing of operations involved. Doing this I found that
the operation below to get a string representation for trees was
in fact not quadratic.
The final...
|
by: Gabriel Genellina |
last post by:
En Fri, 19 Sep 2008 10:59:26 -0300, Ron Brennan <brennan.ron@gmail.com>
escribió:
I guess you probably tried using ' '.join(value) and got an error like
this:
TypeError: sequence item 0:...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |