By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,931 Members | 1,976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,931 IT Pros & Developers. It's quick & easy.

make a string a list

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
>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

P: n/a
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.