Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将字符串列表拆分为列_Python_String_List_Python 3.6 - Fatal编程技术网

Python 将字符串列表拆分为列

Python 将字符串列表拆分为列,python,string,list,python-3.6,Python,String,List,Python 3.6,我有一个字符串列表,需要将这些字符串压缩在一起以创建列。我使用textwrap创建此列表: 首先,我得到: 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:

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

首先,我得到:

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")
我需要我的输出看起来像:

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

如何实现这一点?

使用
itertools.zip\u longest
str.join
的一种方法:

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

然而,这并不会产生你想要的第三件也是最后一件物品。这是原始帖子中的一个错误吗?

您需要包括一些代码以及如何到达第一个数组。
'DDD'
是一个打字错误,应该是
'DDX'
不知道为什么
'GA'
会在必填列表中消失:)。奇怪的是,
'AX'
从某处出现。很抱歉我打字不好,我已经打字好几个小时了哈哈,我会试试看的,不过谢谢有没有办法我可以通过一个列表来压缩最长的文件并得到所需的输出?@chasellis你可以使用解包
压缩最长的文件(*你的列表,fillvalue='')
(请注意前导的
*
,它将打开列表)。
>>> from itertools import zip_longest
>>> [''.join(item) for item in zip_longest('FADVAG', 'XXDXFA', 'GDXX', fillvalue='')]
['FXG', 'AXD', 'DDX', 'VXX', 'AF', 'GA']