On the problem
This question was also raised by group friends during group chat
- Make the upper part into the lower part, the simplest way to write one
Solution (my)
- It can be solved with only one line of code, involving the following knowledge (official documentation)
- The idea is to implement the code logic in the traditional way. Then look for ways to simplify. You can see that I tried three and the last line was right, but without the previous step-by-step reasoning, it is difficult to think of the third one at once.
There are answers from other group friends
- As many for as you can
Give my code again:
a = [{
'a':1, "b": 2, 'c':3}, {
'a':11, 'b':22, 'c':33}]
d = {
}
for key in a[0].keys():
c = []
for query in a:
c.append(query.get(key))
d[key] = c
print(d)
b = [query.get(key) for key in a[0].keys() for query in a]
m = [[query.get(key) for query in a] for key in a[0].keys()]
# n是正确解法
n = {
key:[query.get(key) for query in a] for key in a[0].keys()}
print(b)
print(m)
print(n)
over!