Extracting Value from List
How to extract only the values from a list in python?
Best way of extracting elements from a list and assigning them to a variable?
arrays - Python: Extracting values from list of values - Stack Overflow
What is the fastest way to get one element from a Python list?
Does slicing a Python list change the original list?
How do I extract elements from several lists at the same time?
Videos
I'm getting this input:
[{'symbol': 'aapl'}, {'symbol': 'googl'}]
And want to make a list with aapl, google
How do I do this? I've tried multiple ways but my brain don't seem to work today
Hello! I have a list of numpy arrays and I want to create a separate variable for each of them so that I can combine them.
This is what I have. The mask_array variable is what contains the list of arrays:
for array in mask_arrays:
mask_one = (mask_arrays[0])
mask_two = (mask_arrays[1])
mask_three = (mask_arrays[2])
mask_four = (mask_arrays[3])
combined_arys = mask_one + mask_two + mask_three + mask_fourBut I don't always know the amount of arrays that will be in the list. Sometimes it's 4 like it is in the example, but sometimes it may be two or 0 (5 is the max)
Is there anyway I can extract the numpy arrays from the list and assign them to mask_one, mask_two, etc in a way that best follows python conventions? I looked this up on stack overflow, and it seems like it isn't good practice to create variables that can vary in number like this and that I should use a dictionary instead? Is there a better method of combining the arrays?
Try using a list of lists, like this:
lsts = [[" "],
[".", ",", "?"],
["a", "b", "c"],
["d", "e", "f"],
["g", "h", "i"],
["j", "k", "l"],
["m", "n", "o"],
["p", "q", "r", "s"],
["t", "u", "v"],
["w", "x", "y", "z"]]
Now you can access each sublist by its index:
lsts[1]
=> [".", ",", "?"]
And each element by both of its indexes:
lsts[1][2]
=> "?"
Now it's easy to extract values from a list of key presses, like those shown in the question and then join them:
keypresses = [[6, 3], [0, 1], [5, 2]]
''.join(lsts[i][j-1] for i, j in keypresses)
=> "n k"
Notice that the indexes start from zero, so I had to subtract one unit from the sample key presses given in the question.
This should do what your looking for.
def key_pressed(key, character):
"""
:param key: index of keyboard button
:param character: desired character represented by key
:return: requested character
"""
lookup = [[" "],
[".", ",", "?"],
["a", "b", "c"],
["d", "e", "f"],
["g", "h", "i"],
["j", "k", "l"],
["m", "n", "o"],
["p", "q", "r", "s"],
["t", "u", "v"],
["w", "x", "y", "z"]]
return lookup[key][character]
print(key_pressed(6, 2))
print(key_pressed(1, 2))
print(key_pressed(5, 1))
Output:
o
?
k