简体   繁体   中英

Python - Delete elements of several sublists at the same index

I am developing an algorithm for the analysis of biological data for the lab I'm working in but i feel stuck at a point. The idea is to compare several experiences containing the same type of data from a single csv file.

I am filtering a list of lists depending on one of the values in the element in the sublist (which are also list of length 3), i don't know how many sublist s are composing my main list neither their length (thousands) but I want to delete the i th element in all of them. (I have a list of index)

An example may be more explicative.

Here's my list of lists of list (with known length), my "list of index"

mainlist=[[[a,1,1,5],[b,2,1,50],[c,3,1,5]],[[alph,1,2,5],[bet,2,2,50],[gam,3,2,5]]] 
indextodelete=[1] (where the 4rth element is > 10)

I try to get the following output

out=[[[a,1,1],[c,3,1]],[[alph,1,2],[gam,3,2]]]

I'm not very used to numpy or pandas .

I tried to use pop() , or by reversing my lists because i have a lot of elements to delete and i already have my index list. Here, npa is my main list and pval is the threshold given by the user.

for i,l in enumerate(npa):
    for l,k in reversed(list(enumerate(l))):
        if float(k)>float(pval):
            npa[0:len(npa)][l].pop()

This post isn't really clear, but to get from A -> B with this example I would run:

main_list = [
    [[0, 1, 1, 5], [0, 2, 1, 50], [0, 3, 1, 5]],
    [[0, 1, 2, 5], [0, 2, 2, 50], [0, 3, 2, 5]]
]
index_to_delete = 1
for sub_list in main_list:
    del sub_list[index_to_delete]
    for smallest_list in sub_list:
        smallest_list.pop()
print(main_list)

With output that matches what you've specified:

[[[0, 1, 1], [0, 3, 1]], [[0, 1, 2], [0, 3, 2]]]

Note: I have substituted 0 for a , b , c , etc since these were not provided

This may not be exactly what you were looking for, but I think it should set you on the right track at least!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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