Python Forum
"my_value".index() inquiry...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"my_value".index() inquiry...
#1
So I am learning this .index() thingy and know how to use it for a single character in a string, but I am running into errors trying to attempt to index multiple characters that match. For instance, I am running this code:
# test code (alabaster, can I index all three a's?)
"alabaster".index("a")
It outputs 0, for the first a in the string. But what about 2 and 4? I can run:
my_value = alabaster
my_value[0]
my_value[2]
my_value[4]
...to pull the a's myself, but can I simply call the string of all a's up front?

I have tried all types of coding setups and failed; and searching the web leads me nowhere. Can anyone help me understand this and the limitations (if any) Python has in this scenario?

Synopsis: I am trying to pull all "a" locations for output >>> 0, 2, 4 using .string(). Can I pull all three? Or am I limited to just the first "a" in a string?

Stuck in a loop of my own errors in learning,

-Rod
Reply
#2
The index method (or thingy), takes a second parameter which indicates where to start searching. (It also takes a third parameter for where to stop searching.) So you start from one more than the previous index.

Output:
>>> word = 'alabaster' >>> word.index('a') 0 >>> word.index('a', 1) 2 >>> word.index('a', 3) 4 >>> word.index('a', 5) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
So I must use multiple lines? I can't use one index() command to pull all three. Okie-dokie.

Thank you @ichabod801, I truly appreciate your time and assist on this.

-Rod
Reply
#4
(Jun-10-2017, 07:45 PM)RodNintendeaux Wrote: So I must use multiple lines? I can't use one index() command to pull all three. Okie-dokie.
You have iterate over string and check for value a,if find it print index.
enumerate() can be used.
>>> my_value = 'alabaster'
>>> for index,item in enumerate(my_value):
...     if item == 'a':
...         print(index)
...         
0
2
4
Or regex.
>>> import re
>>> my_value = 'alabaster'
>>> [(m.start()) for m in re.finditer(r'a', my_value)]
[0, 2, 4]
Reply
#5
You can't use one index command, but you could put it all into a loop, and then into a function, which you could then use in one line.

def indexes(text, sub_text):
    next = text.find(sub_text)
    indexes = []
    while next != -1:
        indexes.append(next)
        next = text.find(sub_text, next + 1)
    return indexes
Output:
>>>indexes('alabaster', 'a') [0, 2, 4]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(Jun-10-2017, 08:17 PM)snippsat Wrote:
(Jun-10-2017, 07:45 PM)RodNintendeaux Wrote: So I must use multiple lines? I can't use one index() command to pull all three. Okie-dokie.
You have iterate over string and check for value a,if find it print index.
enumerate() can be used.
>>> my_value = 'alabaster'
>>> for index,item in enumerate(my_value):
...     if item == 'a':
...         print(index)
...         
0
2
4
Or regex.
>>> import re
>>> my_value = 'alabaster'
>>> [(m.start()) for m in re.finditer(r'a', my_value)]
[0, 2, 4]

(Jun-10-2017, 08:18 PM)ichabod801 Wrote: You can't use one index command, but you could put it all into a loop, and then into a function, which you could then use in one line.

def indexes(text, sub_text):
    next = text.find(sub_text)
    indexes = []
    while next != -1:
        indexes.append(next)
        next = text.find(sub_text, next + 1)
    return indexes
Output:
>>>indexes('alabaster', 'a') [0, 2, 4]

I haven't seen enumerate, yet. Interesting. I see how it works, but the regex thing, uh... I will get there eventually I suppose. That with the import re went so far over my head.

Heck, ichabod801 went over my head, too with that != -1. I get the logic, but not how it applies to the 'a' in your code. I have so much to learn.

Thanks guys, seriously. Seeing you both do something almost identical with different code simply amazes me. I can't wait to be that good. Thank you so much for giving me such awesome examples. I am very grateful.

Chugging through the Python books,

-Rod
Reply
#7
(Jun-10-2017, 08:32 PM)RodNintendeaux Wrote: Heck, ichabod801 went over my head, too with that != -1. I get the logic, but not how it applies to the 'a' in your code. I have so much to learn.

The find method is like the index method, except that when it can't find the sub-string it returns -1, where the index method raises an exception. So the while next != -1: will only loop if you actually found the sub-string. It's basically: if you found something, add it to the list and look for something else; otherwise return the list.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  bluetooth device inquiry anne 0 2,339 Aug-01-2020, 12:24 AM
Last Post: anne

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020