我如何从字符串更改为列表 [英] how do i change from string to list

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

问题描述

我有以下


s =" spot"


我想将其更改为列表


l = ['''''''''','''','''''


我试过l = eval(s)但是我收到了错误


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

NameError:spot


谢谢大家的时间

I have the following

s = "spot"

which I would like to change to a list

l = [''s'',''p'',''o'',''t'']

I have tried l = eval(s) but I get the error

File "<string>", line 1, in ?
NameError: spot

thank you all for your time

推荐答案

str =''spot''

lst = []#首先声明列表

str中的字母:

lst.append(letter)#append to the list


打印lst


结果:[''',''p'',''o'','t'']


cheerz,

Ivo。

" john" < MI ****** @ hotmail.com>在留言中写道

news:99 ************************** @ posting.google.c om ...
str=''spot''

lst = [] # declare the list first
for letter in str:
lst.append(letter) # append to the list

print lst

result: [''s'', ''p'', ''o'', ''t'']

cheerz,
Ivo.
"john" <mi******@hotmail.com> wrote in message
news:99**************************@posting.google.c om...
我有以下

s =" spot"

我想将其更改为清单
l = ['s'',''p'',''o'','t'']

我试过l = eval(s)但我收到了错误

文件"< string>",第1行,在?
NameError:当场

谢谢大家的时间
I have the following

s = "spot"

which I would like to change to a list

l = [''s'',''p'',''o'',''t'']

I have tried l = eval(s) but I get the error

File "<string>", line 1, in ?
NameError: spot

thank you all for your time



2004年10月24日11:05:28 -0700,john< mi ****** @ hotmail.com>写道:
On 24 Oct 2004 11:05:28 -0700, john <mi******@hotmail.com> wrote:
我有以下

s =" spot"

我想将其更改为列表
I have the following

s = "spot"

which I would like to change to a list

< br>

list(''spot'')
list(''spot'')



['s'',''p'',''o'','t'']


/ Jorgen


-

// Jorgen Grahn< jgrahn @ Ph''nglui mglw''nafh Cthulhu

\ X / algonet.se> R''lyeh wgah''nagl fhtagn!


[''s'', ''p'', ''o'', ''t'']

/Jorgen

--
// Jorgen Grahn <jgrahn@ Ph''nglui mglw''nafh Cthulhu
\X/ algonet.se> R''lyeh wgah''nagl fhtagn!


Ivo Woltring写道:
Ivo Woltring wrote:
str =''spot''

lst = []#首先声明列表
for str中的字母:
lst.append(letter)#append to the list

print lst


我只会使用list(spot)。

结果:['s'',''p'',''o'',' 't'']
str=''spot''

lst = [] # declare the list first
for letter in str:
lst.append(letter) # append to the list

print lst
I just would have used list("spot").
result: [''s'', ''p'', ''o'', ''t'']




结果相同,但更快,更透明,代码更少。


此外,请不要过头。

-

Michael Hoffman



Same result but faster, more transparent, and with a lot less code.

Also, please don''t top-post.
--
Michael Hoffman


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

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