python - 使用模式创建更大的列表列表

标签 python list pattern-matching

假设我有以下模式:

PATTERN = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]

我想用它来创建以下内容:

PATTERN | HORIZONTAL_MIRROR (PATTERN)
VERTICAL_MIRROR(PATTERN) | HORIZONTAL_MIRROR(VERTICAL_MIRROR(PATTERN))

换句话说:

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

除了从 PATTERN 复制每个元素之外,是否有一种可以在 Python 中使用的有效方法?

最佳答案

只是一个想法:

PATTERN = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
def patternify(l):
    for sl in l:
        yield sl+sl[::-1]
    for sl in l[::-1]:
        yield sl+sl[::-1]

list(patternify(PATTERN))
#output: [[1, 2, 3, 3, 2, 1], [4, 5, 6, 6, 5, 4], [7, 8, 9, 9, 8, 7], [7, 8, 9, 9, 8, 7], [4, 5, 6, 6, 5, 4], [1, 2, 3, 3, 2, 1]]

关于python - 使用模式创建更大的列表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14825101/

相关文章:

python - 将 matplotlib.pylab 的 imshow 与 python ide 一起使用?

python - TypeError:cv2_imshow()接受1个位置参数,但给出了2个:google colab cv相关问题

python - 将嵌套列表转换为字符串

java - 将具体类型列表转换为 Java 中的接口(interface)列表

python - list1对应list2的元素总和

swift - 是否可以在 swift 的 map 函数中使用模式匹配?

haskell - 如何解决haskell中的逻辑公式?

Python3 pandas 数据框使用 fillna(方法 ='bfill')和分组依据

python - 没有内置插件的选择排序 Python

java - 安卓 : Need Regex for complex String