Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查项目是否在字典值中_Python_Python 3.x_Dictionary_Return - Fatal编程技术网

Python 检查项目是否在字典值中

Python 检查项目是否在字典值中,python,python-3.x,dictionary,return,Python,Python 3.x,Dictionary,Return,我正在尝试为我的类编程编写一个游戏,我必须使用一个如下所示的字典: board={ 'map': [{'x': 39, 'y': 41}], 'hubs': [{'x': 21, 'y': 3, 'structure_points': 1000, 'capacity': 1500, 'regen_rate': 25}, { 'x'

我正在尝试为我的类编程编写一个游戏,我必须使用一个如下所示的字典:

board={
    'map': [{'x': 39,
        'y': 41}],
    'hubs': [{'x': 21,
            'y': 3,
            'structure_points': 1000,
            'capacity': 1500,
            'regen_rate': 25},
        {
            'x': 21,
            'y': 38,
            'structure_points': 1000,
            'capacity': 1500,
            'regen_rate': 25}],
    'peaks': [{'x': 10,
            'y': 10,
            'intensity': 200},
        {
            'x': 11,
            'y': 10,
            'intensity': 300},
        {
            'x': 12,
            'y': 10,
            'intensity': 400},
        {
            'x': 10,
            'y': 11,
            'intensity': 200},
        {
            'x': 10,
            'y': 12,
            'intensity': 500}],
    'cruisers': [{
        'alpha': {
            'health_points': 100,
            'available_energy': 400,
            'strike_cost': 10,
            'cruiser_deplacement': 10,
            'shooting_range': 1},
        'beta': {
            'health_points': 100,
            'available_energy': 400,
            'strike_cost': 10,
            'cruiser_deplacement': 10,
            'shooting_range': 1
        }}]}
def hub_exists(board, x_dest, y_dest):
    for hub in board.get('hubs', []):
        if hub.get('x') == x_dest and hub.get('y') == y_dest:
            return True
        else:
            return False
基本上,该词典创建了一个棋盘,并在上面放置了不同的单位(山峰、枢纽和巡洋舰)。我想创建一个函数来检查某个点上是否已经有一个单元。我已经有了这样的东西:

board={
    'map': [{'x': 39,
        'y': 41}],
    'hubs': [{'x': 21,
            'y': 3,
            'structure_points': 1000,
            'capacity': 1500,
            'regen_rate': 25},
        {
            'x': 21,
            'y': 38,
            'structure_points': 1000,
            'capacity': 1500,
            'regen_rate': 25}],
    'peaks': [{'x': 10,
            'y': 10,
            'intensity': 200},
        {
            'x': 11,
            'y': 10,
            'intensity': 300},
        {
            'x': 12,
            'y': 10,
            'intensity': 400},
        {
            'x': 10,
            'y': 11,
            'intensity': 200},
        {
            'x': 10,
            'y': 12,
            'intensity': 500}],
    'cruisers': [{
        'alpha': {
            'health_points': 100,
            'available_energy': 400,
            'strike_cost': 10,
            'cruiser_deplacement': 10,
            'shooting_range': 1},
        'beta': {
            'health_points': 100,
            'available_energy': 400,
            'strike_cost': 10,
            'cruiser_deplacement': 10,
            'shooting_range': 1
        }}]}
def hub_exists(board, x_dest, y_dest):
    for hub in board.get('hubs', []):
        if hub.get('x') == x_dest and hub.get('y') == y_dest:
            return True
        else:
            return False

这个函数告诉我是否已经有了一个中心,但是如何让我的函数知道它是哪一个,这样我就可以修改中心字典中的值呢?谢谢你的帮助

当dict的
x
y
与输入匹配时,您迭代dict列表的
hub
dict就是您要修改的dict,因此它可以简单地返回dict供调用者进行修改:

def get_hub(board, x_dest, y_dest):
    for hub in board.get('hubs', []):
        if hub.get('x') == x_dest and hub.get('y') == y_dest:
            return hub

...

hub = get_hub(board, 21, 3)
hub['capacity'] = 2000

为什么不直接返回集线器而不是返回布尔值?