1

I have a set of initial variables from a->i, a full list of variables, a separate list of integer values and a set of lists made from subsets of the initial variables:

a=0
b=0
...
i=0

exampleSubList_1 = [c, e, g]
fullList = [a, b, c, d, e, f, g, h, i] #The order of the list is important
otherList = [2, 4, 6, 8, 10, 12, 14, 16, 18] #The order of the list is important

I want my program to read an input exampleList_X and find its corresponding index entry in fullList, and use that index number to output the corresponding value in otherList. For example...

exampleList_1[0]
#Which is c
#It should find index 2 in fullList
#Entry 2 in otherList is the value 6.
#So it should output
6

Is this possible? I would be willing to use tuples/dictionary is required.

In the interest of clarity, this is for a Raspberry Pi noughts and crosses game project using LEDs. c, e and g correspond to the win condition of the diagonal from top right to bottom left, and otherList corresponds to the pin on the Raspberry Pi which sends out a current to light up the LED.

Pingk
  • 532
  • 2
  • 7
  • 17
  • Have you considered ``from collections import OrderedDict``? `` – Dietrich Mar 21 '14 at 21:29
  • @GWW, I've experimented with various bits and pieces, but I keep getting stuck when trying to compare fullList to otherList. – Pingk Mar 21 '14 at 21:37
  • @Dietrich, I've tried Dict, but I've not heard of OrderedDict. I'll have a look and come back to you – Pingk Mar 21 '14 at 21:38
  • OP: currently `exampleSubList_1` is `[0,0,0]`, so `fullList.index(exampleSubList_1[0]) == 0` since all your variables `a` through `i` are `0`, so there's no telling them apart. Do you mean `exampleSubList_1 = ['c','e','g']`, `fullList = ['a','b','c', ... , 'i']` ? – Adam Smith Mar 21 '14 at 21:57
  • @Dietrich, I've had a look at the documentation, but it's not particularly clear. Do I initiate ordereddict in the same way as dict, and do the same commands for keys/values still work? – Pingk Mar 21 '14 at 22:04
  • @AdamSmith, No. The variables a->i are likely to change their value by the time the program gets around to running this bit of code, exampleSubList will be either [1,1,1] or [2,2,2], but the principle is the same. I think if I can tell them apart using variable names, I can achieve what I described. – Pingk Mar 21 '14 at 22:08
  • Regarding the ``OrderedDict``, it works the same ways as a normal ``dict`` (see the answer form Dan H), but your original lists are embedded in ``d1.keys()`` and ``d1.values()``, you can use those lists for other things as well. – Dietrich Mar 22 '14 at 10:07

5 Answers5

1

List comprehension:

results = [otherList[fullList.index(c)] for c in exampleSubList_1]

and results will return:

[6, 10, 14]

Or a simple for loop:

for c in exampleSubList_1:
    print otherList[fullList.index(c)]

Should print

6
10
14
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
1
>>> symbol_pos_map = {v:k for k,v in enumerate(fullList)}
>>> otherList[symbol_pos_map[exampleSubList_1[0]]]
6

Don't use list.index, because it does a linear search everytime, first map fullList to a dictionary at linear cost, then subsequent lookups are a constant time.

Yanshuai Cao
  • 1,257
  • 10
  • 14
  • This is a useful optimization for anyone looking for what the OP seemed to be asking. (OP said in comments the expected output was the actual variable names. Your answer is still good against a valid reading of the question.) – Two-Bit Alchemist Mar 22 '14 at 01:15
1

You really should consider using a dictionary.

Consider:

l1 = ['c', 'e', 'g']
l2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
l3 = [2, 4, 6, 8, 10, 12, 14, 16, 18]

results = [l3[l2.index(c)] for c in l1]
print "results 1:", results

d1 = {'a': 2, 'b': 4, 'c': 6, 'd':8, 'e': 10, 'f': 12, 'g': 14, 'h': 16, 'i': 18}
results = [d1[c] for c in l1]
print "results 2:", results

Those give you the same results. Let the dictionary do what it was designed to do: lookup a key, and return a value.

Note that the key value can be almost anything: a number, a letter, a full string, a tuple of values...

If you already have your two "lookup lists" (l2 and l3 in my example), then you can use the dict() and zip() functions to create the dictionary for you:

d2 = dict(zip(l2, l3))  # this creates a dictionary identical to d1
results = [d2[c] for c in l1]
print "results 3:", results
Dan H
  • 14,044
  • 6
  • 39
  • 32
  • I've tried, but Dict doesn't preserve the order in which you input the variables: http://dft.ba/-N_lists2 – Pingk Mar 21 '14 at 22:16
  • @pingk Why do you need the order preserved? In your post, you seem to use the order to keep two lists "organized" so that the values at same indexes are related; but with the dict, you just use the dict itself to relate the two data sets. My proposal produces the results you say you need without keeping the list ordered! – Dan H Apr 02 '14 at 18:58
0

After reading around dictionaries, I've found that using OrderedDict works perfectly for what I need, see Python select ith element in OrderedDict.

@Dietrich thanks for the idea.

Community
  • 1
  • 1
Pingk
  • 532
  • 2
  • 7
  • 17
-1

It's possible using list.index(x) method and of course you can access any element in the list just writing list[index]

  • Could you give me an example of how it would work in practise? I've found if I use a dictionary, I can get to the index in fullList, but then I get stuck trying to translate the information to otherList. – Pingk Mar 21 '14 at 21:40