高级数组连接python

时间:2016-10-15 13:01:51

标签: python arrays pygame concatenation

假设我有四个多维数组 -

a = [["a","a","a"],
    ["a","a","a"],
    ["a","a","a"]]
b = [["b","b","b"],
    ["b","b","b"],
    ["b","b","b"]]
c = [["c","c","c"],
    ["c","c","c"],
    ["c","c","c"]]
d = [["d","d","d"],
    ["d","d","d"],
    ["d","d","d"]]

我希望将它们组合成一个像

这样的数组
total = [["a","a","a","b","b","b"],
        ["a","a","a","b","b","b"],
        ["a","a","a","b","b","b"], 
        ["c","c","c","d","d","d"],
        ["c","c","c","d","d","d"],
        ["c","c","c","d","d","d"]]

我该怎么做?

我正在为笨重的地图生成

这样做

4 个答案:

答案 0 :(得分:2)

也许是这样的:

top = list(x+y for x,y in zip(a,b))
bottom = list(x+y for x,y in zip(c,d))
total = top + bottom

for r in total: print(r)

输出:

['a', 'a', 'a', 'b', 'b', 'b']
['a', 'a', 'a', 'b', 'b', 'b']
['a', 'a', 'a', 'b', 'b', 'b']
['c', 'c', 'c', 'd', 'd', 'd']
['c', 'c', 'c', 'd', 'd', 'd']
['c', 'c', 'c', 'd', 'd', 'd']

答案 1 :(得分:2)

你可以使用一行指令,混合列表理解,zip指令和列表串联+

[aa+bb for aa,bb in zip(a,b)] + [cc+dd for cc,dd in zip(c,d)]

整个代码

a = [["a","a","a"],
    ["a","a","a"],
    ["a","a","a"]]
b = [["b","b","b"],
    ["b","b","b"],
    ["b","b","b"]]
c = [["c","c","c"],
    ["c","c","c"],
    ["c","c","c"]]
d = [["d","d","d"],
    ["d","d","d"],
    ["d","d","d"]]
result = [aa+bb for aa,bb in zip(a,b)] + [cc+dd for cc,dd in zip(c,d)]

答案 2 :(得分:2)

假设您的地图布局比2x2块更复杂:

from itertools import chain
from pprint import pprint


a = [["a","a","a"],
     ["a","a","a"],
     ["a","a","a"]]
b = [["b","b","b"],
     ["b","b","b"],
     ["b","b","b"]]
c = [["c","c","c"],
     ["c","c","c"],
     ["c","c","c"]]
d = [["d","d","d"],
     ["d","d","d"],
     ["d","d","d"]]
e = [["e","e","e"],
     ["e","e","e"],
     ["e","e","e"]]
f = [["f","f","f"],
     ["f","f","f"],
     ["f","f","f"]]

layouts = [
    ((a, b), (c, d)),
    ((a, b, c), (d, e, f)),
    ((a, b), (c, d), (e, f)),
]

for layout in layouts:
    total = [list(chain(*row)) for lrow in layout for row in zip(*lrow)]
    pprint(total)

<强>输出:

[['a', 'a', 'a', 'b', 'b', 'b'],
 ['a', 'a', 'a', 'b', 'b', 'b'],
 ['a', 'a', 'a', 'b', 'b', 'b'],
 ['c', 'c', 'c', 'd', 'd', 'd'],
 ['c', 'c', 'c', 'd', 'd', 'd'],
 ['c', 'c', 'c', 'd', 'd', 'd']]

[['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'],
 ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'],
 ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'],
 ['d', 'd', 'd', 'e', 'e', 'e', 'f', 'f', 'f'],
 ['d', 'd', 'd', 'e', 'e', 'e', 'f', 'f', 'f'],
 ['d', 'd', 'd', 'e', 'e', 'e', 'f', 'f', 'f']]

[['a', 'a', 'a', 'b', 'b', 'b'],
 ['a', 'a', 'a', 'b', 'b', 'b'],
 ['a', 'a', 'a', 'b', 'b', 'b'],
 ['c', 'c', 'c', 'd', 'd', 'd'],
 ['c', 'c', 'c', 'd', 'd', 'd'],
 ['c', 'c', 'c', 'd', 'd', 'd'],
 ['e', 'e', 'e', 'f', 'f', 'f'],
 ['e', 'e', 'e', 'f', 'f', 'f'],
 ['e', 'e', 'e', 'f', 'f', 'f']]

答案 3 :(得分:0)

由于您正在讨论数组,使用numpy数组以及hstack()vstack()

import numpy as np

ab = np.hstack((np.array(a),np.array(b)))
cd = np.hstack((np.array(c),np.array(d)))

print np.vstack((ab,cd))

哪个引导你:

[['a' 'a' 'a' 'b' 'b' 'b']
 ['a' 'a' 'a' 'b' 'b' 'b']
 ['a' 'a' 'a' 'b' 'b' 'b']
 ['c' 'c' 'c' 'd' 'd' 'd']
 ['c' 'c' 'c' 'd' 'd' 'd']
 ['c' 'c' 'c' 'd' 'd' 'd']]

或者,使用concatenate()

ab = np.concatenate((np.array(a),np.array(b)),axis=1)
cd = np.concatenate((np.array(c),np.array(d)),axis=1)

print np.concatenate((ab,cd))