在字典中,键是名称,值是它们所属的关联,作为参数之一,而我感兴趣的人是另一个参数,这样做的目的是让他所在的组中的所有人都进入一个新的列表。你知道吗
例如
connections = {
'Alex Dunphy': ['Orchestra', 'Chess Club'],
'Manny Delgado': ['Chess Club'],
'Cameron Tucker': ['Clown School', 'Wizard of Oz Fan Club'],
'Claire Dunphy': ['Parent Teacher Association'],
'Gloria Pritchett': ['Parent Teacher Association'],
'Phil Dunphy': ['Real Estate Association'],
'Mitchell Pritchett': ['Law Association']
}
我所做的是颠倒顺序,使其成为关键关联和价值观,即参与该关联的人,并试图从中附加到一个空列表中,但由于某些原因,它不起作用。代码如下。你知道吗
if person_to_networks != {}:
ppl = []
network_to_people = {}
for key in person_to_networks:
for i in range(len(person_to_networks[key])):
if person_to_networks[key][i] not in network_to_people:
ppl.append(key)
network_to_people[person_to_networks[key][i]] = [key]
elif person_to_networks[key][i] in network_to_people:
network_to_people[person_to_networks[key][i]].append(key)
for net in network_to_people:
for i in range(len(network_to_people[net])):
if person in network_to_people[net]:
test.append(network_to_people[net][i])
print(test)
输出为:
[]
所需输出为:
['Manny Delgado', 'Alex Dunphy', 'Alex Dunphy']
如果被选中的人是Alex Dunphy
有什么建议吗?你知道吗
试试这个。考试的顺序和你想要的不一样。你知道吗
与其编辑代码,不如说这是一个更清晰的解决方案:
函数的作用是。你知道吗
相关问题 更多 >
编程相关推荐