0

If I have for instance, the list: ['a', 'b', 'c', 'd', 'e']

and I want each letter by index to be equal to the ints by index in this list:

[1, 1, 2, 1, 1]

so that 'a' == 1, 'b' == 1, 'c' == 2, etc.

Is there a simple way of doing this that I'm missing?

Vlad
  • 19
  • 2

2 Answers2

1

You can use a dictionary to hold the numeric values as values of the keys 'a' - 'e'.

You can set this up manually like this:

d = {'a': 1, 'b': 1, 'c': 2, 'd': 1, 'e': 1}

...or if you have the two lists already made...

d = dict()
for i, letter in enumerate(letterList):
    d[letter] = numberList[i]
m_callens
  • 6,100
  • 8
  • 32
  • 54
0

The simplest way to do this would be to use a dictionary instead of a list. As a few others have suggested.

But if you absolutely need to use a list, I imagine you would want to just compare the values of the index after you find them.

alpha = ['a','b','c','d','e']
numeric = [1,1,2,1,1]
alpha.index('a') == numeric.index('1')
alpha.index('b') == numeric.index('1',1)

Note: The index method finds the first occurrence of the parameter so you need to pass it a starting point for duplicate items.