从具有相同索引的矩阵生成列表

时间:2019-07-13 11:22:43

标签: python python-3.x list

我有一个矩阵,其中包含'N'个列表,每个列表具有3个字符串,每个字符串具有'M'个字符。

例如,N = 3(3个列表)和M = 3(所有字符串均为3个字符):

M = [['123', '456', '789'], ['abc', 'def', 'ghi'], ['ABC', 'DEF', 'GHI']]

我需要生成具有相同索引值的列表,让我向您展示输出:

['1aA', '2bB', '3cC']
['1aD', '2bE', '3cF']
['1aG', '2bH', '3cI']

['1dA', '2eB', '3fC']
['1dD', '2eE', '3fF']
['1dG', '2eH', '3fI']

['1gA', '2hB', '3iC']
['1gD', '2hE', '3iF']
['1gG', '2hH', '3iI']

['4aA', '5bB', '6cC']
['4aD', '5bE', '6cF']
['4aG', '5bH', '6cI']

['4dA', '5eB', '6fC']
['4dD', '5eE', '6fF']
['4dG', '5eH', '6fI']

['4gA', '5hB', '6iC']
['4gD', '5hE', '6iF']
['4gG', '5hH', '6iI']

['7aA', '8bB', '9cC']
['7aD', '8bE', '9cF']
['7aG', '8bH', '9cI']

['7dA', '8eB', '9fC']
['7dD', '8eE', '9fF']
['7dG', '8eH', '9fI']

['7gA', '8hB', '9iC']
['7gD', '8hE', '9iF']
['7gG', '8hH', '9iI']

您有什么建议吗?

2 个答案:

答案 0 :(得分:2)

解决此问题的一种方法是使用itertools.product获取所有字符串的笛卡尔乘积,对于每个乘积,选择具有相同索引的项目,然后使用zip从其中生成一个字符串

from itertools import product

M = [['123', '456', '789'], ['abc', 'def', 'ghi'], ['ABC', 'DEF', 'GHI']]

output = []

#Take product of all elements
for lst in product(*M):

    #Take the characters at the same index and make a string out of them via zip
    res = [''.join(item) for item in zip(*lst)]

    #Append that list to final output
    output.append(res)

for item in output:
    print(item)

输出将为

['1aA', '2bB', '3cC']
['1aD', '2bE', '3cF']
['1aG', '2bH', '3cI']
['1dA', '2eB', '3fC']
['1dD', '2eE', '3fF']
['1dG', '2eH', '3fI']
['1gA', '2hB', '3iC']
['1gD', '2hE', '3iF']
['1gG', '2hH', '3iI']
['4aA', '5bB', '6cC']
['4aD', '5bE', '6cF']
['4aG', '5bH', '6cI']
['4dA', '5eB', '6fC']
['4dD', '5eE', '6fF']
['4dG', '5eH', '6fI']
['4gA', '5hB', '6iC']
['4gD', '5hE', '6iF']
['4gG', '5hH', '6iI']
['7aA', '8bB', '9cC']
['7aD', '8bE', '9cF']
['7aG', '8bH', '9cI']
['7dA', '8eB', '9fC']
['7dD', '8eE', '9fF']
['7dG', '8eH', '9fI']
['7gA', '8hB', '9iC']
['7gD', '8hE', '9iF']
['7gG', '8hH', '9iI']

或者使用列表理解的单线方式

output = [[''.join(item) for item in zip(*lst)] for lst in product(*M)]

答案 1 :(得分:0)

使用itertools.product()zip(),我们可以将所需的元素组合在一起,然后从中生成所有特定的字符串(“单元格内容”),然后将它们“重塑”为矩阵。

尝试一下:

from itertools import product

M = [['123', '456', '789'], ['abc', 'def', 'ghi'], ['ABC', 'DEF', 'GHI']]

nums = zip(*M[0])
lowers = zip(*M[1])
uppers = zip(*M[2])
groups = zip(nums, lowers, uppers)
strings = [[''.join(chars) for chars in [*product(*g)]] for g in groups ]
string_chunks = [list(chunk) for chunk in zip(*strings)]
result = [string_chunks[i:i + len(M)] for i in range(0, len(string_chunks), len(M))]
for matrix in result:
    for row in matrix:
        print(row)
    print()

输出:

['1aA', '2bB', '3cC']
['1aD', '2bE', '3cF']
['1aG', '2bH', '3cI']

['1dA', '2eB', '3fC']
['1dD', '2eE', '3fF']
['1dG', '2eH', '3fI']

['1gA', '2hB', '3iC']
['1gD', '2hE', '3iF']
['1gG', '2hH', '3iI']

['4aA', '5bB', '6cC']
['4aD', '5bE', '6cF']
['4aG', '5bH', '6cI']

['4dA', '5eB', '6fC']
['4dD', '5eE', '6fF']
['4dG', '5eH', '6fI']

['4gA', '5hB', '6iC']
['4gD', '5hE', '6iF']
['4gG', '5hH', '6iI']

['7aA', '8bB', '9cC']
['7aD', '8bE', '9cF']
['7aG', '8bH', '9cI']

['7dA', '8eB', '9fC']
['7dD', '8eE', '9fF']
['7dG', '8eH', '9fI']

['7gA', '8hB', '9iC']
['7gD', '8hE', '9iF']
['7gG', '8hH', '9iI']