How to parse a string completely into a list

J

john.ford

I want to take a long alpha-numeric string with \n and white-space and
place ALL elements of the string (even individual parts of a long
white-space) into separate list elements. The most common way I've
seen this performed is with the split() function, however I don't
believe that it has the power to do what I am looking for.
Any suggestions?
thanks
 
C

Chris Rebert

I want to take a long alpha-numeric string with \n and white-space and
place ALL elements of the string (even individual parts of a long
white-space) into separate list elements. The most common way I've
seen this performed is with the split() function, however I don't
believe that it has the power to do what I am looking for.
Any suggestions?
thanks

Could you please define exactly what you mean by "elements" of a string?

If you mean characters, then just use list():[' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']

Regards,
Chris
 
J

john.ford

I want to take a long alpha-numeric string with \n and white-space and
place ALL elements of the string (even individual parts of a long
white-space) into separate list elements. The most common way I've
seen this performed is with the split() function, however I don't
believe that it has the power to do what I am looking for.
Any suggestions?
thanks

Could you please define exactly what you mean by "elements" of a string?

If you mean characters, then just use list():>>> list("  \n \t abc")

[' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']

Regards,
Chris

Worked like a charm.
kudos!
 
M

Matt Nordhoff

I want to take a long alpha-numeric string with \n and white-space and
place ALL elements of the string (even individual parts of a long
white-space) into separate list elements. The most common way I've
seen this performed is with the split() function, however I don't
believe that it has the power to do what I am looking for.
Any suggestions?
thanks
Could you please define exactly what you mean by "elements" of a string?

If you mean characters, then just use list():>>> list(" \n \t abc")

[' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']

Regards,
Chris

Worked like a charm.
kudos!

Why do you need to convert it to a list? Strings are sequences, so you
can do things like slice them or iterate through them by character:
.... print character
....
f
o
o--
 
J

john.ford

I want to take a long alpha-numeric string with \n and white-space and
place ALL elements of the string (even individual parts of a long
white-space) into separate list elements. The most common way I've
seen this performed is with the split() function, however I don't
believe that it has the power to do what I am looking for.
Any suggestions?
thanks
Could you please define exactly what you mean by "elements" of a string?
If you mean characters, then just use list():>>> list("  \n \t abc")
[' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']
Regards,
Chris
Worked like a charm.
kudos!

Why do you need to convert it to a list? Strings are sequences, so you
can do things like slice them or iterate through them by character:

...     print character
...
f
o
o

--

The string draws a map that I then want to be able to traverse
through. If I can count through the individual characters of a list I
can create an x-y coordinate plane for navigation.
 
T

Tim Roberts

The string draws a map that I then want to be able to traverse
through. If I can count through the individual characters of a list I
can create an x-y coordinate plane for navigation.

Well, the point Matt was making is that traversing through a list and
traversing through a string are the same.

# Given this:
s = 'abcde'
l = ['a','b','c','d','e']

# These are identical:
for ch in s:
pass
for ch in l:
pass

# And these are identical:
print s[3]
print l[3]

Slicing is identical. Subsetting is identical. The only difference is
that I can change an element of the list.
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
I want to take a long alpha-numeric string with \n and white-space and
place ALL elements of the string (even individual parts of a long
white-space) into separate list elements. The most common way I've
seen this performed is with the split() function, however I don't
believe that it has the power to do what I am looking for.
Any suggestions?

Did you try passing your string to the list() type ?

