Python 如果是和,那么

Python 如果是和,那么,python,list,if-statement,dictionary,Python,List,If Statement,Dictionary,我试图创建一个脚本,在列表中循环 我需要查看能力标识符的有限列表(400)(例如124、129等-正常整数) 然后我有一个字典,记录每个用户的能力。键是用户名,每个键的值是一个整数列表(即用户具有哪些能力) 比如说 User x - [124, 198, 2244 ...] User Y - [129, 254, 198, 2244 ...] 我希望编制一个矩阵,强调每个能力与其他能力之间的频率-邻接矩阵 例如,在上述示例中,能力198与能力2244发生了两次。而能力254和124从未同时出现

我试图创建一个脚本,在列表中循环

我需要查看能力标识符的有限列表(400)(例如124、129等-正常整数)

然后我有一个字典,记录每个用户的能力。键是用户名,每个键的值是一个整数列表(即用户具有哪些能力)

比如说

User x - [124, 198, 2244 ...]
User Y - [129, 254, 198, 2244 ...]
我希望编制一个矩阵,强调每个能力与其他能力之间的频率-邻接矩阵

例如,在上述示例中,能力198与能力2244发生了两次。而能力254和124从未同时出现过

我目前正在使用此代码:

fe = []    
count = 0
competency_matches = 0
for comp in competencies_list:
    common_competencies = str("")
for comp2 in competencies_list:
    matches = int(0)
    for person in listx:
        if comp and comp2 in d1[person]:
            matches = matches + 1
        else:
            matches = matches
    common_competencies = str(common_competencies) + str(matches) + ","
fe.append(common_competencies)
print fe
print count
count = count + 1
这不起作用,只返回每个能力发生的总次数。我认为问题在于“d1中的if comp和comp2[person]:”行

例如,问题是,如果一个人具有以下能力[123、1299、1236],我搜索了能力123,这将返回两次,因为它出现在123和1236条目中。是否存在在使用if ______;和__; then操作时强制精确匹配的方法

或者有没有人对如何实现这一点提出改进建议


提前感谢您的指点。干杯

你误解了
的工作原理。要测试列表中是否有两个值,请使用:

if comp1 in d1[person] and comp2 in d1[person]:
  ...

你的版本做了一些其他的事情。它是这样绑定的:
if(comp1)和(d1[person]中的comp2])
。换句话说,它将
comp1
解释为真值,然后使用列表包含检查执行布尔值
。这是有效的代码,但它不能满足您的需要。

您误解了
的工作原理。要测试列表中是否有两个值,请使用:

if comp1 in d1[person] and comp2 in d1[person]:
  ...

你的版本做了一些其他的事情。它是这样绑定的:
if(comp1)和(d1[person]中的comp2])
。换句话说,它将
comp1
解释为真值,然后使用列表包含检查执行布尔值
。这是有效的代码,但它不能满足您的需要。

此处的缩进表示两个循环没有嵌套。首先遍历
能力列表
并将
普通能力
设置为空字符串400次,然后再次遍历
能力列表
,并按照phooji的解释进行操作。我很确定这不是您想要做的。

此处的缩进意味着您的两个循环没有嵌套。首先遍历
能力列表
并将
普通能力
设置为空字符串400次,然后再次遍历
能力列表
,并按照phooji的解释进行操作。我很确定这不是您想要做的。

这应该运行得更快一些,因为它删除了一个额外的迭代层。希望能有帮助

from collections import defaultdict
from itertools import combinations

def get_competencies():
    return {
        "User X": [124, 198, 2244],
        "User Y": [129, 254, 198, 2244]
    }

def get_adjacency_pairs(c):
    pairs = defaultdict(lambda: defaultdict(int))
    for items in c.itervalues():
        items = set(items)  # remove duplicates
        for a,b in combinations(items, 2):
            pairs[a][b] += 1
            pairs[b][a] += 1
    return pairs

def make_row(lst, fmt):
    return ''.join(fmt(i) for i in lst)

def make_table(p, fmt="{0:>8}".format, nothing=''):
    labels = list(p.iterkeys())
    labels.sort()

    return [
        make_row([""] + labels, fmt)
    ] + [
        make_row([a] + [p[a][b] if b in p[a] else nothing for b in labels], fmt)
        for a in labels
    ]

def main():
    c = get_competencies()
    p = get_adjacency_pairs(c)
    print('\n'.join(make_table(p)))

if __name__=="__main__":
    main()
导致

             124     129     198     254    2244
     124                       1               1
     129                       1       1       1
     198       1       1               1       2
     254               1       1               1
    2244       1       1       2       1        

。。。显然,一个400列的表格要打印到屏幕上有点多;我建议使用csv.writer()将其保存到一个文件中,然后您可以在Excel或OpenOffice中处理该文件。

这应该运行得更快一些,因为它删除了一个额外的迭代层。希望能有帮助

from collections import defaultdict
from itertools import combinations

def get_competencies():
    return {
        "User X": [124, 198, 2244],
        "User Y": [129, 254, 198, 2244]
    }

def get_adjacency_pairs(c):
    pairs = defaultdict(lambda: defaultdict(int))
    for items in c.itervalues():
        items = set(items)  # remove duplicates
        for a,b in combinations(items, 2):
            pairs[a][b] += 1
            pairs[b][a] += 1
    return pairs

def make_row(lst, fmt):
    return ''.join(fmt(i) for i in lst)

def make_table(p, fmt="{0:>8}".format, nothing=''):
    labels = list(p.iterkeys())
    labels.sort()

    return [
        make_row([""] + labels, fmt)
    ] + [
        make_row([a] + [p[a][b] if b in p[a] else nothing for b in labels], fmt)
        for a in labels
    ]

def main():
    c = get_competencies()
    p = get_adjacency_pairs(c)
    print('\n'.join(make_table(p)))

if __name__=="__main__":
    main()
导致

             124     129     198     254    2244
     124                       1               1
     129                       1       1       1
     198       1       1               1       2
     254               1       1               1
    2244       1       1       2       1        

。。。显然,一个400列的表格要打印到屏幕上有点多;我建议使用csv.writer()将其保存到一个文件中,然后您可以在Excel或OpenOffice中处理该文件。

common\u capabilities=str(“”)???Python不是Java。某些_var=''是好的enough@RestRisiko:您如何用Java编写它?不知道-但是在这里使用str(..)并不能sense@khachik我不是Java迷,但Java确实有字符串文字。:)@克里斯·泰勒:你在哪里见过使用
这样的代码?你能提供报价、链接或参考资料吗?这是非常非常错误的事情。你在哪里看到的?普通能力=str(“”???Python不是Java。某些_var=''是好的enough@RestRisiko:您如何用Java编写它?不知道-但是在这里使用str(..)并不能sense@khachik我不是Java迷,但Java确实有字符串文字。:)@克里斯·泰勒:你在哪里见过使用
这样的代码?你能提供报价、链接或参考资料吗?这是非常非常错误的事情。您在哪里看到的?抱歉,当我在堆栈溢出中输入此内容时,缩进出现错误-第二个循环缩进以嵌套在第一个循环中。抱歉,当我在堆栈溢出中输入此值时,缩进似乎是错误的-第二个循环缩进,以嵌套在第一个循环中。嵌套字典实例化很好地使用了
lambda
。嵌套字典实例化很好地使用了
lambda