从几个数组中提取列和行

时间:2019-12-29 21:14:07

标签: python

所以我的数据格式如下:

[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[stats.c, 1, 2, 3, 4, 5, 6, 7, **8**, 9, 10],
[stats.c, 1, 2, 3, 4, 5, 6, 7, **8**, 9, 10],
[stats.c, 1, 2, 3, 4, 5, 6, 7, **8**, 9, 10],
[stats.c, 1, 2, 3, 4, 5, 6, 7, **8**, 9, 10],

我需要从stats.c开始的每一行中提取第9列(我以粗体突出显示了我需要的)。 我不确定该怎么做。

理想情况下,我还需要循环执行此操作,因为我有许多具有相同格式的不同数组,需要从中提取相同的信息 我还有一个更复杂的情况是,我导入的某些数组没有前5行,因此它们仅包括开始时带有stats.c的行。 我希望这是有道理的。

2 个答案:

答案 0 :(得分:0)

  1. 重复您的列表
  2. 检查是否有“ stats.c”
  3. 如果我理解,您的数组大小并不总是10(3.1)或具有空条目(3.2)。因此,用If语句进行检查
  4. 获得价值
    for row in rows: #1
        if row[0] == 'stats.c': #2
            if len(row)  == 10: #3.1
                if not row[1]: #3.2
                    print(row[9]) #4

希望有帮助

答案 1 :(得分:0)

for example your input is 
data=[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[stats.c, 1, 2, 3, 4, 5, 6, 7, **8**, 9, 10],
[stats.c, 1, 2, 3, 4, 5, 6, 7, **8**, 9, 10],
[stats.c, 1, 2, 3, 4, 5, 6, 7, **8**, 9, 10],
[stats.c, 1, 2, 3, 4, 5, 6, 7, **8**, 9, 10]]

if I understand correctly, you need to only the 9 column of the row which contains stats.c at first index place.Assuming stats.c is a string, if now then other numbers are strings. Then you can do like this 
def get_row(data):  # 1
    result=[]        # 2
    for row in data:   # 3
        if row[0]=='stats.c':  #4
             result.append(row[8])  #5
    return result  #5

如果不确定stats.c是什么,则可以先使用type(stats.c)进行检查,然后在我的方法get_row的第4行中比较该类型,您将得到所需的内容。