Python 2.5.1 (r251:54863, Mar 7 2008, 03:41:45)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.white-space and
.... place ALL elements of the string (even individual parts of a long
.... white-space) into separate list elements. The most common way I've
.... seen this performed is with the split() function, however I don't
.... believe that it has the power to do what I am looking for.
.... Any suggestions?
.... thanks
.... """"I want to take a long alpha-numeric string with \n and white-space
and\nplace ALL elements of the string (even individual parts of a
long\nwhite-space) into separate list elements. The most common way
I've\nseen this performed is with the split() function, however I
don't\nbelieve that it has the power to do what I am looking for.\nAny
suggestions?\nthanks\n"['I', ' ', 'w', 'a', 'n', 't', ' ', 't', 'o', ' ', 't', 'a', 'k', 'e', '
', 'a', ' ', 'l', 'o', 'n', 'g', ' ', 'a', 'l', 'p', 'h', 'a', '-', 'n',
'u', 'm', 'e', 'r', 'i', 'c', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ',
'w', 'i', 't', 'h', ' ', '\n', ' ', 'a', 'n', 'd', ' ', 'w', 'h', 'i',
't', 'e', '-', 's', 'p', 'a', 'c', 'e', ' ', 'a', 'n', 'd', '\n', 'p',
'l', 'a', 'c', 'e', ' ', 'A', 'L', 'L', ' ', 'e', 'l', 'e', 'm', 'e',
'n', 't', 's', ' ', 'o', 'f', ' ', 't', 'h', 'e', ' ', 's', 't', 'r',
'i', 'n', 'g', ' ', '(', 'e', 'v', 'e', 'n', ' ', 'i', 'n', 'd', 'i',
'v', 'i', 'd', 'u', 'a', 'l', ' ', 'p', 'a', 'r', 't', 's', ' ', 'o',
'f', ' ', 'a', ' ', 'l', 'o', 'n', 'g', '\n', 'w', 'h', 'i', 't', 'e',
'-', 's', 'p', 'a', 'c', 'e', ')', ' ', 'i', 'n', 't', 'o', ' ', 's',
'e', 'p', 'a', 'r', 'a', 't', 'e', ' ', 'l', 'i', 's', 't', ' ', 'e',
'l', 'e', 'm', 'e', 'n', 't', 's', '.', ' ', 'T', 'h', 'e', ' ', 'm',
'o', 's', 't', ' ', 'c', 'o', 'm', 'm', 'o', 'n', ' ', 'w', 'a', 'y', '
', 'I', "'", 'v', 'e', '\n', 's', 'e', 'e', 'n', ' ', 't', 'h', 'i',
's', ' ', 'p', 'e', 'r', 'f', 'o', 'r', 'm', 'e', 'd', ' ', 'i', 's', '
', 'w', 'i', 't', 'h', ' ', 't', 'h', 'e', ' ', 's', 'p', 'l', 'i', 't',
'(', ')', ' ', 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n', ',', ' ', 'h',
'o', 'w', 'e', 'v', 'e', 'r', ' ', 'I', ' ', 'd', 'o', 'n', "'", 't',
'\n', 'b', 'e', 'l', 'i', 'e', 'v', 'e', ' ', 't', 'h', 'a', 't', ' ',
'i', 't', ' ', 'h', 'a', 's', ' ', 't', 'h', 'e', ' ', 'p', 'o', 'w',
'e', 'r', ' ', 't', 'o', ' ', 'd', 'o', ' ', 'w', 'h', 'a', 't', ' ',
'I', ' ', 'a', 'm', ' ', 'l', 'o', 'o', 'k', 'i', 'n', 'g', ' ', 'f',
'o', 'r', '.', '\n', 'A', 'n', 'y', ' ', 's', 'u', 'g', 'g', 'e', 's',
't', 'i', 'o', 'n', 's', '?', '\n', 't', 'h', 'a', 'n', 'k', 's', '\n']

HTH
 
J

john.ford

(e-mail address removed) wrote:
Could you please define exactly what you mean by "elements" of a string?
If you mean characters, then just use list():>>> list("  \n \t abc")
[' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']
Regards,
Chris
Worked like a charm.
kudos!
Why do you need to convert it to a list? Strings are sequences, so you
can do things like slice them or iterate through them by character:
for character in "foo":
...     print character
...
f
o
o
--
The string draws a map that I then want to be able to traverse
through. If I can count through the individual characters of a list I
can create an x-y coordinate plane for navigation.

You can 'count' (whatever that means) equally in strings as you do in
lists. As said above, they behave exactly the same. Just strings
are imutable - e.g. you can't change individual parts of them.

Tino

 smime.p7s
4KViewDownload

Ahh, but I forgot to mention that I have to mark the path I took in
the string. So using list() and then join() are my best options.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top