【问题标题】:Program that ensures three lists have the same amount of elements确保三个列表具有相同数量元素的程序
【发布时间】:2022-01-24 04:47:52
【问题描述】:

我目前正在尝试构建 python 代码,以在三个列表的长度不相等的情况下删除列表中的最后一个元素。这样可以节省返回列表并在每次不满足条件时手动 listExample{}.pop() 到某些列表的时间。

最小长度的列表应该是所有三个列表所需的元素长度。在此示例中,它将是四个元素,但理想情况下,程序应计算三个列表的长度并获取最小的整数作为目标。如果列表不相等,则程序将从正确的列表中 .pop() 直到满足 listLength1 == listLength2 == listLength3

以下是我为设置示例而创建的列表和变量:

listExample1 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement', 'SixthElement']
listExample2 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
listExample3 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement']

listLength1 = len(listExample1)
listLength2 = len(listExample2)
listLength3 = len(listExample3)

print(listLength1) #6
print(listLength2) #4
print(listLength3) #5

这是我目前正在构建的代码:

if listLength1 == listLength2 == listLength3:
    pass
elif listLength1 < listLength2:
    pass 
elif listLength1 > listLength3:
    pass
elif listLength1 == listLength2:
    pass 
elif listLength2 < listLength1:
    pass
elif listLength2 > listLength3:
    pass
elif listLength2 == listLength1:
    pass
elif listLength3 < listLength3:
    pass
elif listLength3 > listLength1:
    pass
elif listLength3 == listLength2:
    pass
else:
    pass

if/elif 似乎是多余的,当我在某种情况下启动 listExample1.pop() 时,我觉得有出错的余地,这可能会搞砸。解决这个问题的最佳方法是什么?

期望的输出:

print(listExample1) #['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
print(listExample2) #['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
print(listExample3) #['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']

【问题讨论】:

  • minLength = min(listLength1, listLength2, listLength3),后跟三个while len(listExample1) &gt; minLength: listExample1.pop() 形式的循环。您不必为最短​​的列表做任何特别的事情,它的循环不会做任何事情。
  • 你为什么要用pops来做这个?这似乎不是正确的工具。
  • 从列表中删除最后一个元素不会在开始时倾斜数据是pop的想法
  • 这里会更好地使用切片方法吗?例如获取最小列表长度并将其存储为 n。从那时起,您可以剪切所有列表,直到 n 像 "listExample1 = listExample1[:n]"
  • @bbsmfb 我的意思是为什么要使用 pop 来实现这一点,而不是 del(现在已经回答了)。

标签: python list


【解决方案1】:
n = min(listLength1,listLength2,listLength3)

for i in range(listLength1-n):
    listExample1.pop()    
for i in range(listLength2-n):
    listExample2.pop()
for i in range(listLength3-n):
    listExample3.pop()

print(listExample1)
print(listExample2)
print(listExample3)

输出

['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']

【讨论】:

  • 你会得到三个相同长度的列表
【解决方案2】:

您可以使用 min 函数获得最小长度。然后为每个列表运行一个 for 循环,如果需要,弹出最后一个元素。

listExample1 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement', 'SixthElement']
listExample2 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
listExample3 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement']

minLen = min(len(listExample1),len(listExample2),len(listExample3))

for _ in range(len(listExample1)-minLen):
    listExample1.pop(-1)

for _ in range(len(listExample2)-minLen):
    listExample2.pop(-1)

for _ in range(len(listExample3)-minLen):
    listExample3.pop(-1)

print(listExample1)
print(listExample2)
print(listExample3)

【讨论】:

  • 找到“minLen”后,为什么不直接将每个列表中的第一个minLen个元素复制到自己。喜欢...listExample1 = listExample1[:minLen]
【解决方案3】:

您最好将它们放在列表列表中。然后删除不需要的后缀。

n = min(map(len, lists))
for L in lists:
    del L[n:]

比逐个弹出元素或复制前缀切片更有效。

【讨论】:

    【解决方案4】:

    试试这样的:

    def reduce_size(*sequences):
        min_length = min(len(sequence) for sequence in sequences)
        return (sequence[:min_length] for sequence in sequences)
    
    list1 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement', 'SixthElement']
    list2 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
    list3 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement']
    
    list1, list2, list3 = reduce_size(list1, list2, list3)
    

    【讨论】:

      【解决方案5】:

      我们可以简单地使用它,而不是迭代弹出。

      listExample1 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement', 'SixthElement']
      listExample2 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
      listExample3 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement']
      
      lengths = [len(listExample1) ,len(listExample2) ,len(listExample3)  ]
      
      minL = min(lengths)
      
      listExample1 = listExample1[:minL]
      listExample2 = listExample2[:minL]
      listExample3 = listExample3[:minL]
      
      print(listExample1)
      print(listExample2)
      print(listExample3)
      

      输出为

      ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
      ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
      ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
      

      【讨论】:

        【解决方案6】:

        可以切片:

        min_len = min(len(a), len(b), …)
        
        a = a[:min_len]
        b = b[:min_len]
        …
        

        【讨论】:

          【解决方案7】:

          与之前的 cmets 没有太大不同,但我更愿意只对列表进行切片并返回它们的副本(如果它们不是太大的话)。

          l1 = [1,2,3,4,5]
          l2 = [3,4,5,6]
          l3 = [1,2]
          
          m = min(map(len, (l1,l2,l3)))
          for i in [l1,l2,l3]:
              print i[:m]
          

          结果:

          [1, 2]
          [3, 4]
          [1, 2]
          

          【讨论】:

          • 确实没有太大的不同......与之前的3个答案也做了这个切片。
          【解决方案8】:

          这是一种“可爱”的方式,它的优点是适用于任意迭代:

          list1 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement', 'SixthElement']
          list2 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
          list3 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement']
          
          list1, list2, list3 = map(list, zip(*zip(list1, list2, list3)))
          

          如果tuples 适合您的结果,那么只需

          list1, list2, list3 = zip(*zip(list1, list2, list3))
          

          也可以。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-11-27
            • 1970-01-01
            • 1970-01-01
            • 2019-07-30
            • 1970-01-01
            相关资源
            最近更新 更多