[英]Best way to initialize key/value pairs in dictionaries
I'm working in Python 3 我正在使用Python 3
I have a board with cells that I have to store, each cell has a coordinate and needs to store 3 values. 我有一个带有要存储的单元格的板,每个单元格都有一个坐标,需要存储3个值。 I used a dictionary where the key equals the coordinates of the cell and the value is a list of 3 elements that stores the required values.
我使用了一个字典,其中的键等于单元格的坐标,并且该值是包含所需值的3个元素的列表。 Will it be fast to get and read these values when the board gets big and a lot of key/value pairs are stored?
当电路板变大并且存储了很多键/值对时,获取和读取这些值的速度会很快吗? Or what other method would be more efficient in terms of speed?
或者在速度方面,还有其他方法更有效吗?
Alex 亚历克斯
EDIT: 编辑:
The 3 values that I need for each cell are zero's or ones. 我需要每个单元格的3个值是零或一。 I have a tuple that contains the dimension of the board, a tuple that contains all the cell positions that should have a value of 1 as a first value, a frozenset that contains all the cell positions that should have a value of 1 as a second value and a tuple that contains all the cell positions that should have a value of 1 as a 3rd value:
我有一个包含板子尺寸的元组,一个包含所有应将第一个值设为1的单元格位置的元组,一个包含所有应将第二个值设为1的单元格的冻结集的元组值和包含所有单元格位置的元组,该位置应具有值为1的第三值:
tuple1 is a tuple that contains tuples that each represent the positions of the cells that have a 1 as first value, frozenset contains tuples that each represent the positions of the cells that have a 1 as second value, and tuple2 contains tuples that each represent the positions of the cells that have 1 as a third value (in the list) tuple1是一个元组,其中包含每个元组,每个元组代表以1作为第一值的单元格的位置; frozenset包含一个元组,每个元组代表以1作为第二值的单元格的位置,而tuple2包含一个元组,每个元组分别表示一个值。第三个值为1的单元格的位置(在列表中)
Code: 码:
board = {}
for row in range(dimension[0]):
for col in range(dimension[1]):
board[(row,col)] = [0,0,0] #default
for x in tuple1:
board[x][0] = 1
for x in frozenset:
board[x][1] = 1
for x in tuple2:
board[x][2] = 1
return board
Will it now be fast to look up keys and there values? 现在可以快速查找键并找到值了吗?
EDIT2: is this the fastest method to set up a dictionary like this? EDIT2:这是建立字典的最快方法吗?
numpy
numpy
If you're looking for performance, I think you should seriously consider numpy
with boolean values. 如果您正在寻找性能,我认为您应该认真考虑使用布尔值的
numpy
。 Your 2-D Board with tuples as values could be seen as a 3D Matrix: 具有元组作为值的2D板可以看作是3D矩阵:
import numpy as np
width, height = 5, 5
board = np.full((width,height,3), False, dtype = bool)
tuple1 = ((1,2), (3,4), (0,2))
for i,j in tuple1:
board[i,j,0] = True
print(board)
# [[[False False False]
# [False False False]
# [ True False False]
# [False False False]
# [False False False]]
# [[False False False]
# [False False False]
# [ True False False]
# [False False False]
# [False False False]]
# [[False False False]
# [False False False]
# [False False False]
# [False False False]
# [False False False]]
# [[False False False]
# [False False False]
# [False False False]
# [False False False]
# [ True False False]]
# [[False False False]
# [False False False]
# [False False False]
# [False False False]
# [False False False]]]
If you cannot use numpy
, you could simply use standard lists: 如果您不能使用
numpy
,则可以简单地使用标准列表:
width, height = 5, 5
board = [[[0, 0, 0] for j in range(width)] for i in range(height)]
tuple1 = ((1,2), (3,4), (0,2))
for i,j in tuple1:
board[i][j][0] = 1
from pprint import pprint
pprint(board)
[[[0, 0, 0], [0, 0, 0], [1, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [1, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [1, 0, 0]],
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]]
I don't think there's any advantage in using a dict. 我认为使用字典没有任何优势。 It would just shuffle your cells and make display harder.
它只会使您的单元格混乱,并使显示更加困难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.