将字符串列表分隔为列

时间:2017-02-08 02:21:44

标签: python string list python-3.6

import textwrap  

def get_coord(x,matrix):

    code = 'ADFGVX'  
    for i in range(len(matrix)):  
        for a in range(len(matrix[i])):  
            if matrix[i][a] == x:  
                return code[i] + code[a]  
    return -1, -1  

def encode(message, secret_alphabet, keyword):

    message = ''.join(message.split()).lower()   
    matrix = [secret_alphabet[i * 6:(i+1) * 6] for i in range(6)]  
    first = ''  
    lk = len(keyword)  
    for i in message:  
        first += get_coord(i, matrix)  
    first = textwrap.wrap(first, lk)    

encode("I am going", 
       "dhxmu4p3j6aoibzv9w1n70qkfslyc8tr5e2g",
       "cipher")

我有一个字符串列表,我需要将它们压缩在一起以创建列。我使用textwrap来创建此列表:

在texwrapping之后我得到了:

['FADVAG', 'XXDXFA', 'GDXX']

我需要输出看起来像:

['FXG', 'AXD', 'DDX', 'VXX', 'AF', 'GX']

我如何做到这一点?

1 个答案:

答案 0 :(得分:1)

使用itertools.zip_longeststr.join的一种方法:

>>> from itertools import zip_longest
>>> [''.join(item) for item in zip_longest('FADVAG', 'XXDXFA', 'GDXX', fillvalue='')]
['FXG', 'AXD', 'DDX', 'VXX', 'AF', 'GA']

但是,这并不会产生您想要的第三个和最后一个项目。在原帖中这是一个错误吗?

相关问题