A list comprehension would work just fine:
[o.my_attr for o in my_list]
But there is a combination of built-in functions, since you ask :-)
from operator import attrgetter
map(attrgetter('my_attr'), my_list)
Answer from Jarret Hardie on Stack OverflowA list comprehension would work just fine:
[o.my_attr for o in my_list]
But there is a combination of built-in functions, since you ask :-)
from operator import attrgetter
map(attrgetter('my_attr'), my_list)
are you looking for something like this?
[o.specific_attr for o in objects]
attrs = [o.attr for o in objs] was the right code for making a list like the one you describe. Don't try to subclass list for this. Is there something you did not like about that snippet?
You can also write:
attr=(o.attr for o in objsm)
This way you get a generator that conserves memory. For more benefits look at Generator Expressions.
python - Extract elements from list of lists - Stack Overflow
How to extract data from a list in PYTHON - Stack Overflow
Function for extracting data from list
How extract Values from List with Python? - Stack Overflow
Does slicing a Python list change the original list?
What is the fastest way to get one element from a Python list?
How do I extract elements from several lists at the same time?
A list comprehension will cover this quite nicely.
data = [
["Test_1",
{"name": "Test_level_1", "value": 10},
{"name": "Test_level_1 again", "value": 15}],
["Test_2",
{"name": "Test_level_2", "value": 1},
{"name": "Test_level_2 again", "value": 5}]
]
desired_data = [
item['name']
for sublist in data
for item in sublist
if isinstance(item, dict)
]
Result:
['Test_level_1', 'Test_level_1 again', 'Test_level_2', 'Test_level_2 again']
Iterate over your list, with a nice unpacking and keep the value from "name"
values = [["Test_1", {"name":"Test_level_1", "value":10}, {"name":"Test_level_1 again", "value":15}],
["Test_2", {"name":"Test_level_2", "value":1}, {"name":"Test_level_2 again", "value":5}]]
result = []
for first, *others in values:
for other in others:
result.append(other["name"])
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