簡體   English   中英

Python:如何讀取2D動態列表的元素?

[英]python: how can I read elements of 2d dynamic lists?

我有以下數據

['1', '4', '4', '244', '263', '704', '952']
['2', '4', '4', '215', '172', '305', '33']
['3', '4', '4', '344', '279', '377', '1945']
['4', '4', '4', '66', '79', '169', '150']
['5', '4', '3', '16', '22', '247']
['6', '4', '4', '17', '154', '93', '309']
['7', '3', '2', '233', '311']
['8', '3', '1', '15']
['9', '3', '2', '55', '102']
..... 

共200行,由50行數據塊中的4行組成。 類似格式的數據塊在文件中重復4次。 如何索引此數據,並在每個塊內按blcok-wise和column / line wise方式接近每個元素? 我是否需要正交化這些數據?

我被困在從str到int的一步。 我嘗試應用numpy.array或map,但是它們不起作用。

一旦有了清單清單

l = [['1', '4', '4', '244', '263', '704', '952'],
     ['2', '4', '4', '215', '172', '305', '33'],
     ['3', '4', '4', '344', '279', '377', '1945'],
     ['4', '4', '4', '66', '79', '169', '150'],
     ['5', '4', '3', '16', '22', '247'],
     ['6', '4', '4', '17', '154', '93', '309'],
     ['7', '3', '2', '233', '311'],
     ['8', '3', '1', '15'],
     ['9', '3', '2', '55', '102']]

在列表理解中使用map

intList = [map(int, sublist) for sublist in l]

結果

>>> intList
[[1, 4, 4, 244, 263, 704, 952],
 [2, 4, 4, 215, 172, 305, 33],
 [3, 4, 4, 344, 279, 377, 1945],
 [4, 4, 4, 66, 79, 169, 150],
 [5, 4, 3, 16, 22, 247],
 [6, 4, 4, 17, 154, 93, 309],
 [7, 3, 2, 233, 311],
 [8, 3, 1, 15],
 [9, 3, 2, 55, 102]]

關於“正交化”,也許這就是您想要的,

>>> li = [[1, 4, 4, 244, 263, 704, 952], [2, 4, 4, 215, 172, 305, 33], [3, 4, 4, 344, 279, 377, 1945], [4, 4, 4, 66, 79, 169, 150], [5, 4, 3, 16, 22, 247], [6, 4, 4, 17, 154, 93, 309], [7, 3, 2, 233, 311], [8, 3, 1, 15], [9, 3, 2, 55, 102]]
>>> def orthogonalize(li):
    max_col = max(len(x) for x in li) + 1
    for l in li:
        for i in range(max_col-len(l)):
            l.append(0)


>>> orthogonalize(li)
>>> li
[[1, 4, 4, 244, 263, 704, 952, 0], [2, 4, 4, 215, 172, 305, 33, 0], [3, 4, 4, 344, 279, 377, 1945, 0], [4, 4, 4, 66, 79, 169, 150, 0], [5, 4, 3, 16, 22, 247, 0, 0], [6, 4, 4, 17, 154, 93, 309, 0], [7, 3, 2, 233, 311, 0, 0, 0], [8, 3, 1, 15, 0, 0, 0, 0], [9, 3, 2, 55, 102, 0, 0, 0]]
>>> li[5]
[6, 4, 4, 17, 154, 93, 309, 0]
>>> li[6]
[7, 3, 2, 233, 311, 0, 0, 0]
>>> 

根據您的其他要求進行編輯:

由於我的理解局限性,我必須做一個假設,即您的數據是2d的,但是您想使用block#,line#和column#在3d中訪問它,因為我無法在您的數據中找到塊標識符。 您已經處理了數據

>>> def getData(data, block, line, column = None):
    """
     Index start from 0 for block, line and column
     getData(data, 0,1,1)
       => block# is 0 it will be processed as is
       => it will read value of line#1, column#1 
     getData(data, 1,1,1)
       => block# is 1 it will be convert to line number = 50*(block)+line
       => it will read value of line#51, column#1


    """
    if column is None:
        return data[50*(block)+line]
    else:    
        return data[50*(block)+line][column]

>>> d = [[1, 4, 4, 244, 263, 704, 952, 0],
[2, 4, 4, 215, 172, 305, 33, 0],
[3, 4, 4, 344, 279, 377, 1945, 0],
............
[51, 4, 4, 244, 263, 704, 952, 0],
[52, 4, 4, 215, 172, 305, 33, 0],
[53, 4, 4, 344, 279, 377, 1945, 0],
[54, 4, 4, 66, 79, 169, 150, 0],
[55, 4, 3, 16, 22, 247, 0, 0],
[56, 4, 4, 17, 154, 93, 309, 0],
[57, 3, 2, 233, 311, 0, 0, 0],
[58, 3, 1, 15, 0, 0, 0, 0],
[59, 3, 2, 55, 102, 0, 0, 0],
[60, 4, 4, 304, 209, 307, 945, 0]]
>>> getData(d, 0, 0)
[1, 4, 4, 244, 263, 704, 952, 0]
>>> getData(d, 0, 0, 3)
244
>>> getData(d, 1, 0)
[51, 4, 4, 244, 263, 704, 952, 0]
>>> getData(d, 1, 0, 4)
263

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM