python:如何读取2d动态列表的元素?

时间:2014-10-03 00:07:07

标签: python string list integer

我有以下数据

['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和列/行方式接近每个元素?我应该将这些数据正交化吗?

我从str到int步。我试图应用numpy.array或map,但它们没有用。

2 个答案:

答案 0 :(得分:3)

获得列表清单后

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]]

答案 1 :(得分:1)

关于“orthogonalize”,这可能是你要找的,

>>> 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