473,698 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

find all index positions

hi
say i have string like this
astring = 'abcd efgd 1234 fsdf gfds abcde 1234'
if i want to find which postion is 1234, how can i achieve this...? i
want to use index() but it only give me the first occurence. I want to
know the positions of both "1234"
thanks

May 11 '06
14 22560
On 12/05/2006 5:13 AM, vbgunz wrote:
I thought this to be a great exercise so I went the extra length to
turn it into a function for my little but growing library. I hope you
enjoy :)

Oh, indeed ;-)

def indexer(string, target):
Consider not shadowing the name of the string module.
'''indexer(stri ng, target) ->[list of target indexes]

enter in a string and a target and indexer will either return a
list of all targeted indexes if at least one target is found or
indexer will return None if the target is not found in sequence.
Consider returning [] if the target is not found in the sequence. That
would enable callers to "do nothing gracefully":

for posn in indexer(...
# do something

and it doesn't require a gross rewrite ... merely dedent the last line.
>>> indexer('a long long day is long', 'long') [2, 7, 19]
>>> indexer('a long long day is long', 'day') [12]
>>> indexer('a long long day is long', 'short')
None
'''

res = []


Consider evaluating string.count(ta rget) *ONCE*.
if string.count(ta rget) >= 1:
res.append(stri ng.find(target) )

if string.count(ta rget) >= 2:
for item in xrange(string.c ount(target) - 1):
res.append(stri ng.find(target, res[-1] + 1))

return res
if __name__ == '__main__':
print indexer('a long long day is long', 'long') # -> [2, 7, 19]
print indexer('a long long day is long', 'day') # -> [12]
print indexer('a long long day is long', 'short') # -> None


When called with the args('ababababa bababa', 'aba'), it returns [0, 2,
4, 6]. If you *intend* to allow for overlaps, it should return [0, 2, 4,
6, 8, 10, 12]; otherwise it should return [0, 4, 8, 12].

Consider doing something straightforward and understandable, like the
following (tested):

def findallstr(text , target, overlapping=0):
result = []
startpos = 0
if overlapping:
jump = 1
else:
jump = max(1, len(target))
while 1:
newpos = text.find(targe t, startpos)
if newpos == -1:
return result
result.append(n ewpos)
startpos = newpos + jump

HTH,
John
May 11 '06 #11
Hello John,

Thank you very much for your pointers! I decided to redo it and try to
implement your suggestion. I think I did a fair job and because of your
suggestion have a better iterator. Thank you!

def indexer(string, substring, overlap=1):
'''indexer(stri ng, substring, [overlap=1]) -> int

indexer takes a string and searches it to return all substring
indexes. by default indexer is set to overlap all occurrences.
to get the index to whole words only, set the overlap argument
to the length of the substring. The only pitfall to indexer is
it will return the substring whether it stansalone or not.
list(indexer('a babababa', 'aba')) [0, 2, 4, 6]
list(indexer('a babababa', 'aba', len('aba'))) [0, 4]
list(indexer('a babababa', 'xxx')) []
list(indexer('s how chow', 'how'))

[1, 6]
'''

index = string.find(sub string)
if index != -1:
yield index

while index != -1:
index = string.find(sub string, index + overlap)
if index == -1: continue
yield index

if __name__ == '__main__':
print list(indexer('a babababa', 'aba')) # -> [0, 2, 4, 6]
print list(indexer('a babababa', 'aba', len('aba'))) # -> [0, 4]
print list(indexer('a babababa', 'xxx')) # -> []
print list(indexer('s how chow', 'how')) # -> [1, 6]

May 12 '06 #12
I forgot to explain my reason for over shadowing the 'string' built-in
within my iterator. To me, it doesn't matter because the string
identifier is temporary within the function and dies when the function
dies. Also, I personally don't use the string function and prefer
''.join('hi'), etc. Also, at least for me just starting out in Python,
I find 'string' to be as readable as possible :)

