PYTHON:将字典中的值从一个键移动到另一个键

时间:2017-09-08 19:55:56

标签: python python-3.x dictionary key

如果我有一本字典说:

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'或'。'

3 个答案:

答案 0 :(得分:1)

我不确定你是否被允许更改字典(这似乎是一个家庭作业),但我认为将coords作为键和符号存储可能会更好。字典。

# 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()

答案 1 :(得分:0)

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

var myapp = angular.module('myapp', [])
  .controller('controller1', function($scope, $timeout) {
    $scope.title = "Controller 1";
    $scope.cache = false;
    $scope.records = [];
    $scope.display = false;
    $scope.loader = false;

    $scope.add = function() {
      console.log("adding");
      $scope.display = false;
      $scope.loader = true;
      $timeout(function() {
        $scope.records = [{
          "Name": "Alfreds Futterkiste",
          "Country": "Germany"
        }, {
          "Name": "Berglunds snabbköp",
          "Country": "Sweden"
        }, {
          "Name": "Centro comercial Moctezuma",
          "Country": "Mexico"
        }, {
          "Name": "Ernst Handel",
          "Country": "Austria"
        }, {
          "Name": "Lfds Atrfc",
          "Country": "Austria"
        }]
        $scope.display = true;
        $scope.loader = false;
      }, 1500);

    }
  });

然后你仍然可以逐个迭代外部字典的值:

class Phasor
{
public:
    double getSample() 
    { 
        double ret = phase/PI_z_2;
        phase = fmod(phase+phase_inc, TAU); //increment phase
        return ret;
    }
    void setSampleRate(double v) { sampleRate = v; calculateIncrement(); }
    void setFrequency(double v) { frequency = v; calculateIncrement(); }
    void Reset() { phase = 0.0; }
protected:
    void calculateIncrement() { phase_inc = TAU * frequency / sampleRate; }
    double sampleRate = 44100.0; 
    double frequency = 1.0;
    double phase = 0.0;
    double phase_inc = 0.0;
    const double TAU = 2*PI;
};

并且插入/删除一对将平均花费O(1)。

答案 2 :(得分:0)

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

#! /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)