使用两个字典中的值搜索键,并在Python中输出匹配键

2024-06-01 01:32:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一些代码(或者至少尝试过)来比较两个字典(X和Y)的键和值,这两个字典有完全相同的键,但并不总是相同的值,比如X-Y坐标图。代码应该是:

  1. 使用键的值搜索键(需要搜索的特定值由预设变量提供)
  2. 将结果键相互比较是否匹配,如果匹配,则返回其中一个作为输出

我在我的所有代码中都使用了python2.7(我对这门语言非常陌生),但是python3解决方案是受欢迎的,但不是首选

代码如下:

XDict = {
    "Jersey" : 0,
    "Boston" : -1,
    "York" : 0,
    "Vegas" : 1,
    "Diego" : 0
    }

YDict = {
    "Jersey" : 0,
    "Boston" : 0,
    "York" : -1,
    "Vegas" : 0,
    "Diego" : 1
    }


hereX = 0
hereY = 0

def GetLocation(Dict1, Dict2):
    locationX = "_"
    locationY = "not_"
    Location = ''

    for placeX, positionX in Dict1.iteritems():                  
        if positionX == hereX:
            locationX = placeX

    for placeY, positionY in Dict2.iteritems():
        if positionY == hereY:
            locationY = placeY

    if locationX == locationY:
        Location = locationX
        Location = locationY

    print Location

GetLocation(XDict, YDict)

预期的输出是"Jersey"

不幸的是,代码没有产生预期的输出(产生None),这可能是locationX和locationY从不匹配的结果。 我尝试使用上述代码的另一种形式,删除最后一个if块,并在函数顶部插入一个while locationX <> locationY:。但这只会导致代码永远循环

我对代码的更大目标是为Player类编写一个GetLocation()函数,该函数使用分配给每个位置的x-y坐标返回播放器的位置。我知道我可以使用一个字典和具有唯一值的键来实现这一点,但我更愿意使用x-y坐标

我用尽了我能想到的解决方案,并尝试在互联网上的其他地方搜索解决方案,包括堆栈溢出,并找到了类似的,有些有用的建议,但这些也不能解决问题,包括 this

谢谢你抽出时间


Tags: 函数代码if字典location解决方案bostonyork
3条回答

我对这个问题的看法是:

XDict = {
    "Jersey" : 0,
    "Boston" : -1,
    "York" : 0,
    "Vegas" : 1,
    "Diego" : 0
    }

YDict = {
    "Jersey" : 0,
    "Boston" : 0,
    "York" : -1,
    "Vegas" : 0,
    "Diego" : 1
    }


hereX = 0
hereY = 0

for ((n1, x), (n2, y)) in zip(XDict.items(), YDict.items()):
    if (x==hereX) and (y==hereY):
        print(n1)
        break

印刷品:

Jersey

然而,不清楚为什么你在这两个字典中都存储城市名称。更好的解决方案是这样存储数据集:

d = {(0, 0): "Jersey",
     (-1, 0): "Boston",
     (0, -1): "York",
     (1, 0): "Vegas",
     (0, 1): "Diego"}

这样你就可以通过d[(hereX, hereY)]索引它

首先,请遵循一些风格指南,例如PEP8

我给你的解决方案使用发电机

XDict = {
"Jersey" : 0,
"Boston" : -1,
"York" : 0,
"Vegas" : 1,
"Diego" : 0
}

YDict = {
    "Jersey" : 0,
    "Boston" : 0,
    "York" : -1,
    "Vegas" : 0,
    "Diego" : 1
    }

def keys_of_equal_vales(dict1, dict2):
    for key in dict1 :
        if key in dict2 and dict1[key]==dict2[key]:
            yield key
        else :
            continue 
    raise StopIteration 

print(list(keys_of_equal_vales(XDict,YDict)))

结果:

['Jersey']

您没有存储具有所需值的所有键。您需要存储所有这些数据,然后找到交叉点

XDict = {
    "Jersey" : 0,
    "Boston" : -1,
    "York" : 0,
    "Vegas" : 1,
    "Diego" : 0
    }

YDict = {
    "Jersey" : 0,
    "Boston" : 0,
    "York" : -1,
    "Vegas" : 0,
    "Diego" : 1
    }


hereX = 0
hereY = 0

def GetLocation(Dict1, Dict2):
    locationX = []
    locationY = []
    Location = ''

    for placeX, positionX in Dict1.items():                  
        if positionX == hereX:
            locationX.append(placeX)

    for placeY, positionY in Dict2.items():
        if positionY == hereY:
            locationY.append(placeY)

    print(set(locationX).intersection(set(locationY)))

GetLocation(XDict, YDict)

输出:

{'Jersey'}

相关问题 更多 >