繁体   English   中英

Python for循环查询

[英]Python for loops inquiry

在字典中,键是名称,值是关联,它们是参数的一部分,而我感兴趣的人是另一个参数,目标是将组中所有的人都带入一个新的名单。

例如,

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']

如果选择的人是亚历克斯·邓菲

有技巧和东东吗?

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)

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

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]

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM