在python中对(t表示L中的t的t的解释)

python中有一个练习,从元组列表中删除一个空元组。input: [(),(),('',),('a','b'),'b','c'),('d')]和预期的output: [('','d']

这是他们提供的解决方案。

L = [(),('d')]
L = [t for t in L if t]
print(L)

但是我无法理解L = [t for t in L if t]部分。非常感谢您在此方面的帮助!

asdg85171683 回答:在python中对(t表示L中的t的t的解释)

L = [t for t in L if t]可以用伪代码编写,如下所示:

result_list = []

for t in L:
  if t is True:
    result_list.append(t)

伪代码只是不替换原始列表,而是创建一个新列表。您还需要知道一个空元组在Python中的计算结果为False。

本文链接:https://www.f2er.com/3144080.html

大家都在问