为 Python 中包含 [] 个元素的多个列表的元素定义总和

Define sum for elements of multiple list with [] elements in Python

_你好,世界!我在从多个列表中删除元素时遇到问题。我有多个列表。

print("Preparation")
print(dateTableMonthValues)

它打印我:

[['4:10'], ['3:20'], ['3:45'], ['32:05'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['35:00'], ['3:45'], ['5:00'], ['5:00'], ['2:30'], ['2:30'], ['3:45'], ['6:15'], ['28:45'], ['5:00'], ['5:00'], ['7:55'], ['7:05'], ['5:00'], ['5:50'], [], ['35:50']]

我需要从这个数组中删除几个元素,例如 ['32:05'], ['35:00'], ['28:45'], [], ['35:50' ]]

好的,我找到了这些值并且:

for i in range(len(dateTableDayValues)):
    week = dateTableDayValues[i][0]
    print(dateTableDayValues[i][0])
    if week[3] == 'w':
        print('w')
        dateTableMonthValues[i].pop(0)
print("Check")
print(dateTableMonthValues)

现在它打印我:

[['4:10'], ['3:20'], ['3:45'], [], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], [], ['3:45'], ['5:00'], ['5:00'], ['2:30'], ['2:30'], ['3:45'], ['6:15'], [], ['5:00'], ['5:00'], ['7:55'], ['7:05'], ['5:00'], ['5:50'], [], []]

所以,我有两个问题。首先,为什么方法 'pop' 没有删除元素,因为我仍然有这样的元素:'[]'。其次,如何从数组(列表)中删除这些用于定义所有元素总和的空元素“[]”? 它不起作用:

if dateTableMonthValues[i][0] == []:
         del dateTableMonthValues[i][0]
         dateTableMonthValues[i].pop(0)

老问题: 因此,当我尝试访问 list[3][0] 时,它给我写了“list index out of range”。所以我决定删除该元素。

但无论如何我无法定义所有元素的总和。我仍然继续收到消息

IndexError: list index out of range

请帮帮我,我该如何解决这个问题?

有很多方法可以解决!

编辑:(问题稍作修改)

  • 为什么方法 'pop' 没有删除元素? 因为你访问了元素'i',删除了他的第一个元素。 如果你想删除它,你可以这样做:
dateTableMonthValues.pop(i) # Not: dateTableMonthValues[i].pop(0)

dateTableMonthValues.remove(the_element)
  • 如果你想删除列表中的每个 [ ],你可以这样做:
lst = [['4:10'], ['3:20'], ['3:45'], [], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], [], ['3:45'], ['5:00'], ['5:00'], ['2:30'], ['2:30'], ['3:45'], ['6:15'], [], ['5:00'], ['5:00'], ['7:55'], ['7:05'], ['5:00'], ['5:50'], [], [],[],[],[]]
i = 0
while i<len(lst):
    if lst[i] == []:
        lst.pop(i)
    else: 
        i+=1
print(lst) #[['4:10'], ['3:20'], ['3:45'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['5:00'], ['3:45'], ['5:00'], ['5:00'], ['2:30'], ['2:30'], ['3:45'], ['6:15'], ['5:00'], ['5:00'], ['7:55'], ['7:05'], ['5:00'], ['5:50']]

枚举

l = [['4:10'], ['3:20'], ['3:45'], ['Delete me'], ['5:00'], ['Delete me'], ['10:00']]

for i,e in enumerate(l):
    if e == ["Delete me"]:
        l.pop(i)

print(l) #[['4:10'], ['3:20'], ['3:45'], ['5:00'], ['10:00']]

但是如果你真的不知道如何使用enumerate:

Try/Except

l = [['4:10'], ['3:20'], ['3:45'], ['Delete me'], ['5:00'], ['Delete me'], ['10:00']]

for i in range(len(l)):
    try:
        if l[i] == ["Delete me"]:
            l.pop(i)
    except IndexError:
        pass

print(l) #[['4:10'], ['3:20'], ['3:45'], ['5:00'], ['10:00']]

为了简单起见: 当出现“IndexError”时,程序只是 pass,因为什么都没发生。 (实际上,从系统的角度来看它更复杂,但你可以这样解释!)

移除

l = [['4:10'], ['3:20'], ['3:45'], ['Delete me'], ['5:00'], ['Delete me'], ['10:00']]

for e in l:
    if e == ["Delete me"]:
        l.remove(e)

print(l) #[['4:10'], ['3:20'], ['3:45'], ['5:00'], ['10:00']]

最后但并非最不重要的一点! list.remove(element) 从列表中删除元素,而不是像 pop

这样的索引的元素