473,770 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1764
In article <bn************ *@ID-198839.news.uni-berlin.de>,
Michael Loomington <ml*********@ya hoo.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 = "abcdefghijklmn opqrstuvwxyz"
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.maketran s 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*********@ya hoo.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
3823
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 data in a node pointed to by head. 3) Set the head to head's next. 4) decrement the size. 5) returns the data. I can do 1,4, and 5 but I am stuck on 2 and 3. Here is the code I have so far. Please take a look and give me a hint or suggestion:...
10
18638
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 parameter with which you can write wanted character at that position. But no, Replace method only allows replacing one known character with another. The problem is I don't know the character to replace, just must replace the character at a known...
0
2722
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 * * the removal_eligibility_link table for all persons * * in all stages associated with the victim who has * * has had a specific legal status change * EXEC SQL EXECUTE
3
1422
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
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.