0

So I have a large amount of data that is stored in a nested list. The nested list has a lot of sublists but it takes the following general form:

nested_list = [[[ID#, Magnitude, Magnitude Error],[ID#, Magnitude, Magnitude Error]], 
               [[ID#, Magnitude, Magnitude Error],[ID#, Magnitude, Magnitude Error]]]

The ID#, Magnitude and Magnitude Error are all floats. I also have a list of common ID numbers. What I want to do is remove elements that are tagged with an ID# that is not in the common set of ID numbers. Essentially only the ID number matters at this point. I have tried my code with the nested list and data below:

nested_list = [[[1.0, 17.634, 0.025], [4.0,  15.633, 0.015], [8.0,  14.097, 0.023],
                [9.0, 15.134, 0.018], [10.0, 15.247, 0.015]],
               [[4.0, 19.634, 0.025], [8.0,  10.097, 0.023], [10.0, 15.247, 0.015]],
               [[4.0, 13.633, 0.015], [8.0,  12.097, 0.023], [9.0,  15.134, 0.018]]]
common_values = [4.0,8.0] 

I was trying to throw out elements that didn't contain one of the common ID numbers. So what would be returned would be:

final_nested_list = [[[[4.0, 15.633, 0.015],[8.0, 14.097, 0.023]],[[4.0, 19.634, 0.025],
                    [8.0, 10.097, 0.023]], [[4.0, 13.633, 0.015],[8.0, 12.097, 0.023]]]

I ran into trouble trying to figure out how to iterate through only the first element that contained the ID number.

4

2 回答 2

1

Hi I can propose You one of two methods:

Use any function to check whether any occurrence of common values is present in nested list

[[i for i in nl if any(j in i for j in common_values)] for nl in nested_list]

tring to find intersection of sets

cv_set = set(common_values)
[[i for i in nl if set(i) & cv_set] for nl in nested_list]

First is preferable because any evaluates result using shorter way (until firs true statement)

于 2013-06-27T20:02:39.337 回答
1

您可以使用嵌套列表推导:

>>> [[y for y in x if y[0] in common_values] for x in nested_list]
[[[4.0, 15.633, 0.015], [8.0, 14.097, 0.023]], [[4.0, 19.634, 0.025], [8.0, 10.097, 0.023]], [[4.0, 13.633, 0.015], [8.0, 12.097, 0.023]]]

如果common_values列表很大,那么最好先将其转换为集合,因为集合提供O(1)查找。

上面的列表推导大致相当于:

>>> out_put = []
for x in nested_list:
   temp = []
   for y in x:
       if y[0] in common_values: #check if first item is present in common_keys
           temp.append(y)
   out_put.append(temp)
...    
>>> out_put
[[[4.0, 15.633, 0.015], [8.0, 14.097, 0.023]], [[4.0, 19.634, 0.025], [8.0, 10.097, 0.023]], [[4.0, 13.633, 0.015], [8.0, 12.097, 0.023]]]
于 2013-06-27T19:49:48.183 回答