如何除以索引并在嵌套列表中添加项目以使其嵌套?

时间:2016-11-11 09:57:21

标签: python list nested

我的问题是,如何按索引划分项目并添加字符串特定值 在嵌套列表中。

我的清单:

lst = [['  21693282.469 7                  -4963125.899 7  -3821950.54648  21693275.40648\n',
    '  20789781.031 7                    887006.789 7    698075.62748  20789776.77048\n',
    '  24667814.375 5  24667811.441 8   1051991.202 5    827710.336 8  24667810.98847\n',
    '  21414305.883 6  21414301.563 9  -5293000.520 6  -4102616.060 9  21414301.17248\n',
    '  23395450.500 6                   1349998.701 6   1080794.20346  23395447.42246\n',
     '  20965956.617 8                  -3636447.948 8  -2813703.22949  20965951.97349\n'],
     ['  20670086.656 7                   2718596.518 7   2116872.80448  20670081.07848\n',
     '  24222343.500 3                   2146415.760 3   1607697.95946  24222340.25446\n',
     '  22829139.453 6                   1683633.646 6   1300012.93847  22829132.80147\n',
     '  22934656.609 6                   1700166.043 6   1314411.856 7  22934663.711 7\n',
     '  20055874.828 9                    267080.471 9    212506.020 9  20055882.121 9\n',
     '  22774080.570 7                   1762178.392 7   1346501.808 8  22774088.434 8\n',
     '  20194290.688 8                  -2867460.044 8  -2213132.457 9  20194298.629 9\n',
     '  21679624.156 7                   1345827.111 7   1067174.299 8  21679631.973 8\n']]

我的代码在这里:

result=[] 

def extract_line():

    for list in lst:
        for j in list:
            for i in range(0,len(j)-1,16):
                num = j[i:i+16].strip()

                result.append(num if num else 'None')
    yield result

for result in extract_line():
    print result

我只能得到一个没有嵌套的列表

我希望列表保持嵌套列表,如下所示:

[['21693282.469 7', 'None', '-4963125.899 7', '-3821950.54648', '21693275.40648',
 '20789781.031 7', 'None', '887006.789 7', '698075.62748', '20789776.77048',
 '24667814.375 5', '24667811.441 8', '1051991.202 5', '827710.336 8', '24667810.98847',
 '21414305.883 6', '21414301.563 9', '-5293000.520 6', '-4102616.060 9', '21414301.17248',
 '23395450.500 6', 'None', '1349998.701 6', '1080794.20346', '23395447.42246',
 '20965956.617 8', 'None', '-3636447.948 8', '-2813703.22949', '20965951.97349'],
 ['20670086.656 7', 'None', '2718596.518 7', '2116872.80448', '20670081.07848',
 '24222343.500 3', 'None', '2146415.760 3', '1607697.95946', '24222340.25446',
 '22829139.453 6', 'None', '1683633.646 6', '1300012.93847', '22829132.80147',
 '22934656.609 6', 'None', '1700166.043 6', '1314411.856 7', '22934663.711 7',
 ........ '21679631.973 8']]

我很抱歉

因为我还没有习惯使用这个网页  我编辑的问题比以前更容易阅读

3 个答案:

答案 0 :(得分:0)

快速解决方案:如果myList是您想要嵌套的列表,只需写下myList = [myList]

答案 1 :(得分:0)

首先,不要使用list(它是一个类)作为变量名。 Python不会说什么,但它会在以后咬你(硬)。

(与dict,file,...相同)。如有疑问,请在任何事情之前添加my_ 其次,您似乎不想返回项目列表,而是返回包含单个项目的列表。    最简单的解决方案不是产生结果,而是

yield [result]

哪会返回您想要的列表(您的列表是列表中唯一的元素)

答案 2 :(得分:0)

下面我发布了解决方案。引入子列表,以保持列表嵌套。

lst = [['  21693282.469 7                  -4963125.899 7  -3821950.54648  21693275.40648\n',
    '  20789781.031 7                    887006.789 7    698075.62748  20789776.77048\n',
    '  24667814.375 5  24667811.441 8   1051991.202 5    827710.336 8  24667810.98847\n',
    '  21414305.883 6  21414301.563 9  -5293000.520 6  -4102616.060 9  21414301.17248\n',
    '  23395450.500 6                   1349998.701 6   1080794.20346  23395447.42246\n',
     '  20965956.617 8                  -3636447.948 8  -2813703.22949  20965951.97349\n'],
     ['  20670086.656 7                   2718596.518 7   2116872.80448  20670081.07848\n',
     '  24222343.500 3                   2146415.760 3   1607697.95946  24222340.25446\n',
     '  22829139.453 6                   1683633.646 6   1300012.93847  22829132.80147\n',
     '  22934656.609 6                   1700166.043 6   1314411.856 7  22934663.711 7\n',
     '  20055874.828 9                    267080.471 9    212506.020 9  20055882.121 9\n',
     '  22774080.570 7                   1762178.392 7   1346501.808 8  22774088.434 8\n',
     '  20194290.688 8                  -2867460.044 8  -2213132.457 9  20194298.629 9\n',
     '  21679624.156 7                   1345827.111 7   1067174.299 8  21679631.973 8\n']]


result=[] 

def extract_line():

    for list in lst:
    print 'list:',list
    sublist=[]
        for j in list:    
            for i in range(0,len(j)-1,16):
                num = j[i:i+16].strip()

                sublist.append(num if num else 'None')
        result.append(sublist)
    yield result

for result in extract_line():
    print '\n\n\n'
    print result