473,396 Members | 2,098 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,396 software developers and data experts.

Is there a method that returns a character at a specified index?

Couldn't find anything here
http://www.python.org/doc/2.3.2/lib/string-methods.html

I need to cycle through lines in a file that contain words and compare it to
a string thats given, call it SSS to see if the words in the file can be
written from SSS. I am planning to grab each letter from the word one at a
time and removing it from SSS and if the letter doesn't exist then I will go
on to the next word. That's why I need a method that returns a letter at
each index. Maybe there an easier way of doing this.
Jul 18 '05 #1
3 1744
In article <bn*************@ID-198839.news.uni-berlin.de>,
Michael Loomington <ml*********@yahoo.ca> wrote:
Couldn't find anything here
http://www.python.org/doc/2.3.2/lib/string-methods.html

I need to cycle through lines in a file that contain words and compare it to
a string thats given, call it SSS to see if the words in the file can be
written from SSS. I am planning to grab each letter from the word one at a
time and removing it from SSS and if the letter doesn't exist then I will go
on to the next word. That's why I need a method that returns a letter at
each index. Maybe there an easier way of doing this.


I don't understand your description.

I'm certain there's an easier way.

My guess is that this is an example of what you think you want:
Python 2.3+ (#2, Aug 10 2003, 11:33:47)
[GCC 3.3.1 (Debian)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
alphabet = "abcdefghijklmnopqrstuvwxyz"
alphabet[3] 'd' alphabet[14] 'o'

--

Cameron Laird <cl****@phaseit.net>
Business: http://www.Phaseit.net
Jul 18 '05 #2
Michael Loomington wrote:
Couldn't find anything here
http://www.python.org/doc/2.3.2/lib/string-methods.html

I need to cycle through lines in a file that contain words and compare it
to a string thats given, call it SSS to see if the words in the file can
be
written from SSS. I am planning to grab each letter from the word one at
a time and removing it from SSS and if the letter doesn't exist then I
will go
on to the next word. That's why I need a method that returns a letter at
each index. Maybe there an easier way of doing this.


Absolutely -- much faster. Check out the string.maketrans function
in module string, and the .translate method of string instances. The
Python Cookbook, in discussion of recipes 3.6 and 3.7 on pp. 76-78,
has in-depth discussion and examples.
Alex

Jul 18 '05 #3
On Sun, 26 Oct 2003 21:11:34 -0500, "Michael Loomington" <ml*********@yahoo.ca> wrote:
Couldn't find anything here
http://www.python.org/doc/2.3.2/lib/string-methods.html

I need to cycle through lines in a file that contain words and compare it to
a string thats given, call it SSS to see if the words in the file can be
written from SSS. I am planning to grab each letter from the word one at a
time and removing it from SSS and if the letter doesn't exist then I will go
on to the next word. That's why I need a method that returns a letter at
each index. Maybe there an easier way of doing this.

I'm not sure what you intend to do if you find that "words in the file can be
written from SSS," but perhaps you will need to know where in SSS the words are?
Perhaps you can get something useful from the snippet below, which suggests checking
for word availability in SSS by using a dict. Otherwise if SSS is big, you will be
re-scanning half of SSS each time on the average to find a word, and all of it if
the word is not there, so it will be inefficient. I split out the words in the lines
assuming spaces as delimiters, but you may need to use a regex to split away punctuation
marks as well, depending on your actual application. Obviously you could get lines from
a file with "for line in file('foo.txt'):" instead of the "for line in lines:" below,
but mind the '\n' and possible other leading and trailing white space that you get from
iterating through a file by lines.
SSS = 'nuts bolts screws nails'
SSS 'nuts bolts screws nails' sslist = SSS.split()
sslist ['nuts', 'bolts', 'screws', 'nails'] ssdict = dict([(w, (SSS.index(w), len(w))) for w in sslist])
ssdict {'screws': (11, 6), 'nails': (18, 5), 'nuts': (0, 4), 'bolts': (5, 5)} lines = """\ ... line 1 has nuts and bolts
... line 2 has screws and nails
... line 3 has no words from SSS
... """.splitlines() lines ['line 1 has nuts and bolts', 'line 2 has screws and nails', 'line 3 has no words from SSS'] for line in lines:

... print 'Line: %r' % line
... words = line.split()
... for word in words:
... if word in ssdict:
... print ' %r is %s chars starting at %s in SSS' %(
... word, ssdict[word][1], ssdict[word][0])
...
Line: 'line 1 has nuts and bolts'
'nuts' is 4 chars starting at 0 in SSS
'bolts' is 5 chars starting at 5 in SSS
Line: 'line 2 has screws and nails'
'screws' is 6 chars starting at 11 in SSS
'nails' is 5 chars starting at 18 in SSS
Line: 'line 3 has no words from SSS'

Regards,
Bengt Richter
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: heavyone | last post by:
I am trying to write a removeFirst method for an ArrayList of the generic type. The removeFirst method is supposed to do a few things: 1) If the list is empty it throws an error. 2) It stores the...
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
0
by: tickle | last post by:
Need to convert this PL/SQL script to Dynamic SQL Method 2 * copybook - celg02u3.sql SIR 24265 * * updates dt_deny for all rows in * * ...
3
by: truezplaya | last post by:
I have my collectionOfCars and my car class. In my car class i have written public override string ToString() { return String.Format(_carColour + " " + _carMake); }
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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.