Python for循环查询

时间:2015-08-05 01:45:33

标签: python for-loop

字典中的键是名称和值,它们是作为参数之一的关联,而我感兴趣的是另一个参数的人,目标是让他所有的人都在进入新名单。

例如,

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

任何提示和内容?

4 个答案:

答案 0 :(得分:1)

get_associates()函数可以执行您想要的操作。

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)

答案 1 :(得分:0)

我没有编辑代码,而是说这是一个更清晰的解决方案:

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]

答案 2 :(得分:0)

试试这个。测试顺序与您想要的顺序不同。

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

答案 3 :(得分:0)

另一个答案:删除测试中的重复人员。

my_nets = set(connections[person])
for candidate, candidate_nets in connections.items():
    if set(candidate_nets) & my_nets:
        test.append(candidate)