通过if语句查找条件字典的问题

时间:2016-06-03 15:49:45

标签: python if-statement dictionary

我有一本字典,我填充了值。这些值通过for循环提供给字典。

storageDict = {}
listholder = ["word1", "word2", "word2", "word3", "word4", "word5", "word6", "word7"]
values = ["word1", "word2", "word3", "word2", "word5", "word6", "word7", "word8"]
index_tracker = 0

for each_element in listholder:
    if index_tracker == 7:
        pass
    else:
        storageDict[str(each_element)] = values[index_tracker]
        index_tracker += 1

print(storageDict)

这给了我以下输出:

 {'word5': 'word6', 'word1': 'word1', 'word3': 'word2', 'word4': 'word5', 'word6': 'word7', 'word2': 'word3'}

虽然所有键值组合都是唯一的,但我想避免使用字典包含与值键组合相同的键值组合的任何场景。即在上面的字典中,我们有以下两个键值组合:

  • 'word3': 'word2'
  • 'word2': 'word3'

因此,在将其提交到dict之前,我想检查一个值键组合和键值组合是否已经在字典中。我提出了以下代码,但PyCharm给了我一个KeyError

storageDict = {}
listholder = ["word1", "word2", "word2", "word3", "word4", "word5", "word6", "word7"]
values = ["word1", "word2", "word3", "word2", "word5", "word6", "word7", "word8"]
index_tracker = 0

for each_element in listholder:
    if index_tracker == 7:
        pass
    else:
        if storageDict[each_element] == values[index_tracker] and storageDict[values[index_tracker]] == each_element:
             pass
        else:
            storageDict[each_element] = values[index_tracker]
            index_tracker += 1

print(storageDict)

我想要的输出是两种情况之一:

    {'word5': 'word6', 'word1': 'word1', 'word4': 'word5', 'word6': 'word7', 'word2': 'word3'}

或:

    {'word5': 'word6', 'word1': 'word1', 'word3': 'word2', 'word4': 'word5', 'word6': 'word7'}

这是KeyError

Traceback (most recent call last):
File "C:/Users/Admin/PycharmProjects/momely/placementarchitect/testbench.py", line 42, in <module>
if storageDict[each_element] == values[index_tracker] and storageDict[values[index_tracker]] == each_element:
KeyError: 'word1'

我相信我理解错误发生的原因。通过在条件if语句中包含我的检查,我正在请求尚不存在的字典键的键值。

但是,如何检查一个键值呢?

我考虑过使用元组,但需要在不同的操作中使用字典的快速循环。

3 个答案:

答案 0 :(得分:1)

这是一种方法。我基本上zip,过滤我们不需要的条目,然后将过滤后的列表转换为dict

listholder = ["word1", "word2", "word2", "word3", "word4", "word5", "word6", "word7"]
values = ["word1", "word2", "word3", "word2", "word5", "word6", "word7", "word8"]
filtered = []
allItems = zip(listholder, values)
for key, value in allItems:
    if not ( (key,value) in filtered or (value, key) in filtered):
        filtered.append((key,value))
print dict(filtered)

输出:

{'word5': 'word6', 'word4': 'word5', 'word7': 'word8', 'word6': 'word7', 'word1': 'word1', 'word2': 'word3'}

答案 1 :(得分:0)

响应不准确

确定。在@spectras和@wwii的帮助下(谢谢你指出我正确的方向)我能够想出一个解决方案。问题在于我在不使用some_dict.get(item)方法的情况下设置逻辑检查的方式。

这里是解决方案:

bool ptest(int y)
{
    for (int x=0; x<primes.size(); ++x)
    {
        if (y%primes[x]==0)
        {
            return false;
        }
    }
    return true;
}

这会根据需要生成输出:

storageDict = {}
listholder = ["word1", "word2", "word2", "word3", "word4", "word5", "word6", "word7"]
values = ["word1", "word2", "word3", "word2", "word5", "word6", "word7", "word8"]
index_tracker = 0

for each_element in listholder:
    if index_tracker == 7:
        pass
    else:
        if storageDict.get(each_element) == storageDict.get(values[index_tracker]) and each_element == storageDict.get(values[index_tracker]):
            pass
        else:
            storageDict[each_element] = values[index_tracker]
        index_tracker += 1

print(storageDict)

感谢大家提供的有用提示。这真的帮助我解决了这个问题。

中号

答案 2 :(得分:0)

在两个列表中使用zip并检查键值对是否在字典中具有备用值 - 密钥对:

d = {}

for k, v in list(zip(listholder, values))[:-1]:
    if v in d:
        if d[v] == k:
            continue
    d[k] = v

print(d)
# {'word5': 'word6', 'word4': 'word5', 'word2': 'word3', 'word1': 'word1', 'word6': 'word7'}

现有密钥会在原始代码中更新,并且您正在跳过两个列表中的最后一项。我在这里复制了那些。