0

Here I have a list:

some_list = [a','r','p','i','l','a','z','a','r','l','i','i','l','z','p']

I want some function to index each of the characters in the list with an unique index.

So the code should be something like:

for char in some_list:
    char_index = some_list.magic_index(char)
    print(char_index)

magic_index should be a function that returns a number from 0 to 14 incrementally for each character.

The output should be something like:

0
1
2
3
4
4
5
6
7
8
9
10
11
12
13
14

I know this isn't really indexing each character, but I just want some function to return a value from 0 to 14 for each character, so that each character has their own unique number from 0 to 14.

I know this is kind of a dumb question, it is some how just very hard for me. If someone know how to solve this, please give me some help. Thank you!

Tian
  • 31
  • 5

1 Answers1

0

Use enumerate and build a map of characters to indices:

>>> magic_index = {c: i for i, c in enumerate(some_list)}.get
>>> magic_index('a')
7
Samwise
  • 68,105
  • 3
  • 30
  • 44