Saturday, 15 January 2011

python - Creating a bigger List of Lists using a pattern -



python - Creating a bigger List of Lists using a pattern -

let's have next pattern:

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

and want utilize create following:

pattern | horizontal_mirror (pattern) vertical_mirror(pattern) | horizontal_mirror(vertical_mirror(pattern))

in other words:

[[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]]

is there efficient method can used in python apart copying each element pattern?

just idea:

pattern = [[1, 2, 3],[4, 5, 6],[7, 8, 9]] def patternify(l): sl in l: yield sl+sl[::-1] 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 list pattern-matching

No comments:

Post a Comment