Python for循环查询

2024-09-28 22:21:16 发布

您现在位置:Python中文网/ 问答频道 /正文

在字典中,键是名称,值是它们所属的关联,作为参数之一,而我感兴趣的人是另一个参数,这样做的目的是让他所在的组中的所有人都进入一个新的列表。你知道吗

例如

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

有什么建议吗?你知道吗


Tags: tokeyinfornetifnetworkpeople
3条回答

试试这个。考试的顺序和你想要的不一样。你知道吗

for net in connections[person]:
    for candidate, candidate_nets in connections.items():
        if net in candidate_nets:
                test.append(candidate)

与其编辑代码,不如说这是一个更清晰的解决方案:

for net in network_to_people:
    if person in network_to_people[net]:
        test.extend(network_to_people[net]) 
# extend does exactly what you'd think it does. 
# l = [2,3], with l.extend([4,4]) l becomes [2,3,4,4]

函数的作用是。你知道吗

from __future__ import print_function  # For Python 2/3 support

demo_affiliations = {
    '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'],
    }
demo_person = 'Alex Dunphy'


# This is the main function; it'll take affiliations and a person
# as arguments, and returns a list of associates.


def get_associates(affiliations, person):
    associates = []
    persons_organizations = set(affiliations[person])

    for possible_associate, organizations in affiliations.items():
        intersection = set(organizations).intersection(persons_organizations)
        num_intersections = len(intersection)

        if intersection:  # This isn't needed, but it's more readable
            associates.extend([possible_associate] * num_intersections)

    return associates


def main(affiliations, person):
    associates = sorted(get_associates(affiliations, person))
    print('Associates:', associates)


if __name__ == '__main__':
    main(demo_affiliations, demo_person)

相关问题 更多 >