0

I am trying to write a function which consumes a string and a character and produces a list of indices for all occurrences of that character in that string.

So far this is what I have, but it always gives me [].

def list_of_indices(s,char):

    string_lowercase = s.lower()
    sorted_string = "".join(sorted(string_lowercase))
    char_list = list(sorted_string)

    for x in char_list:
          a = []
          if x == char:

             a.append(char_list.index(x))
          return a

I don't understand why this does not yield the answer. And it has to be a list of non-empty length.

Anyone aware of how to get the indices for all occurrences?

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • Related: [How to find all occurrences of an element in a list?](http://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list) – kojiro Mar 25 '14 at 00:17

3 Answers3

1

You're returning on the first iteration of your for-loop. Make sure the return statement is outside the scope of the loop.

Also, be sure to put a = [] before the for-loop. Otherwise, you're effectively resetting the list on each iteration of loop.

There is also a problem with char_list.index(x). This will always return the index of the first occurrence of x, which isn't what you want. You should keep track of an index as you are looping (e.g. with enumerate()).

And I'm not sure what you were trying to do with the sort; looping through the original string should be sufficient.

Lastly, note that you can loop over a string directly; you don't need to convert it to a list (i.e. char_list is unnecessary).


Note that your task can be accomplished with a simple list comprehension:

>>> s = 'abcaba'
>>> char = 'a'
>>> 
>>> [i for i,c in enumerate(s) if c == char]  # <--
[0, 3, 5]
arshajii
  • 127,459
  • 24
  • 238
  • 287
0

You could implement it using a quick list comprehension.

def list_of_indicies(s, char):
    return [i for i, c in enumerate(s) if c == char]

or by using a for loop instead:

def list_of_indicies(s, char):
    results = list()

    for i, c in enumerate(s):
        if c == char:
            results.append(i)

    return results
mdadm
  • 1,333
  • 1
  • 12
  • 9
0

You are returning a on the first loop of your for loop iteration.

Change for loop to this for starters:

def list_of_indices(s,char):
    string_lowercase = s.lower()
    a = []
    i = 0
    for x in string_lowercase:
        if x == char:
            a.append(i)
        i+=1
    return a
Aidan
  • 757
  • 3
  • 13
  • This only gives a list of the same index of the character the number of times it appears in the list. Also, is there a way to get the answer while staying away from enumerate() or list comprehension, strictly loops? – user3457606 Mar 25 '14 at 00:29
  • Sorry, maybe I missunderstood the question, can you please provide an example of an input and an output of your desired function ? This would make it much easier. – Aidan Mar 25 '14 at 00:31
  • so lets say if i have list_of_indices("abcc","c") => [2,3] – user3457606 Mar 25 '14 at 00:35