473,396 Members | 1,780 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.

Addition to slice syntax (for strings)

Hi,

I'd like to suggest an addition to the slice syntax. How about allowing
strings as arguments to slices on strings, and interpret them as the
index of the substring found within the string being sliced. I'm sure I
didnt explain that too well, so here is an example..
a= "Find the string within the (parenthesis)." #equivalent of a[ a.find('(') : a.find(')', a.find('(') + 1 ) + 1 ] a['(':')'] '(parenthesis)'

#Strings with a negative step would search for the substring from the
end of the string a= 'some/folder/file.txt';
a['.',-1,-1]

'.txt'
I have actually implemented something similar in a C++ string class, so
its not completely crazy - but I would be interested in what more
experienced Python developers think of it.
Thanks,

Will McGugan
Jul 18 '05 #1
2 1435
>>>>> "Will" == Will McGugan <ne**@NOwillmcguganSPAM.com> writes:

Will> I'd like to suggest an addition to the slice syntax. How about
Will> allowing strings as arguments to slices on strings, and interpret
Will> them as the index of the substring found within the string being
Will> sliced. I'm sure I didnt explain that too well, so here is an
Will> example..

How would you interpret
s = "my dog has fleas"
s['x':'z'] # both strings nonexistent
s['z':'f'] # first string nonexistent
s['f':'z'] # second string nonexistent
s['g':'d'] # second appears earlier than first


Returning the empty string in all cases is plausible, but other
possibilities make sense at some level:

s['x':'z'] => ""
s['x':'z'] => "my dog has fleas"
s['z':'f'] => "my dog has f"
s['z':'f'] => "my dog has "
s['f':'z'] => "leas"
s['f':'z'] => "fleas"
s['g':'d'] => ""
s['g':'d'] => "god"

Two other quibbles about your example:

* why not use s.rfind("...") instead of s.find("...") for the second
index?

* why add 1 to the offset of the second index?

In short, there is enough uncertainty in what the "right" thing to do is, I
suspect you'd be better off just subclassing strings when you need this sort
of functionality in your apps.

Skip
Jul 18 '05 #2
Will McGugan wrote:
I have actually implemented something similar in a C++ string class, so
its not completely crazy - but I would be interested in what more
experienced Python developers think of it.


I actually find that it is a good idea for a class, but the devil is in
the details, and I think that it might be too limited in the long run.

Sometimes you want to use rfind() instead of find(). Other times you
want to use a regular expression as the index etc.

It is probably a good idea to leave it out of the language. Especially
as it is pretty easy to roll your own.

A better idea is to add the recipe to the Python Cookbook

http://aspn.activestate.com/ASPN/Python/Cookbook/

regards Max M
Jul 18 '05 #3

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

Similar topics

15
by: Roberto A. F. De Almeida | last post by:
I found that when using negative indices, the slice object passed to __getitem__ depends on the number of slices. An example to clarify: class a: def __getitem__(self, index): return index ...
4
by: Jens Lippmann | last post by:
Hi all! I'm new to Python and just tried to assign values to a portion of a string, but I don't get it. My code is: bits = '\3ff' * bmi.bmiHeader.biSizeImage ofs = 0x1E2C0 for i in range(0,...
108
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...
40
by: Ron Adam | last post by:
After considering several alternatives and trying out a few ideas with a modified list object Bengt Richter posted, (Thank You), I think I've found a way to make slice operation (especially far end...
2
by: jbperez808 | last post by:
Reading: http://docs.python.org/ref/slicings.html it would seem to indicate that the ff will work: L= however, you get:
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
23
by: Antoon Pardon | last post by:
Now slices are objects in python, I was wondering if slice notation will be usable outside subscribtion in the future. Will it ever be possible to write things like: a = 4:9 for key, value in...
0
by: Travis Oliphant | last post by:
This post is to gather feedback from the wider community on PEP 357. It is nearing the acceptance stage and has previously been discussed on python-dev. This is a chance for the wider Python...
7
by: Alexandre Guimond | last post by:
Hi all, i'm trying to deepcopy a slice object but i get the following error. Does anyone know a workaround? ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on Python 2.4.3 (#69,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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,...
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
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...

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.