猿问

如何从给定的矩阵创建字典

假设我得到一个类似于电话键盘的矩阵,例如-


1 2 3

4 5 6

7 8 9

  0

我如何生成以下字典而不实际输入它(元组不一定要排序)-


my_dict = {1: (1, 2, 4, 5), 2: (1, 2, 3, 4, 5, 6), 3: (2, 3, 5, 6),

           4: (1, 2, 4, 5, 7, 8), 5: (1, 2, 3, 4, 5, 6, 7, 8, 9),

           6: (2, 3, 5, 6, 8, 9), 7: (0, 4, 5, 7, 8),

           8: (0, 4, 5, 6, 7, 8, 9), 9: (0, 5, 6, 8, 9),

           0: (0, 7, 8, 9)}

这本字典基本上告诉我给定数字的所有相邻数字。例如,1 的相邻数字是 1、2、4、5。


编辑:理想情况下,矩阵应存储为列表列表:


[[1, 2, 3], [4, 5, 6], [7, 8, 9], [None, 0, None]]

我知道蛮力方法,但想知道一种有效地做到这一点的方法。


阿波罗的战车
浏览 300回答 3
3回答

萧十郎

假设键盘存储为位置字典(元组 x,y)和相应的数字作为值,您可以执行以下操作:import itertoolsdef distance(p1, p2):&nbsp; &nbsp; return sum((x1 - x2) ** 2 for x1, x2 in zip(p1, p2))def neighbors(positions, target):&nbsp; &nbsp; return [position for position in positions if distance(target, position) < 4]def numbers(kpad, keys):&nbsp; &nbsp; return tuple(sorted(map(kpad.get, keys)))values = list(range(1, 10)) + [0]positions = list(itertools.product([0, 1, 2], repeat=2)) + [(3, 1)]keypad = dict(zip(positions, values))result = {value: numbers(keypad, neighbors(keypad, key)) for key, value in keypad.items()}print(result)输出{0: (0, 7, 8, 9), 1: (1, 2, 4, 5), 2: (1, 2, 3, 4, 5, 6), 3: (2, 3, 5, 6), 4: (1, 2, 4, 5, 7, 8), 5: (1, 2, 3, 4, 5, 6, 7, 8, 9), 6: (2, 3, 5, 6, 8, 9), 7: (0, 4, 5, 7, 8), 8: (0, 4, 5, 6, 7, 8, 9), 9: (0, 5, 6, 8, 9)}这个想法是为每个位置获取相邻点的值。更新要将列表 a 转换为键盘字典,您可以执行以下操作:data = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [None, 0, None]]keypad = {(i, j): value for i, sub in enumerate(data) for j, value in enumerate(sub) if value is not None}其余方法保持不变。

杨魅力

