0

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

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

然后是棘手的部分:如果它们的长度相同,我想将列表abf中的每个项目与ref.

但是......在一种情况下,程序将继续前进到下一个值列表ref(不检查当前值列表的剩余项目),这是如果值列表的项目是 1 并且对应的列表中的项目abf为 0。

为了清楚起见,这里有一个例子:

dict ref 中键 'issue1' 的值列表是[1, 1, 0, 0, 0, 1, 1, 1].The list abfis [1, 1, 0, 1, 0, 1, 0, 0]

现在,我想检查这两个列表中的每一项(issue1 的值列表的第一项与 list 的第一项abf,然后是第二项issue1的第二项,abf以此类推……):前两个项目是 1 和 1 并且条件(见上文)不满足,它应该继续接下来的两个项目(再次是 1 和 1)等等,直到它到达(在这种情况下)第七项(即 1 和 0)。此时它将停止将 value-list ofissue1与 listabf进行比较,并继续将下一个 value-list (of issue2) 与 list进行比较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

4 回答 4

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]
于 2012-11-15T17:53:14.220 回答
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
于 2012-11-17T15:42:25.293 回答
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():

在上面的代码段中,您应该将您的if条件移到 for 循环中,而不是先有条件,然后再循环。因此,对于每一对,检查. 如果为真,则继续内部 for 循环。forif conditionkey, valuelen(value) == len(abf)

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]
于 2012-11-15T17:42:45.057 回答
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]
于 2012-11-15T17:50:48.063 回答