Blog writing style began to change today, not much else to say.
Today topics are as follows:
# 7, the write function, performs the following functions: (8 points)
# example:
# user_list = [
# { "name": "Alex", "Hobby": "Smoke"},
# { "name": "Alex" , "hobby": "drink"}
# { "name": "Alex", "hobby": "hot head"}
# { "name": "wusir", "hobby": "call wheat"},
# { "name": "wusir", "Hobby": "hip-hop"},
# ]
# write function processes user_list and return the following results:
# [{ "name": "Alex", "hobby_list": [ "smoke." , "drink", "hot head"]},
# {"name":"wuhir","hobby_list": ["喊麦","街舞"]},]
I would like to write their own writing process method, the first direct look at the complete correct code directly look down
Read the title beginning, I found that the law is "alex", "name", "hobby" became a by multiple
So I thought of using the set to re-set
I want to put a list of key user_list collected and turned into a list, then go through a re-set collection
But come the following code can no longer be found when the handwritten code, I think I know this idea is wrong with the
dict={}
list=[]
for a in user_list:
# print(a)
for k,v in a.items():
# print(k,v)
dict.setdefault(k,[]).append(v)
for b,c in dict.items():
print(b,c)
print(dict)
The above problem my card two or three hours I have been stuck in the 'name' and 'hobby' is set to a variable, leading to too many variables feel, can not start (quietly to force force: I do not know is not the reason)
If it is not the reason I said, I want to know where is the problem
So I can not think like a long time, and then go to Baidu to find, locate the following code, but I did not understand
= result [] # { 'name': 'Alex', 'hobby_list': [ 'smoke']}
for User in user_list:
# 1. empty list for loop, which determines whether the result of the presence of this person, if there is added hobby_list a Hobby.
# 2. create a new dictionary does not exist.
for new_user in the Result:
IF the User [ ' name ' ] == new_user [ ' name ' ]:
new_user [ ' hobby_list ' ] .append (the User [ ' Hobby ' ])
BREAK
the else :
December = {}
dic["name"] = user['name']
dic['hobby_list'] = [user['hobby']]
result.append(dic)
print(result)
But I read it several times above code and retrial question several times, I think of a solution ideas
Ideas are as follows:
I have the results in a list, by the dictionary and found that, there is the name and hobby may be composed of a relatively key-value pair
Names and preferences may form another key-value pairs, so I thought of the name hobby into a dictionary, and then finally to become value
Specific code as follows:
def main(user_list):
#有结果可知,我们需要字典和列表,所以先设dict和list
dict={}
list=[]
for i in user_list:#这里通过for循环先把原列表中的字典提出来
dict.setdefault(i['name'],[]).append(i['hobby'])#这里通过字典的setdefault的性质来把名字去重
for k,v in dict.items():#这里通过for循环把上一步字典里的名字和爱好分开变成值
list.append({'name':k,'hobby_list':v})#这里把名字和爱好变成的键变成字典里'name'和'hobby_list'的值
return list
print(main(
[
{"name": "alex","hobby":"抽烟"},
{"name" :"alex","hobby":"喝酒"},
{"name" :"alex","hobby":"烫头"},
{"name":"wusir","hobby":"喊麦"},
{"name":"wusir","hobby":"街舞"},
]
))
总结:写代码要看清要求是什么,不一定要顺着推,可以逆着推,有结果推出来,这样也或许可以解决问题