472,794 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Strings in Python

Playing a little more with strings, I found out that string.find
function provides the position of
the first occurance of the substring in the string.
Is there a way how to find out all substring's position ?
To explain more,
let's suppose

mystring='12341'
import string
>>string.find(mystring ,'1')
0

But I need to find the possition the other '1' in mystring too.
Is it possible?
Or must I use regex?
Thanks for help
L

Feb 8 '07 #1
6 1703
Johny wrote:
Playing a little more with strings, I found out that string.find
function provides the position of
the first occurance of the substring in the string.
Is there a way how to find out all substring's position ?
To explain more,
let's suppose

mystring='12341'
import string

>>>string.find(mystring ,'1')
0

But I need to find the possition the other '1' in mystring too.
Is it possible?
Or must I use regex?
Thanks for help
L

You could use a regular expression. The re module has s function
"findall" that does what you want.

Also, if you read the documentation for strings find method, you'll find:

1 S.find(sub [,start [,end]]) -int
2
3 Return the lowest index in S where substring sub is found,
4 such that sub is contained within s[start,end]. Optional
5 arguments start and end are interpreted as in slice notation.
6
7 Return -1 on failure.

So put your find in a loop, starting the search one past the previously
found occurrence.

i = string.find(mystring, i+1)

Gary Herron
Feb 8 '07 #2
On 8 Feb 2007 08:28:25 -0800, Johny <py****@hope.czwrote:
Playing a little more with strings, I found out that string.find
function provides the position of
the first occurance of the substring in the string.
Is there a way how to find out all substring's position ?
To explain more,
let's suppose

mystring='12341'
import string
>string.find(mystring ,'1')
0

But I need to find the possition the other '1' in mystring too.
Is it possible?
Or must I use regex?
Thanks for help
L

--
http://mail.python.org/mailman/listinfo/python-list
Loop it -- once you know the index of the first character, add the
third argument to string.find(), which tells it the position at which
to start (the last find + 1).
Feb 8 '07 #3
On 2/8/07, Gary Herron <gh*****@islandtraining.comwrote:
Johny wrote:
Playing a little more with strings, I found out that string.find
function provides the position of
the first occurance of the substring in the string.
Is there a way how to find out all substring's position ?
To explain more,
let's suppose

mystring='12341'
import string

>>string.find(mystring ,'1')
0

But I need to find the possition the other '1' in mystring too.
Is it possible?
Or must I use regex?
Thanks for help
L
You could use a regular expression. The re module has s function
"findall" that does what you want.

Also, if you read the documentation for strings find method, you'll find:

1 S.find(sub [,start [,end]]) -int
2
3 Return the lowest index in S where substring sub is found,
4 such that sub is contained within s[start,end]. Optional
5 arguments start and end are interpreted as in slice notation.
6
7 Return -1 on failure.

So put your find in a loop, starting the search one past the previously
found occurrence.

i = string.find(mystring, i+1)

Gary Herron
--
http://mail.python.org/mailman/listinfo/python-list
Speaking of regex examples, that's basically what I did in the script
below which James Kim and I were collaborating on yesterday and this
morning, as a result of his thread.

This matches not only a string, but a regex, then loops through each
match to do something to it. I hope this helps. I submitted this to
the list for recommendations on how to make it more Pythonic, but at
least it works.

Here are the most important, stripped down pieces:

#! /usr/bin/python

import re

#match a date in this format: 05/MAR/2006
regex = re.compile(r",\d{2}/[A-Z]{3}/\d{4},")

for line in infile:

matches = regex.findall(line)
for someDate in matches:

newDate = #do something here
line = line.replace(someDate, newDate)
Here is the full script:

#! /usr/bin/python

import sys
import re

month ={'JAN':1,'FEB':2,'MAR':3,'APR':4,'MAY':5,'JUN':6, 'JUL':7,'AUG':8,'SEP':9,'OCT':10,'NOV':11,'DEC':12 }
infile=file('TVA-0316','r')
outfile=file('tmp.out','w')

def formatDatePart(x):
"take a number and transform it into a two-character string,
zero padded"
x = str(x)
while len(x) < 2:
x = "0" + x
return x

regex = re.compile(r",\d{2}/[A-Z]{3}/\d{4},")

for line in infile:
matches = regex.findall(line)
for someDate in matches:

dayNum = formatDatePart(someDate[1:3])
monthNum = formatDatePart(month[someDate[4:7]])
yearNum = formatDatePart(someDate[8:12])

newDate = ",%s-%s-%s," % (yearNum,monthNum,dayNum)
line = line.replace(someDate, newDate)

outfile.writelines(line)

infile.close
outfile.close
Feb 8 '07 #4
On Feb 8, 8:28 am, "Johny" <pyt...@hope.czwrote:
Playing a little more with strings, I found out that string.find
function provides the position of
the first occurance of the substring in the string.
Is there a way how to find out all substring's position ?
To explain more,
let's suppose

mystring='12341'
import string
>string.find(mystring ,'1')

0

But I need to find the possition the other '1' in mystring too.
Is it possible?
Or must I use regex?

In this case, you can use:

mystring = '12341'
indices = [ _ for _ in range(len(mystring)) if mystring[_] == '1' ]
print indices

--
Hope this helps,
Steven

Feb 8 '07 #5
On Feb 8, 6:32 pm, attn.steven....@gmail.com wrote:
On Feb 8, 8:28 am, "Johny" <pyt...@hope.czwrote:
Playing a little more with strings, I found out that string.find
function provides the position of
the first occurance of the substring in the string.
Is there a way how to find out all substring's position ?
To explain more,
let's suppose
mystring='12341'
import string
>>string.find(mystring ,'1')
0
But I need to find the possition the other '1' in mystring too.
Is it possible?
Or must I use regex?

In this case, you can use:

mystring = '12341'
indices = [ _ for _ in range(len(mystring)) if mystring[_] == '1' ]
print indices
Or:

mystring = '12341'
indices = [ i for i, c in enumerate(mystring) if c == '1' ]
print indices

Feb 8 '07 #6
Thanks ALL for help and ideas
L

Feb 9 '07 #7

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

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
5
by: Maurice LING | last post by:
I'm trying to toy around with PLY (python lex-yacc) by David Beazley from University of Chicago and realized that the lex module uses python raw strings. What are python raw strings and how are...
11
by: bearophile | last post by:
Hello, here are a four more questions (or suggestions) for the language (probably people have already discussed some of/all such things: I've seen the contracts for Python:...
26
by: William Park | last post by:
How do you compare 2 strings, and determine how much they are "close" to each other? Eg. aqwerty qwertyb are similar to each other, except for first/last char. But, how do I quantify that? ...
2
by: Neil Schemenauer | last post by:
python-dev@python.org.] The PEP has been rewritten based on a suggestion by Guido to change str() rather than adding a new built-in function. Based on my testing, I believe the idea is...
14
by: Dennis Benzinger | last post by:
Hi! The following program in an UTF-8 encoded file: # -*- coding: UTF-8 -*- FIELDS = ("Fächer", ) FROZEN_FIELDS = frozenset(FIELDS) FIELDS_SET = set(FIELDS)
5
by: BBands | last post by:
I'd like to see if a string exists, even approximately, in another. For example if "black" exists in "blakbird" or if "beatles" exists in "beatlemania". The application is to look though a long...
2
by: bearophileHUGS | last post by:
Helmut Jarausch: Asking in comp.compression is a good starting point. My suggestions (sorry if they look a bit unsorted): it depends on what language you want to use, how much you want to...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.