简体   繁体   English

删除 python 中的元素后如何保存所有元素索引

[英]How to save all element index after removing element in python

sentence = [[1,0,3],[2,0,0],[0,0,5]]
empty = []
for element in sentence :
    for icelement in element :
        if not icelement == 0:
            empty.append(icelement)
        
         
        
print(empty)
print(empty[2]) #this should give 3 or empty[8] should give 5

I did many attempt but stil having problem.我做了很多尝试,但仍然有问题。 I know this is because python automatically updating the elements index but dont know how to control it.我知道这是因为 python 自动更新元素索引但不知道如何控制它。 Any suggestion will help me.任何建议都会帮助我。

num = [1,0,4,5]
print(num[3]) #gives 5
for n in num :
    if n == 0:
        num.remove(n)

print(num[3])# doesnt exist.

The indexes of python starts with 0 python的索引从0开始

It gives you back the list:它会返回列表:

[1, 3, 2, 5]

so to get the second element we should do:所以要获得第二个元素,我们应该这样做:

print(empty[1])

so the code looks like this now所以代码现在看起来像这样

sentence = [[1,0,3],[2,0,0],[0,0,5]]
empty = []
for element in sentence :
    for icelement in element :
        if not icelement == 0:
            empty.append(icelement)
        
         
        
print(empty)
print(empty[1])

Output: Output:

[1, 3, 2, 5]
3

You want to preserve the position in the original flattened list while removing some elements?您想在删除一些元素的同时保留原始扁平列表中的 position 吗?

The options are either flatten the list, leave the zeroes in place;选项要么展平列表,要么保留零; just ignore them.只是忽略它们。

empty = [item for sublist in sentence for item in sublist] #[1, 0, 3, 2, 0, 0, 0, 0, 5]
print(empty[2]) # returns 3

Or do something with a dictionary, use the original position in the flattened list as the key.或者用字典做点什么,用扁平化列表中原来的position作为key。

count = 0
empty = {}
for element in sentence :
    for icelement in element :
        if not icelement == 0:
            empty[count] = icelement
        count +=1

print(empty[2]) # returns 3

From your code and the description of the output, it looks like you want to flatten you nested list and remove the zeros.从您的代码和 output 的描述来看,您似乎想要展平嵌套列表并删除零。 At the same time you want to retain the original flattened indices.同时你想保留原来的扁平化索引。

This is not possible with a list as the indices necessarily go from 0 to len(the_list) .这对于列表是不可能的,因为索引必须是 go 从0len(the_list)

You can either keep the zeros:您可以保留零:

sentence = [[1,0,3],[2,0,0],[0,0,5]]

empty = [i for l in sentence for i in l]
# [1, 0, 3, 2, 0, 0, 0, 0, 5]

Or use a different container.或者使用不同的容器。 Dictionaries enable to have arbitrary keys:字典可以有任意键:

from itertools import chain

empty = {i:v for i,v in
         enumerate(chain.from_iterable(sentence))
         if v}
# {0: 1, 2: 3, 3: 2, 8: 5}

empty[2] 
# 3

empty[8]
# 5


you can write the same loop in one line and it gives the same result as your code.您可以在一行中编写相同的循环,它会给出与您的代码相同的结果。

sentence = [[1,0,3],[2,0,0],[0,0,5]]

empty= [icelement for element in sentence for icelement in element if icelement != 0]
print(empty)

output: output:

[1,3,2,5]

one liner solution:一种衬垫解决方案:

sentence = [[1,0,3],[2,0,0],[0,0,5]]
counter = [j for i in sentence for j in i if j!=0]

we take j in case j is not equal to 0如果j不等于0 ,我们取j

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

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