【问题标题】:How to save all element index after removing element in python如何在python中删除元素后保存所有元素索引
【发布时间】:2021-12-28 21:30:31
【问题描述】:
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

我做了很多尝试,但仍然有问题。我知道这是因为 python 自动更新元素索引但不知道如何控制它。任何建议都会对我有所帮助。

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.

【问题讨论】:

  • 不清楚你想要什么。为什么empty[2] 会是3 或者为什么empty 甚至有9 个元素让你能够做到empty[8]?请提供预期的输出以及它是如何形成的。
  • 嘿,@Bünyamin Mete,Python 索引从 0 开始。所以如果你想打印 number 3 你需要使用索引 1,像这样:print(empty[1])
  • 您能否添加更多信息,您期望的输出是什么,您是否试图收集所有不为零的元素?您要存储什么索引。您当前的输出应该类似于 [1,3,2,5],您是否期望相同?
  • 你只是想扁平化数组吗?
  • 顺便说一句,使用if not x == 0: 有点奇怪,对于“不等于”,您通常会使用if x != 0:

标签: python arrays python-3.x list


【解决方案1】:

您想在删除一些元素的同时保留原始展平列表中的位置吗?

选项要么展平列表,要么保留零;忽略它们。

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

或者用字典做点什么,用扁平化列表中的原始位置作为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

【讨论】:

    【解决方案2】:

    从您的代码和输出描述来看,您似乎想要展平嵌套列表并删除零。同时你想保留原来的扁平化索引。

    这对于列表是不可能的,因为索引必须从 0len(the_list)

    您可以保留零:

    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]
    

    或者使用不同的容器。字典可以有任意键:

    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
    
    
    

    【讨论】:

      【解决方案3】:

      python的索引以0开头

      它会返回列表:

      [1, 3, 2, 5]
      

      所以要得到我们应该做的第二个元素:

      print(empty[1])
      

      所以代码现在看起来像这样

      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])
      

      输出:

      [1, 3, 2, 5]
      3
      

      【讨论】:

        【解决方案4】:

        您可以在一行中编写相同的循环,它给出的结果与您的代码相同。

        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)
        

        输出:

        [1,3,2,5]
        

        【讨论】:

          【解决方案5】:

          一个班轮解决方案:

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

          如果j不等于0,我们取j

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-03-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-10-02
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多