我不想删除此列表中的任何值
input= [(None, 'Ibrahimpatnam', 9440627084, None, 'Under Investigation'),
(None, 'Ibrahimpatnam', 9440627084, None, 'Under Investigation')]并得到如下输出
['Ibrahimpatnam', 9440627084, 'Under Investigation', 'Ibrahimpatnam', 9440627084, 'Under Investigation']发布于 2019-03-13 11:58:07
您需要遍历列表(其中包含元组),然后遍历每个元组。检查每个元组的每个元素是否为None。
a = [
(None, "Ibrahimpatnam", 9_440_627_084, None, "Under Investigation"),
(None, "Ibrahimpatnam", 9_440_627_084, None, "Under Investigation"),
]
b = [element for sub_tuple in a for element in sub_tuple if element is not None]
print(b)然后你就会得到
“易卜拉欣帕特南”,9440627084,“正在调查”,“易卜拉欣帕特南”,9440627084,“正在调查”
发布于 2019-03-13 12:09:56
试试这个:
input_= [(None, 'Ibrahimpatnam', 9440627084, None, 'Under Investigation'),
(None, 'Ibrahimpatnam', 9440627084, None, 'Under Investigation')]
output = []
for each in input_:
newList = list(filter(None,each))
output = output+newList
print(output)注意:不使用input作为变量,它是Python语言中的保留关键字。如果你只是在这篇文章中使用过它,那也没关系。
发布于 2019-03-13 12:53:38
如果你想剥离,那么连接--列表理解在这里工作得非常干净,而且不会失去可读性:
import itertools
stripped_lists = [ [x for x in sublist if x] for sublist in input_ ]
print(list(itertools.chain.from_iterable(stripped_lists )))输出:
['Ibrahimpatnam', 9440627084, 'Under Investigation', 'Ibrahimpatnam', 9440627084, 'Under Investigation']或者,如果您连接,然后剥离,这是很好和简短的:
print(list(x for x in itertools.chain.from_iterable(aa) if x))https://stackoverflow.com/questions/55134123
复制相似问题