0

This question is very similar to this question, Question, except for the fact that I need an answer for Python.

I'll explain the question anyway. If I have an array of characters, for example ["a", "b", "a", "c"], then it needs to return an array that includes the indexes of all the appearances of a certain character.

This means that if I input ['a', 'b', 'a', 'c', 'c', 'a'], and I need to find all appearances of 'a', then it would output [0, 2, 5].

I also need the program to be pretty fast, as I need to mass generate my final product, so the whole program needs to be quick. I will probably be running this piece of code millions of times in my code so the quicker it is the better, but I am accepting anything right now.

Right now my code does not work since it simply output [0, 0, 0] (I am running through a for loop so it isn't fast either)

Could someone please help? Thanks!

5
  • Use Numpy if you need speed fir these functions. They are built in. Commented Feb 16, 2020 at 22:04
  • Post your code. Commented Feb 16, 2020 at 22:04
  • Why is it a list of characters instead of a string? String might allow faster solutions. Commented Feb 16, 2020 at 22:19
  • @ethan: would would this be? I am already using a lot of numpy in my code, so what would this build-in function be? Commented Feb 16, 2020 at 22:33
  • @Bob Robert. If you create a masked array you can use np.where(myMaskedArray==‘a’) and it will return an array of indexes. Commented Feb 16, 2020 at 23:05

3 Answers 3

4

A simple option, not optimised for speed:

list_of_characters = ['a', 'b', 'a', 'c', 'c', 'a']
character = 'a'
index_list = [i for i,v in enumerate(list_of_characters) if v == character]

Edit: If you're doing this calculation a lot and the list doesn't change between each run, you should pre-compute the solutions and store them in an easily accessible format.

Sign up to request clarification or add additional context in comments.

1 Comment

Never use list as a variable name, as it clobbers the builtin list command, otherwise good solution to find the index locations of a single character.
2

You can use a defaultdict to default the index locations of each character to a list, then just enumerate over your list and append the index locations. This allows you to later index the location of any character in the list.

from collections import defaultdict

my_list = ['a', 'b', 'a', 'c', 'c', 'a']

dd = defaultdict(list)
for n, c in enumerate(my_list):
    dd[c].append(n)

>>> dd['a']
[0, 2, 5]

>>> dict(dd)
{'a': [0, 2, 5], 'b': [1], 'c': [3, 4]}

1 Comment

This would be nice, except for the fact that it would use unnecessary memory and time (I know, very slight, but every millisecond here makes a difference).
1

Just use the np.where function.

>>> l=np.array(['a','b','a','c','c','a'])
>>> np.where(l=='a')
(array([0, 2, 5]),)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.