473,395 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2499
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments...
10
by: Daniel | last post by:
how to make two references to one string that stay refered to the same string reguardless of the changing value in the string?
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
4
by: Kyote | last post by:
I'm trying to persist a list of filenames. I've made a custom collection and a FileName class: 'Class to hold file name information Public Class FileNames Public fullName As String Public...
2
by: Øyvind Isaksen | last post by:
Mabye this is question is very simple, but I need to make a string-array (in a variable) with the values from a checkbox-list. THANKS :)
2
by: james | last post by:
I recieve a business object and need to create a form to edit each property. This object is huge (like over 100 properties), and I dont want to make 100 label/textboxes by hand. Does anyone know...
1
by: xahlee | last post by:
Elisp Tutorial: Make Google Earth Xah Lee, 2006-12 This page shows a example of writing a emacs lisp function that creates a Google Earth file, and creates a link to the file, as well a link...
19
by: active | last post by:
I'm using a ComboBox to display objects of a class I've defined, say CQQ. Works great except somehow I occasionally set an Item to a String object instead of an object of type CQQ. It looks...
1
by: nuffnough | last post by:
I have defined two classes with one common field (called code) and several different fields. In class A there is only one instance of any given code as all items are individual. In class B, there...
2
by: Gustaf | last post by:
I had VS (2008) telling me my class isn't enumerable when trying to use a foreach loop. This happens quite often, and every time I Google around for an hour trying to find a solution that applies to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.