【问题标题】:PYTHON: Move a value in a dictionary from one key to another keyPYTHON:将字典中的值从一个键移动到另一个键
【发布时间】:2017-09-08 19:55:56
【问题描述】:

如果我有字典说:

dictionary = {'blue': [ [0,1], [0,2] ] , 'red': [ [0,3], [0,4] ] }

我如何将blue 值例如[0,1] 移动到red 而不会在blue 中留下一个副本而不对删除进行硬编码(所以如果我有一个包含一百个值的不同列表并且想要移动从一个键到另一个键的值也应该以这种方式工作)。

我希望输出看起来像这样:

dictionary = {'blue': [ [0,2] ] , 'red': [ [0,3], [0,4], [0,1] ] }

编辑:

好的,因为人们在询问上下文:

我的字典是:

d = {' . ': [[0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [0, 8], [0, 9], [1, 1],
             [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [2, 1], [2, 2],
             [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 9], [3, 1], [3, 2], [3, 3],
             [3, 4], [3, 5], [3, 6], [3, 7], [3, 8], [3, 9], [4, 0], [4, 1], [4, 2], [4, 3],
             [4, 4], [4, 5], [4, 6], [4, 7], [4, 8], [4, 9], [5, 0], [5, 1], [5, 2], [5, 3],
             [5, 4], [5, 5], [5, 6], [5, 7], [5, 8], [5, 9], [6, 0], [6, 1], [6, 2], [6, 3],
             [6, 4], [6, 5], [6, 6], [6, 7], [6, 8], [6, 9], [7, 0], [7, 1], [7, 2], [7, 3],
             [7, 4], [7, 5], [7, 6], [7, 7], [7, 8], [7, 9], [8, 0], [8, 1], [8, 2], [8, 3],
             [8, 4], [8, 5], [8, 6], [8, 7], [8, 8], [8, 9], [9, 0], [9, 1], [9, 2], [9, 3],
             [9, 4], [9, 5], [9, 6], [9, 7], [9, 8], [9, 9]],
     ' X ': [[0, 0], [1, 0], [2, 0], [3, 0]]}

我想将 [3,0] 从 ' X ' 移动到 ' 。 ' 并且不希望它被抛在后面。

我不希望这种硬编码,因为我想来回使用其他值。

移动值的条件:

如果一个值有特定的邻居

即[1,0]邻居是[0,0]、[2,0]、[0,1]等

所以如果它有一定数量的邻居或者我没有将它移动到 ' X ' 或 ' 。 '

这样我就可以绘制带有相应标签的坐标 --> 'X' 或 '.'

【问题讨论】:

  • 这个没有捷径。
  • dictionary['blue'].remove([0,1]); dictionary['red'].append([0,1])?
  • @Rawing OP 说没有硬编码删除。
  • @direprobs 是什么意思?他们想移动一个随机元素?
  • @Rawing 我不确定 OP 究竟期望什么,但问题指出 “无需对删除进行硬编码...”

标签: python python-3.x dictionary key


【解决方案1】:

我不确定您是否可以更改字典(这似乎是一项家庭作业),但我认为将坐标存储为键并将符号存储为值可能会更好字典。

# A dictionary comprehension to create a dict.
# Coords as the keys and '.' as the values.
board = {(x, y): '.' for x in range(10) for y in range(10)}

# To change the value just set some coords to 'X' or '.'.
board[(2, 3)] = 'X'

# Print the board.
for y in range(10):
    for x in range(10):
        print(board[(x, y)], end=' ')
    print()

【讨论】:

    【解决方案2】:

    也许您可以将元组用于内部列表,将 OrderedDict 用于外部:

    from collections import OrderedDict
    
    dictionary = {'blue': OrderedDict.fromkeys([(0, 1), (0, 2)]),
                  'red': OrderedDict.fromkeys([(0, 3), (0, 4)])}
    

    那么你仍然可以一一迭代外部字典的值:

    for pair in dictionary['blue']:
        print(pair)
    

    插入/删除一对平均需要 O(1)。

    【讨论】:

      【解决方案3】:

      只是一个在键之间移动值的通用函数。你应该处理函数外的异常。

      #! /usr/bin/env python
      # -*- coding: utf-8 -*-
      # vim:fenc=utf-8
      
      
      def moveBetweenKeys(dictionary, src_key, dest_key, value):
          """
          Move the value <value> from the key <src_key> to the key <dest_key>
          Raises ValueError if the value was not found in the list of the keywork
          <src_key>
          """
      
          src_list = dictionary[src_key]
          dest_list = dictionary[dest_key]
      
          if value not in src_list:
              raise ValueError("'%s' was not found in the list '%s'" % (value, src_key))
      
          src_list.remove(value)
          dest_list.append(value)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-14
        • 1970-01-01
        • 2020-01-03
        • 2021-03-04
        • 2018-12-05
        • 1970-01-01
        • 2017-07-07
        相关资源
        最近更新 更多