0

Basically, I have a list that contains all the possibles values of the second list. For example:

First list (Possible values):

list1 = ['cat','dog','pig'] 

Second list:

list2 = ['dog','cat','cat','cat','dog','pig','cat','pig']

I want to compare those lists and substitute all the strings in the second list to the index of the first one.

So expect something like this:

list2 = [1,0,0,0,1,2,0,2]

I've tried it in several different ways. The first one, although it worked, was not an intelligent method. Since if the first list had a huge variety of possible values, this strategy would not be functional to code.

That was the first solution:

list3 = []
for i in list2:
    if i == 'cat':
        i = 0
        list3.append(i)
    elif i == 'dog':
        i = 1
        list3.append(i)
    elif i == 'pig':
        i = 2
        list3.append(i)
    list2 = list3


print(list2)

output

[1, 0, 0, 0, 1, 2, 0, 2]

But I want a solution that works in a huge variety of possible values without having to code each test.

So I tried this (and other failed attempts), but it isn't working

for i in list2:
    for j in list1:
        if i == j:
            i = list1.index(j)
martineau
  • 119,623
  • 25
  • 170
  • 301

4 Answers4

1

The problem with your code is that you are simply replacing i on each iteration. You want to create a list and append the result from list1.index(j) to it on each iteration:

 l = []
for i in list2:
    for j in list1:
        if i == j:
            l.append(list1.index(j))

Note that this can be simplified with a list comprehension:

[list1.index(i) for i in list2]
# [1, 0, 0, 0, 1, 2, 0, 2]

Note that for a lower complexity solution, you can create a dictionary mapping strings to index, and simply create a list by looking up with the strings in list2, as in @blhshing's answer.


Some reads you might find useful:

yatu
  • 86,083
  • 12
  • 84
  • 139
  • Note that using `list.index` in a loop costs *O(n^2)* in time complexity. – blhsing Mar 28 '19 at 14:37
  • Yes just adapting from OP's code. For a better performing solution looking up on a dict as in your solution is much better @blhsing – yatu Mar 28 '19 at 14:39
0

The i you get by iterating the list does not reflect changes back into the list. Hence no change.


Create a dictionary that mapps animal to index position.

Use a list comprehension to create a new list by replacing animals of list2 its lookup-index:

list1 = ['cat','dog','pig']

lookup = {animal:index for index,animal in enumerate(list1)}

list2 = ['dog','cat','cat','cat','dog','pig','cat','pig']

result = [lookup.get(what) for what in list2]

print(result) # [1, 0, 0, 0, 1, 2, 0, 2]

Doku:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

You can use a dict comprehension to create a mapping dict that maps keys to indices using the enumerate function, and then map items in list2 to the values of the mapping dict:

mapping = {k: i for i, k in enumerate(list1)}
list(map(mapping.get, list2))

This returns:

[1, 0, 0, 0, 1, 2, 0, 2]
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

You can use only one for loop and one if-statement that is easy to understand.

list1 = ['cat','dog','pig']
list2 = ['dog','cat','cat','cat','dog','pig','cat','pig']

for i,item in enumerate(list2):
    if item in list1:
        list2[i] = list1.index(item)       

# list2 = [1, 0, 0, 0, 1, 2, 0, 2]
YusufUMS
  • 1,506
  • 1
  • 12
  • 24