what do you think about that?

May 12 '06 #13
On 13/05/2006 1:45 AM, vbgunz wrote:
Hello John,

Thank you very much for your pointers! I decided to redo it and try to
implement your suggestion. I think I did a fair job and because of your
suggestion have a better iterator. Thank you!

def indexer(string, substring, overlap=1):
'''indexer(stri ng, substring, [overlap=1]) -> int

indexer takes a string and searches it to return all substring
indexes. by default indexer is set to overlap all occurrences.
to get the index to whole words only, set the overlap argument
to the length of the substring.
(1) Computing the length should be done inside the function, if
necessary, which (2) avoids the possibility of passing in the wrong
length. (3) "whole words only" does *NOT* mean the same as "substrings
don't overlap".
The only pitfall to indexer is
it will return the substring whether it stansalone or not.
>>> list(indexer('a babababa', 'aba')) [0, 2, 4, 6]
>>> list(indexer('a babababa', 'aba', len('aba'))) [0, 4]
>>> list(indexer('a babababa', 'xxx')) []
>>> list(indexer('s how chow', 'how'))
[1, 6]
'''

index = string.find(sub string)
if index != -1:
yield index

while index != -1:
index = string.find(sub string, index + overlap)
if index == -1: continue
yield index


Quite apart from the fact that you are now using both 'string' *AND*
'index' outside their usual meaning, this is hard to follow. (1) You
*CAN* avoid doing the 'find' twice without losing readibility and
elegance. (2) continue?? Somebody hits you if you use the 'return'
statement or the 'break' statement?

Sigh. I'll try once more. Here is the function I wrote, with the minimal
changes required to make it an iterator, plus changing from 0/1 to
False/True:

def findallstr(text , target, overlapping=Fal se):
startpos = 0
if overlapping:
jump = 1
else:
jump = max(1, len(target))
while True:
newpos = text.find(targe t, startpos)
if newpos == -1:
return
yield newpos
startpos = newpos + jump

if __name__ == '__main__':
print list(indexer('a babababa', 'aba')) # -> [0, 2, 4, 6]
print list(indexer('a babababa', 'aba', len('aba'))) # -> [0, 4]
print list(indexer('a babababa', 'xxx')) # -> []
print list(indexer('s how chow', 'how')) # -> [1, 6]


Get yourself a self-checking testing mechanism, and a more rigorous set
of tests. Ultimately you will want to look at unittest or pytest, but
for a small library of functions, you can whip up your own very quickly.
Here is what I whipped up yesterday:

def indexer2(string , target):
res = []
if string.count(ta rget) >= 1:
res.append(stri ng.find(target) )
if string.count(ta rget) >= 2:
for item in xrange(string.c ount(target) - 1):
res.append(stri ng.find(target, res[-1] + 1))
return res # dedent fixed

if __name__ == '__main__':
tests = [
('a long long day is long', 'long', [2, 7, 19], [2, 7, 19]),
('a long long day is long', 'day', [12], [12]),
('a long long day is long', 'short', [], []),
('ababababababa ba', 'aba', [0, 4, 8, 12], [0, 2, 4, 6, 8, 10, 12]),
('qwerty', '', range(7), range(7)),
('', 'qwerty', [], []),
]
for test in tests:
text, target = test[:2]
results = test[2:]
for olap in range(2):
result = findallstr(text , target, olap)
print (
'FAS', text, target, olap,
result, results[olap], result == results[olap],
)
for test in tests:
text, target = test[:2]
results = test[2:]
result = indexer2(text, target)
print (
'INDXR2', text, target,
result, result == results[0], result == results[1],
)

Make sure your keyboard interrupt is not disabled before you run the
2nd-last test :-)

HTH,
John
May 12 '06 #14
On 13/05/2006 1:55 AM, vbgunz wrote:
I forgot to explain my reason for over shadowing the 'string' built-in
within my iterator. To me, it doesn't matter because the string
identifier is temporary within the function and dies when the function
dies. Also, I personally don't use the string function and prefer
''.join('hi'), etc. Also, at least for me just starting out in Python,
I find 'string' to be as readable as possible :)

