Reputation: 3
so say i have list1 and list2.
list1 = [n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,star,pound]
list2 = [1,2,3,4,5,6,7,8,9,0,star,pound]
How to i write a function that when an element of list one is called, the corresponding element in list2 is called?
this may have been answered in another post but I have not been able to find and / or understand how to do this.
---Edit---
John Coleman's comment answered my question, but to help others reading this in the future I will clarify a few things.
"call" might have been the wrong term to use, I'm definitely a novice at programming. Here is how i used the code with the help from this thread.
n1 = 5
n2 = 6
n3 = 13
n4 = 19
n5 = 26
n6 = 12
n7 = 16
n8 = 20
n9 = 12
n0 = 4
star = 17
pound = 27
inputs = [n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,star,pound]
numbers = [1,2,3,4,5,6,7,8,9,0,star,pound]
shows = dict(zip(inputs,numbers))
count = 1
def loop2():
for input in inputs:
if GPIO.input(input) == False:
print '%s' % shows[input]
Upvotes: 0
Views: 170
Reputation: 51998
You can use .index()
to look up where an element of one of the two lists occurs, and then feed that index to the other list:
>>> list1 = ['n1','n2','n3','n4','n5','n6','n7','n8','n9','n0','star','pound']
>>> list2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'star', 'pound']
>>> list2[list1.index('n5')]
'5'
Alternatively, you can create a dictionary linking the two lists:
>>> d = dict(zip(list1,list2))
>>> d['n5']
'5'
Upvotes: 1
Reputation: 598
Not much can be answered if you do not clarify what do you mean by "corresponding element in list2 is called", you mean function call or something?
Anyway, no matter what the situation, you will need to define a relation between the two items in the lists you want. In the example above, my best guess is that list1
's n1
-> list2
's 1
, or by index corresponding.
You can achieve this by define a dictionary, something like
relate_dict = {"n{}".format(k):k for k in list2}
so every relate_dict[key1]
, with key1
in list1
will return corresponding in list2
, i.e relate_dict['n1'] => '1'
In the case of index, just use zip
Upvotes: 0