PYTHON:将字典中的值从一个键移动到另一个键
如果我有字典,说:PYTHON:将字典中的值从一个键移动到另一个键,python,python-3.x,dictionary,key,Python,Python 3.x,Dictionary,Key,如果我有字典,说: dictionary = {'blue': [ [0,1], [0,2] ] , 'red': [ [0,3], [0,4] ] } 如何将蓝色值(比如[0,1]移动到红色),而不在蓝色中留下副本,而不硬编码删除(因此,如果我有一个包含100个值的不同列表,并且希望将一个值从一个键移动到另一个键,它也应该以这种方式工作) 我希望输出如下所示: dictionary = {'blue': [ [0,2] ] , 'red': [ [0,3], [0,4], [0,1] ] }
dictionary = {'blue': [ [0,1], [0,2] ] , 'red': [ [0,3], [0,4] ] }
如何将蓝色
值(比如[0,1]
移动到红色
),而不在蓝色
中留下副本,而不硬编码删除(因此,如果我有一个包含100个值的不同列表,并且希望将一个值从一个键移动到另一个键,它也应该以这种方式工作)
我希望输出如下所示:
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”
这样我就可以用相应的标签-->'X'或'.'绘制坐标。也许您可以将元组用于内部列表,将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)。只是一个在键之间移动值的通用函数。您应该处理函数外部的异常
#! /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)
#/usr/bin/env python
#-*-编码:utf-8-*-
#vim:fenc=utf-8
def moveBetweenKeys(字典、src_键、dest_键、值):
"""
将值从关键点移动到关键点
如果在键工作列表中找不到该值,则引发ValueError
"""
src_list=字典[src_key]
dest\u list=字典[dest\u key]
如果值不在src_列表中:
raise VALUERROR(“%s”未在列表“%s”中找到(值,src_键))
src_list.remove(值)
dest_list.append(值)
我不确定是否允许您更改字典(这似乎是一项家庭作业),但我认为最好将坐标存储为键,将符号存储为字典的值
# 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()
此操作没有快捷方式。
dictionary['blue']。请删除([0,1]);字典['red'].追加([0,1])
?@Rawing OP没有硬编码删除就说了。@direprobs的意思是什么?他们想移动一个随机元素?@Rawing我不确定OP到底期望什么,但问题是“没有硬编码删除…”