简体   繁体   English

将二维数组处理成 Python 中的列表列表

[英]Processing 2D Array into a List of Lists in Python

I want to process this 2D array ('more') in order to get a list of lists ('rm'), so that the final result would look like this:我想处理这个二维数组('more')以获得列表列表('rm'),以便最终结果如下所示:

     Month1   Month2   Month3
rm = [[3,       [4],    [5]]    
       2, 
       1, 
       0], 

where the first column of 'more' corresponds to the Month of data collection at the stations.其中“更多”的第一列对应于车站数据收集的月份。 The final output above has 3 lists for the respective 3 Months of data collection, after removing the -999 values.在删除 -999 值之后,上面的最终 output 有 3 个列表,分别用于 3 个月的数据收集。 Note that in the original array data for one month could spread to several rows, but they all end up in one single list in the final output.请注意,在原始数组中,一个月的数据可能会分布到几行,但它们最终都会在最终 output 中的一个列表中结束。

I am new to python, so I know I am not doing a good job properly framing this.我是 python 的新手,所以我知道我没有很好地正确构建这个框架。 I appreciate your help on this.感谢您在这方面的帮助。

rm = []
       Month  Station1 Station2 Station3
more = [[1,    -999,      3,        2], 
        [1,       1,      0,     -999], 
        [2,       4,   -999,     -999],
        [3,    -999,   -999,        5]]

for i in range(0, len(more)):
    rm.append([])
    r = 0
    for j in range(1, len(more[0])):
        col = more[i][0] - 1
        if (more[i][j] > -999):
            rm[r][col].append(more[i][j])
            r = r + 1

print(rm)

with this code I am getting the error "list index out of range" for the line:使用此代码,我收到以下行的错误“列表索引超出范围”:

rm[r][col].append(more[i][j])

thanks !谢谢 !

You're trying to get the index of an empty list which doesn't have any index values.您正在尝试获取没有任何索引值的空列表的索引。 rm[r][col] will access the 0 index of rm but then try to access the 0 index of the list you appended which is empty and has no indexes. rm[r][col]将访问 rm 的 0 索引,然后尝试访问您附加的列表的 0 索引,该列表为空且没有索引。 Instead of appending to a list I think a dictionary would be best to hold the values in a more organized and then easier to convert into an array if you have to have it in one.我认为字典最好不要附加到列表中,以更有条理地保存值,然后如果必须将其包含在一个数组中,则更容易将其转换为数组。 Myself I would keep it in the dictionary that way you could just refer to the value of the month like so data[1] and get the values you need without worrying about indexing.我自己我会把它保存在字典中,这样你就可以像data[1]那样引用月份的值并获得你需要的值而不必担心索引。

rm = []
       Month  Station1 Station2 Station3
more = [[1,    -999,      3,        2], 
        [1,       1,      0,     -999], 
        [2,       4,   -999,     -999],
        [3,    -999,   -999,        5]]

data = {}
for i in range(0, len(more)):
    if more[i][0] not in data.keys():
        data[more[i][0]] = []
    for j in range(1, len(more[0])):
        if (more[i][j] > -999):
            data[more[i][0]].append(more[i][j])

rm = [v for k, v in data.items()]

print(rm)

Which gives this output:这给出了这个 output:

[3, 2, 1, 0], [4], [5]]

There are many ways to solve this problem, and you could probably use dict or other data structures to facilitate the problem.有很多方法可以解决这个问题,您可能可以使用dict或其他数据结构来解决这个问题。 Here's a solution that uses a dictionary to hold the lists.这是一个使用字典来保存列表的解决方案。 This solution works well because you want to associate the items in the list with a month number, but the same month can span multiple sublists.此解决方案效果很好,因为您希望将列表中的项目与月份编号相关联,但同一月份可以跨越多个子列表。

In python, you can iterate over lists directly without using indices, if you want indices, you can just do for index, item in enumerate(list) rather than using range and len .在 python 中,您可以在不使用索引的情况下直接迭代列表,如果您需要索引,您可以只使用for index, item in enumerate(list)而不是使用rangelen

Try:尝试:

from collections import defaultdict

d = defaultdict(list)
for row in more:
    for val in row[1:]:
        if val != -999:
            # row[0] stores the month value which we want to key on
            d[row[0]].append(j)

rm = list(d.values())

This will still yield the same set of lists in rm as before but you lose the association between which values belong to which month.这仍然会在rm中产生与以前相同的列表集,但是您会丢失哪些值属于哪个月份之间的关联。 It is probably better to keep them in dictionary format.最好将它们保存为字典格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM