468,316 Members | 1,522 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,316 developers. It's quick & easy.

make a string a list

or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.

Thanks,
Nikhil
Jun 27 '08 #1
7 2362
On May 29, 4:30 pm, Nikhil <mnik...@gmail.comwrote:
or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.
Hmmm. Can you state your problem in a different way?

I find that Python excels at string handling and I've never had to
write a pure character scanner like you are suggesting. Not to say
that you don't need it, I just think that if you give us a small
example of your data and your expected outcome, it would be easier to
help you.
Jun 27 '08 #2
On May 29, 2:30 pm, Nikhil <mnik...@gmail.comwrote:
or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.

Thanks,
Nikhil
I a little confused by what you are trying to do, but:

Strings are iterable.
>>for c in "hello":
.... print "look, a letter", c
....
look, a letter h
look, a letter e
look, a letter l
look, a letter l
look, a letter o

And indexable:
>>"hello"[0]
'h'

And converting to a list is done like this:
>>list("hello")
['h', 'e', 'l', 'l', 'o']

They also have some pretty nifty methods:
>>"hello\r\nworld\r\nlets\r\nsplit\r\n".split("\r\ n")
['hello', 'world', 'lets', 'split', '']
>>"hello world".index('world')
6
>>"hello world".index('l')
2
>>"hello world".index('l', 2+1)
3
>>"hello world".index('l', 3+1)
9
>>"hello world".index('l', 9+1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found

Hopefully some of that helps.

Matt
Jun 27 '08 #3
Nikhil <mn*****@gmail.comwrites:
or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.
What problem are you trying to solve?

Are you aware that Python file objects are already iterable, yielding
one line of text per iteration?

input_file = open("foo.txt")
for line in input_file:
do_stuff(line)

--
\ "I'd take the awe of understanding over the awe of ignorance |
`\ any day." -- Douglas Adams |
_o__) |
Ben Finney
Jun 27 '08 #4
Nikhil wrote:
or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.
Why dont the ``find`` or ``index`` methods work for you?
http://docs.python.org/lib/string-methods.html

Cheers,
Alan Isaac
Jun 27 '08 #5
On May 29, 11:30 pm, Nikhil <mnik...@gmail.comwrote:
or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.

Thanks,
Nikhil
If you just want to check required value then you can even use Sets or
build your own hash table.
Jun 27 '08 #6
>or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.
They already are. They are quite like lists in many ways:
>>s = 'abcdefg'
for c in s: print c
....
a
b
c
d
e
f
g
>>s[3]
'd'
>>s = "foo\r\n"
s.find("\r")
3
>>s.replace("\r", "").replace("\n", "")
'foo'
>>>
** Posted from http://www.teranews.com **
Jun 27 '08 #7
Lie
On May 30, 4:30*am, Nikhil <mnik...@gmail.comwrote:
or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.

Thanks,
Nikhil
Isn't it already iterable? And combined with str.split(), it could be
line iterable too.
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by scottyman | last post: by
2 posts views Thread by Řyvind Isaksen | last post: by
2 posts views Thread by james | last post: by
1 post views Thread by xahlee | last post: by
2 posts views Thread by Gustaf | last post: by
1 post views Thread by howard w | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.