what do you think about that?


1. 'string' is not a function, it is a module. I'm glad you don't use
the non-existent 'string function'.

2.
''.join('hi') 'hi'

You prefer to do that instead of what?

Look at the docs on the .join() method carefully, and consider the
following: '-'.join('hello') 'h-e-l-l-o'


3. Back to the shadowing thing:

'string' is admittedly not a good example to pull you up on, as that
module is little used these days. However it is the thin end of the
wedge, and your adding 'index' in round 2 just drove the wedge in a
little further. Once you start using words like 'file' and 'list' as
variable names, your code will be almost as *UN*readable as possible.
It's not just the shadowing possibility, it's the confusion in the
reader's mind between the usual/normal Python meaning of a word and the
meaning that you have attached to it. As you are just starting out in
Python, now is the time to acquire good habits.

Cheers,
John
May 12 '06 #15

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

Similar topics

9
2133
by: kosh | last post by:
I was wondering if there is or there could be some way to pass a generator an optional starting index so that if it supported that slicing could be made more efficient. Right now if you do use a generator and do a or any other kind of slice it reads all the values up to 100 also. I know a lot of generators have to do all previous parts before they can do the next part but it would be a nice optimization that can start at any index to...
108
6390
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would describe if applied to a sequence of length items. It returns a tuple of three integers; respectively these are the /start/ and /stop/ indices and the /step/ or stride length of the slice. Missing or out-of-bounds indices are handled in a manner...
7
1978
by: Mary | last post by:
Hi, I need some assistance with a query. To be honest, I'm not even sure it can be done. I'll try to keep the information limited to only what's relevant to what I have and what I am trying to achieve with this query. I have a table that contains around 100,000 records. For the sake of this discussion, assume just two columns: ID Data
4
2342
by: Deniz Bahar | last post by:
Hello all, Often times programs in C have arrays used as buffers and shared among different sections of code. The need arises to have position indicators to point to different parts of an array (example: point to top of stack). Before I even got K&R2 I used to just define extra pointers to types equal to the element type of the arrays to act as indicators. Now flipping through K&R2, I see they use int variables to act as "offsets." ...
6
3634
by: Anjali | last post by:
Hi, I am handling an array with a hexadecimal index for the first time. What actually does the below means. arr = { '@','£','$','@','@','@','@','@','@','@', 10,'@', 13,'@','@','@', '@','_','@','@','@','@','@','@','@','@','@', 32,'@','@','@','@', ' ','!','"','#','@','%','&', 39,'(',')','*','+',',','-','.','/', '0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?',...
0
1290
by: KK | last post by:
Dear All I have an MDI Application which needs to restore it's previously closed state on subsequent openings. While closing the application, I am storing the positions of all the Mdi child windows to a file.While opening I read the positions from the file,create Mdi Childs and apply positions. My problem is when I'm recreating the Mdi child windows they are created and appear in the Z order initially and later they moved to the applied...
4
1267
by: Anon | last post by:
Hello All! I have a long string that I need to make sense out of. I have the documentation about what info is between which characters, I just need to somehow parse each 94 character string into it's own section and from there be able to identify what info is in positions 8-11 or 12-24 etc... Any thoughts? TIA
3
1280
by: jmDesktop | last post by:
This program: s = 'abcde' i = -1 for i in range (-1, -len(s), -1): print s, i gives abcd -1
6
3934
by: Henry J. | last post by:
I have a composite index on two columns in a table. However, the index is not used in a query that restricts the 2nd column to a constant. If both columns are linked with columns in other join tables, the index will be used. To illustrate it with an example, I have a query like this: select s.ticker, p.quantity from stock s, positions p where s.type_id = 4
0
8611
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
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
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,...
1
6531
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
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
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
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.