您可以使用生成器函数:import redef all_adjacent(_c, _graph):&nbsp; &nbsp;_funcs = [lambda x,y:(x+1, y), lambda x,y:(x+1, y+1), lambda x,y:(x+1, y-1), lambda x,y:(x, y+1), lambda x,y:(x-1, y+1), lambda x,y:(x-1, y-1), lambda x,y:(x, y-1), lambda x,y:(x-1, y)]&nbsp; &nbsp;yield _graph[_c[0]][_c[1]]&nbsp; &nbsp;for func in _funcs:&nbsp; &nbsp; &nbsp;a, b = func(*_c)&nbsp; &nbsp; &nbsp;try:&nbsp; &nbsp; &nbsp; &nbsp;if a >= 0 and b >= 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_val = _graph[a][b]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if _val != '&nbsp; ':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;yield _val&nbsp; &nbsp; &nbsp;except:&nbsp; &nbsp; &nbsp; &nbsp;passs = """1 2 34 5 67 8 9&nbsp; 0&nbsp;&nbsp;"""new_data = [re.findall('\d+|\s{2,}', i) for i in filter(None, s.split('\n'))]final_results = {c:list(all_adjacent((i, d), new_data)) for i, a in enumerate(new_data) for d, c in enumerate(a) if c != '&nbsp; '}_result = {int(a):tuple(sorted(map(int, b))) for a, b in final_results.items()}输出:{1: (1, 2, 4, 5), 2: (1, 2, 3, 4, 5, 6), 3: (2, 3, 5, 6), 4: (1, 2, 4, 5, 7, 8), 5: (1, 2, 3, 4, 5, 6, 7, 8, 9), 6: (2, 3, 5, 6, 8, 9), 7: (0, 4, 5, 7, 8), 8: (0, 4, 5, 6, 7, 8, 9), 9: (0, 5, 6, 8, 9), 0: (0, 7, 8, 9)}编辑:将矩阵存储为列表列表:import redef all_adjacent(_c, _graph):&nbsp; _funcs = [lambda x,y:(x+1, y), lambda x,y:(x+1, y+1), lambda x,y:(x+1, y-1), lambda x,y:(x, y+1), lambda x,y:(x-1, y+1), lambda x,y:(x-1, y-1), lambda x,y:(x, y-1), lambda x,y:(x-1, y)]&nbsp; yield _graph[_c[0]][_c[1]]&nbsp; for func in _funcs:&nbsp; &nbsp; a, b = func(*_c)&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; if a >= 0 and b >= 0:&nbsp; &nbsp; &nbsp; &nbsp; _val = _graph[a][b]&nbsp; &nbsp; &nbsp; &nbsp; if _val is not None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield _val&nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp; passnew_data = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [None, 0, None]]final_results = {c:(i, d) for i, a in enumerate(new_data) for d, c in enumerate(a) if c is not None}_result = {int(a):tuple(map(int, all_adjacent(b, new_data))) for a, b in final_results.items()}输出:{1: (1, 4, 5, 2), 2: (2, 5, 6, 4, 3, 1), 3: (3, 6, 5, 2), 4: (4, 7, 8, 5, 2, 1), 5: (5, 8, 9, 7, 6, 3, 1, 4, 2), 6: (6, 9, 8, 2, 5, 3), 7: (7, 0, 8, 5, 4), 8: (8, 0, 9, 6, 4, 7, 5), 9: (9, 0, 5, 8, 6), 0: (0, 9, 7, 8)}

冉冉说

假设您的矩阵如下所示:m = [&nbsp; &nbsp; [1, 2, 3],&nbsp; &nbsp; [4, 5, 6],&nbsp; &nbsp; [7, 8, 9],&nbsp; &nbsp; [None, 0, None]]您可以蛮力解决您的解决方案,只需遍历矩阵并使用 defaultdict 收集相邻单元格:from collections import defaultdictfrom pprint import pprintm = [&nbsp; &nbsp; [1, 2, 3],&nbsp; &nbsp; [4, 5, 6],&nbsp; &nbsp; [7, 8, 9],&nbsp; &nbsp; [None, 0, None]]rows = len(m)cols = len(m[0])# adjacent cellsadjacency = [(i, j) for i in (-1, 0, 1) for j in (-1, 0, 1) if not i == j == 0]d = defaultdict(list)for r in range(rows):&nbsp; &nbsp; for c in range(cols):&nbsp; &nbsp; &nbsp; &nbsp; cell = m[r][c]&nbsp; &nbsp; &nbsp; &nbsp; if cell is not None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d[cell].append(cell)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for x, y in adjacency:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if 0 <= r + x < rows and 0 <= c + y < cols:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adjacent = m[r + x][c + y]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if adjacent is not None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d[cell].append(adjacent)# print sorted adjacent cellspprint({k: tuple(sorted(v)) for k, v in d.items()})这给出了排序的相邻单元格的字典:{0: (0, 7, 8, 9),&nbsp;1: (1, 2, 4, 5),&nbsp;2: (1, 2, 3, 4, 5, 6),&nbsp;3: (2, 3, 5, 6),&nbsp;4: (1, 2, 4, 5, 7, 8),&nbsp;5: (1, 2, 3, 4, 5, 6, 7, 8, 9),&nbsp;6: (2, 3, 5, 6, 8, 9),&nbsp;7: (0, 4, 5, 7, 8),&nbsp;8: (0, 4, 5, 6, 7, 8, 9),&nbsp;9: (0, 5, 6, 8, 9)}
随时随地看视频慕课网APP

相关分类

Python
我要回答