棘手:如何将字典值(包含列表)与列表进行比较?

时间:2012-11-15 17:36:45

标签: python list dictionary

我有一个dict(包含列表)和一个列表,我想比较一下:

首先,我想知道的是ref中的每个值列表(例如,对于issue1,值列表是[1, 1, 0, 0, 0, 1, 1, 1])是否与列表abf具有相同的长度。< / p>

然后是棘手的部分:如果它们具有相同的长度,我想将列表abf中的每个项目与ref中的每个值列表进行比较。

但是......在一个条件下,程序将继续移动到ref中的下一个值列表(不检查当前值列表的其余项目),这是否是项目的value-list为1,列表abf中的对应项为0。

为清楚起见,这是一个例子:

dict ref中键'issue1'的值列表为[1, 1, 0, 0, 0, 1, 1, 1].列表abf[1, 1, 0, 1, 0, 1, 0, 0]

现在,我想检查这两个列表中的每个项目(issue1的值列表的第一项,列表abf的第一项,然后是issue1的第二项abf的第二项......依此类推......):由于前两项是1和1而且条件(见上文)没有达到,所以它将继续下两个项目(再次1和1)等等,直到它(在这种情况下)得到第七项(1和0)。此时,它将停止将issue1的值列表与列表abf进行比较,并继续将下一个值列表(issue2)与列表abf进行比较。我希望你明白这个想法!

到目前为止,这是我的代码:

## ref is a dict with lists as values, abf is a list
ref = {'issue1': [1, 1, 0, 0, 0, 1, 1, 1], 
       'issue2': [1, 0, 0, 1, 0, 0, 0, 0], 
       'issue3': [0, 1, 0, 0, 1, 0, 0, 1]}
abf = [1, 1, 0, 1, 0, 1, 0, 0]

## getting the length of the lists in ref and abf ans save them in ref_total & abf_total
for key in ref:
    [int(item) for item in ref[key]]
ref_total = len(ref[key])
abf_total = len(abf)

## check whether ref_total and abf_total has same value
if ref_total == abf_total:
    for key, value in ref.items():
        for j in value:
            if (ref[key][j] == 1) and (abf[j] == 0): ## if item in ref is 1 and in abf is 0, go on to the next value-list
                break
            if j == abf_total-1: ## if he compared the whole value-list of the current key of ref with abf and the condition above did not occur, save the key of this value-list in resp!
                resp = ref[key]

else:
    resp = 'Length of strings varies!' ##if the lists don't have the same length

print resp ##let me know, which key "went through"

我真的很期待你的回应。代码不起作用,我不明白为什么!

4 个答案:

答案 0 :(得分:0)

您的代码存在很少的问题,我想指出: -

for key in ref:
    [int(item) for item in ref[key]]

首先,您的上述循环不明确。它没有做任何事情,只是创建一个被忽略的列表

其次,

ref_total = len(ref[key])
abf_total = len(abf)

上述分配不起作用,因为您已将其放在for循环之外。缩进问题。

if ref_total == abf_total:
    for key, value in ref.items():

在上面的代码段中,您应该先在{for循环中移动if,而不是先使用for条件,然后再使用if condition循环。因此,对于每个key, value对,请检查len(value) == len(abf)。如果为true,则继续内部for循环。

if (ref[key][j] == 1) and (abf[j] == 0)

此条件不考虑abf[j] = 1ref[key][j] = 0。您可以检查相等性,而不是检查不等式,而只是对此进行否定。那会更容易。 (编辑: - 只是注意到了,你只想检查它是否只适用于那种情况。所以你可以忽略这一变化。)

此外,您的内部循环不应该是for j in value。您无法在j上编制索引。使用for j in range(ref_list)来比较每个索引的值。


您可以在上述更改后试用此代码: -

ref = {'issue1': [1, 1, 0, 0, 0, 1, 1, 1], 
       'issue2': [1, 0, 0, 1, 0, 0, 0, 0], 
       'issue3': [0, 1, 0, 0, 1, 0, 0, 1],
       'issue4': [1, 1, 0, 1, 0, 1, 0, 0]}
abf = [1, 1, 0, 1, 0, 1, 0, 0]
abf_total = len(abf)  # Since it will never change. Move it outside

for key, value in ref.items():
    resp = ""
    ref_total = len(value)        

    if ref_total == abf_total:
        for j in range(ref_total):

            if not (value[j] == abf[j]): 
                break
            if j == abf_total-1: 
                resp = value
                print resp

    else:
        resp = 'Length of strings varies!' ##if the lists don't have the same length

输出: -

[1, 1, 0, 1, 0, 1, 0, 0]

答案 1 :(得分:0)

我添加了一些注释作为评论,但这应该有效:

## ref is a dict with lists as values, abf is a list
ref = {'issue1': [1, 1, 0, 0, 0, 1, 0, 1],
       'issue2': [1, 0, 0, 1, 0, 0, 0, 0],
       'issue3': [0, 1, 0, 0, 1, 0, 0, 1]}
abf = [1, 1, 0, 1, 0, 1, 1, 0]

# abf_total does not change during cycle. Calculate it outside.
abf_total = len(abf)

## getting the length of the lists in ref and abf ans save them in ref_total & abf_total
for key, items in ref.iteritems():
    ref_total = len(items)

    ## check whether ref_total and abf_total has same value
    if ref_total == abf_total:
        # Here 'ref' screened the outside ref. Use 'i'
        for i, value in enumerate(items):
            if (items[i] == 1) and (abf[i] == 0): ## if item in ref is 1 and in abf is 0, go on to the next value-list
                break
        if i == abf_total-1:
            # if he compared the whole value-list of the current key of ref with abf and the condition above did not occur, save the key of
this value-list in resp!
            resp = "%s = %s" % (key, items)
    else:
        resp = 'Length of strings varies!' ##if the lists don't have the same length

print resp ##let me know, which key "went through"

输出结果为:

issue2 = [1, 0, 0, 1, 0, 0, 0, 0]

答案 2 :(得分:0)

如果我理解你的要求,Python就内置了这个功能。你可以这样做。

ref = {'issue1': [1, 1, 0, 0, 0, 1, 0, 1], 
       'issue2': [1, 0, 0, 1, 0, 0, 0, 0], 
       'issue3': [0, 1, 0, 0, 1, 0, 0, 1]}

abf = [1, 1, 0, 1, 0, 1, 1, 0]

abf == abf
# OUT: True


def findinref(d, abf):
    for key, value in ref.items():
        if value == abf:
            return value
    return None

findinref(ref, abf)

findinref(ref, [1,1,0,0,0,1,0,1])
# OUT: [1, 1, 0, 0, 0, 1, 0, 1]

答案 3 :(得分:0)

非常感谢你们,你们真棒!我没想到那么多反馈!!你真的帮助了我很多。我去了Rohit Jain的解决方案 - 非常感谢Rohit!这是完整的代码(如果有改进的空间,请毫不犹豫地批评我!任何反馈都非常感谢!)

ref = {'issue1': [1, 1, 0, 0, 0, 1, 1, 1], 
       'issue2': [1, 0, 0, 1, 0, 0, 0, 0], 
       'issue3': [0, 1, 0, 0, 1, 0, 0, 1]}
abf = [1, 1, 0, 1, 0, 1, 0, 0]

abf_total = len(abf)

for key, value in ref.items():
    ref_total = len(ref[key])   
    if ref_total == abf_total:
        i = -1
        for j in value:
            i += 1
            if (j == 1) and (abf[i] == 0):
                break
            if i == len(abf)-1:
                resp = key
    else:
        resp = 'Length of strings varies!'
print resp