遍历 python 中的列表时如何删除 None
How to remove None when iterating through a list in python
我有这两个不相等的列表,我正在使用 itertools 循环遍历它们,我正在尝试使用过滤器函数删除 List1 中生成的 None,以便在一天结束时a 只包含两个元素而不是三个(算上 none),但我不断收到此错误:类型错误:None类型对象不可迭代
import itertools
List1 = [['a'],['b']]
List2 = ['A','b','C']
l = list(itertools.chain(*List1))
print(l)
for a, b in itertools.zip_longest((b for a in List1 for b in a),List2):
filter(None, a)
print(a,b)
为什么不直接使用 zip
?
for a, b in zip((b for a in List1 for b in a),List2):
print(a,b)
但是,如果你真的坚持使用zip_longest
,你不需要使用filter
来删除None值。你只需要一个 if
.
for a, b in itertools.zip_longest((b for a in List1 for b in a),List2):
if a is None: continue
print(a,b)
不完全清楚你想要什么。据我了解问题和评论,您想使用 izip_longest
组合列表,但结果中没有任何 None
元素。
这将从列表的压缩 'slices' 中过滤 None
,并仅打印非 None
值。但请注意,这样您无法确定 non_none
列表中的第一个元素是否来自第一个列表或第二个或第三个列表。
a = ["1", "2"]
b = ["a", "b", "c", "d"]
c = ["x", "y", "z"]
for zipped in izip_longest(a, b, c):
non_none = filter(None, zipped)
print non_none
输出:
('1', 'a', 'x')
('2', 'b', 'y')
('c', 'z')
('d',)
顺便说一句,你的 filter(None, a)
做了什么:它从你的 a
中过滤 None
值,即从 strings "a"
和 "b"
(这没什么用,因为它们不包含 None
值),直到最后一个值失败,因为 None
不可迭代。此外,它无论如何都会丢弃结果,因为您没有将它绑定到变量。 filter
不会 改变原始列表,但 returns 过滤后的副本!
import itertools as it
def grouper(inputs, n, fillvalue=None):
iters = [iter(inputs)] * n
interim = it.zip_longest(*iters, fillvalue=fillvalue)
return interim
nums = range(23)
results = (list(grouper(nums, 4)))
finalanswer = []
for zipped in results:
# non_none = tuple(filter(None, zipped))
# unfortunately the line above filters 0 which is incorrect so instead use:
non_none = tuple(filter(lambda x: x is not None, zipped))
finalanswer.append(non_none)
print(finalanswer)
上面的代码使用 zip_longest 来说明生成 'n' 列表的压缩迭代,无论给定列表中是否存在相应的条目---然后删除 'None' 值 --- 注意 FILTER 认为 None 和 0 是等价的,所以你必须使用 'lambda' 版本。
我有这两个不相等的列表,我正在使用 itertools 循环遍历它们,我正在尝试使用过滤器函数删除 List1 中生成的 None,以便在一天结束时a 只包含两个元素而不是三个(算上 none),但我不断收到此错误:类型错误:None类型对象不可迭代
import itertools
List1 = [['a'],['b']]
List2 = ['A','b','C']
l = list(itertools.chain(*List1))
print(l)
for a, b in itertools.zip_longest((b for a in List1 for b in a),List2):
filter(None, a)
print(a,b)
为什么不直接使用 zip
?
for a, b in zip((b for a in List1 for b in a),List2):
print(a,b)
但是,如果你真的坚持使用zip_longest
,你不需要使用filter
来删除None值。你只需要一个 if
.
for a, b in itertools.zip_longest((b for a in List1 for b in a),List2):
if a is None: continue
print(a,b)
不完全清楚你想要什么。据我了解问题和评论,您想使用 izip_longest
组合列表,但结果中没有任何 None
元素。
这将从列表的压缩 'slices' 中过滤 None
,并仅打印非 None
值。但请注意,这样您无法确定 non_none
列表中的第一个元素是否来自第一个列表或第二个或第三个列表。
a = ["1", "2"]
b = ["a", "b", "c", "d"]
c = ["x", "y", "z"]
for zipped in izip_longest(a, b, c):
non_none = filter(None, zipped)
print non_none
输出:
('1', 'a', 'x')
('2', 'b', 'y')
('c', 'z')
('d',)
顺便说一句,你的 filter(None, a)
做了什么:它从你的 a
中过滤 None
值,即从 strings "a"
和 "b"
(这没什么用,因为它们不包含 None
值),直到最后一个值失败,因为 None
不可迭代。此外,它无论如何都会丢弃结果,因为您没有将它绑定到变量。 filter
不会 改变原始列表,但 returns 过滤后的副本!
import itertools as it
def grouper(inputs, n, fillvalue=None):
iters = [iter(inputs)] * n
interim = it.zip_longest(*iters, fillvalue=fillvalue)
return interim
nums = range(23)
results = (list(grouper(nums, 4)))
finalanswer = []
for zipped in results:
# non_none = tuple(filter(None, zipped))
# unfortunately the line above filters 0 which is incorrect so instead use:
non_none = tuple(filter(lambda x: x is not None, zipped))
finalanswer.append(non_none)
print(finalanswer)
上面的代码使用 zip_longest 来说明生成 'n' 列表的压缩迭代,无论给定列表中是否存在相应的条目---然后删除 'None' 值 --- 注意 FILTER 认为 None 和 0 是等价的,所以你必须使用 'lambda' 版本。