Search
# Inicializace pole a = [0] * 5 # Jak správně zkopírovat pole? b = a c = a[:] d = list(a) a[3] = 3 b[0] = -5 c[4] = 4
# Inicializace 2D pole přímo a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Inicializace po řádcích f = [] for i in range(3): f.append([i] * 3) # Inicializace po řádcích ve zkráceném zápisu g = [[i] * 3 for i in range(3)] # Jak správně zkopírovat pole? b = a c = a[:] d = list(b) e = [ r[:] for r in a ] f = [ list(a[i]) for i in range(len(a))] a[0][0] = -1 b[0][1] = -2 c[0],c[1]=c[1],c[0] d[1][0] = -3 e[1][1] = -4
copy
deepcopy
import copy d = copy.deepcopy(b)
a = [[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, 0, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 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, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ]
[[0]*len(a[0]) for i in a]
import time time.sleep(0.5)
''.join('X' if i!=0 else ' ' for i in x)
array2png(a, “soubor.png”)
#importujeme knihovny pro vykresleni matic do obrazku import numpy as np import matplotlib.pyplot as plt from matplotlib.patches import Rectangle from matplotlib import cm def array2png(arr, output_filename, cell_px=30, colormap='viridis', show_colorbar=True, add_grid=True): """ Draw a 2D array as colored rectangles (one per cell) and save to PNG. Parameters ---------- arr : list of lists or 2D numpy array Values of any numeric range; they are normalized to the colormap. output_filename : str Path of the output PNG (e.g. 'grid.png'). cell_px : int Width and height of each cell in pixels (default 30). colormap : str Name of a matplotlib colormap ('viridis', 'Greys', 'hot', ...). show_colorbar : bool Attach a colorbar showing the value→color mapping. add_grid : bool Draw thin dark borders around every cell. """ arr = np.array(arr, dtype=float) n_rows, n_cols = arr.shape dpi = 100 # ---- Figure size = grid pixels + optional colorbar space ---- grid_w = n_cols * cell_px grid_h = n_rows * cell_px cbar_w = int(0.25 * cell_px) + 30 if show_colorbar else 0 # bar + padding fig_w = (grid_w + cbar_w) / dpi fig_h = grid_h / dpi fig, ax = plt.subplots(figsize=(fig_w, fig_h), dpi=dpi) # ---- Value → RGBA color via colormap ---- vmin, vmax = arr.min(), arr.max() norm = plt.Normalize(vmin=vmin, vmax=vmax) cmap = cm.get_cmap(colormap) # ---- Draw one rectangle per cell ---- for r in range(n_rows): for c in range(n_cols): color = cmap(norm(arr[r, c])) rect = Rectangle( (c * cell_px, r * cell_px), # (x, y) in pixels cell_px, cell_px, facecolor=color, edgecolor='black' if add_grid else 'none', linewidth=0.8 if add_grid else 0, ) ax.add_patch(rect) # ---- Canvas setup (pixel units, y pointing down) ---- ax.set_xlim(0, grid_w) ax.set_ylim(grid_h, 0) # row 0 at the top ax.set_aspect('equal') ax.set_xticks([]) ax.set_yticks([]) ax.set_frame_on(False) # ---- Optional colorbar ---- if show_colorbar: sm = cm.ScalarMappable(norm=norm, cmap=cmap) cb = fig.colorbar(sm, ax=ax, fraction=0.3, shrink=0.6, pad=0.08) cb.set_label('cell value') fig.savefig(output_filename, dpi=dpi, bbox_inches='tight', pad_inches=0.05) plt.close(fig) print(f"Saved: {output_filename} ({grid_w}x{grid_h} px grid)")
$$ (f * g)(i, j) = \sum_{m=0}^{M-1} \sum_{n=0}^{N-1} f(i + m,\, j + n) \cdot g(m, n) $$
""" obrazky cislic 0...9 ve formatu 7x5 (radky x sloupce). 1 predstavuje cerny pixel, 0 predstavuje bile pozadni """ Digit0 = [ [0, 1, 1, 1, 0], [1, 0, 0, 0, 1], [1, 0, 0, 1, 1], [1, 0, 1, 0, 1], [1, 1, 0, 0, 1], [1, 0, 0, 0, 1], [0, 1, 1, 1, 0] ] Digit1 = [ [0, 0, 1, 0, 0], [0, 1, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 1, 1, 1, 0] ] Digit2 = [ [0, 1, 1, 1, 0], [1, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 0, 0, 0], [1, 1, 1, 1, 1] ] Digit3 = [ [1, 1, 1, 1, 0], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 1, 1, 0], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [1, 1, 1, 1, 0] ] Digit4 = [ [0, 0, 0, 1, 0], [0, 0, 1, 1, 0], [0, 1, 0, 1, 0], [1, 0, 0, 1, 0], [1, 1, 1, 1, 1], [0, 0, 0, 1, 0], [0, 0, 0, 1, 0] ] Digit5 = [ [1, 1, 1, 1, 1], [1, 0, 0, 0, 0], [1, 1, 1, 1, 0], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [1, 0, 0, 0, 1], [0, 1, 1, 1, 0] ] Digit6 = [ [0, 0, 1, 1, 0], [0, 1, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 1, 1, 0], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [0, 1, 1, 1, 0] ] Digit7 = [ [1, 1, 1, 1, 1], [0, 0, 0, 0, 1], [0, 0, 0, 1, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0] ] Digit8 = [ [0, 1, 1, 1, 0], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [0, 1, 1, 1, 0], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [0, 1, 1, 1, 0] ] Digit9 = [ [0, 1, 1, 1, 0], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [0, 1, 1, 1, 1], [0, 0,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