将列表转换为字符串到列表 [英] converting lists to strings to lists

查看:106
本文介绍了将列表转换为字符串到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在做一些udp的东西并收到0.870000

0.250000 0.790000; \ n''形式的字符串

我需要的是一个表单[0.870000 0.250000 0.790000]

i到[0:-3]部分获取字符串'' 0.870000 0.250000

0.790000''但我无法找到将其转换为列表的方法。我试过了

eval()但是这给了我以下错误:


回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

文件"< string>",第1行

.870000 0.250000 0.79000


和我有相同的问题反过来。例如我有一个清单

我需要转换成一个字符串才能通过udp发送它。

顺便说一句:我不能使用泡菜,因为我发送东西到LISP计划。


提前感谢您的帮助!

最好,


robin



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

推荐答案

robin写道:


我正在做一些udp的东西和接收字符串形式''0.870000
0.250000 0.790000; \ n''
我需要的是一个表格列表[0.870000 0.250000 0.790000]
我得到[0:-3]部分得到一个字符串''0.870000 0.250000
0.790000'',但我找不到将其转换为列表的方法。我试过了
eval()但是这给了我以下错误:


检查关于拆分的pydoc字符串


my_string ='' 0.870000 0.250000 0.790000; \ n''

my_list = my_string.split()

打印my_list


我有反过来也是同样的问题。例如我有一个列表
我需要转换为字符串,以便通过udp发送它。


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


my_new_string = '' ''.join(my_list)
print my_new_string

Eric


了解字符串拆分和连接。例如:

l =''0.870000 0.250000 0.790000''

floatlist = [float(s)for s in l.split()]


在另一个方向:

floatlist = [0.87,0.25,0.7000000000000004]

outstring =''''。join(floatlist)


如果你需要控制精度(即抑制00004),请阅读

字符串格式化运算符%。


- George Young

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


感谢您的回答。 split给了我一个字符串列表,但我找到了一个

的方法来做我想要的:


input =''0.1,0.2,0.3; \\ \\ n''

输入=列表(eval(输入[0:-2]))

打印输入
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]
[0.10000000000000001, 0.20000000000000001, 0.29999999999999999]




这样做很好......但现在,我如何将此列表转换为字符串?


my_new_string =''''。join(输入)


给了我:


Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

TypeError:序列项0:期望字符串,找到浮点数


谢谢,


robin



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


这篇关于将列表转换为字符串到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