python清理文本d

2024-06-07 10:21:16 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有人有什么提示来清理文本数据?我拥有的数据在一个列表(master_list)中,我试图创建一个循环或函数来删除额外的[]符号以及None,None,因此master_list中的数据基本上是由,分隔的字符串

非常感谢您的帮助。。你知道吗

master_list = [['the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.3.', 'the supply fan is running, the VFD speed output mean value is 94.3.'], None, ['the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.2.', 'the supply fan is running, the VFD speed output mean value is 94.2.'], None, ['the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.1.', 'the supply fan is running, the VFD speed output mean value is 94.1.'], None, ['the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.0.', 'the supply fan is running, the VFD speed output mean value is 94.0.'], None, ['the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 93.9.', 'the supply fan is running, the VFD speed output mean value is 93.9.'], None]

Tags: thenoneisvaluestaticmeanrunninglike
3条回答

看起来您要求的是扁平列表,而不是包含列表的列表。同时,您希望删除None对象。列表的扁平化可以使用in this answer描述的方法来完成。现在,你只需在中间添加一个if语句。你知道吗

master_list = [x for sublist in master_list if sublist is not None for x in sublist]

输出:

['the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.3.',
 'the supply fan is running, the VFD speed output mean value is 94.3.',
 'the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.2.',
 'the supply fan is running, the VFD speed output mean value is 94.2.',
 'the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.1.',
 'the supply fan is running, the VFD speed output mean value is 94.1.',
 'the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.0.',
 'the supply fan is running, the VFD speed output mean value is 94.0.',
 'the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 93.9.',
 'the supply fan is running, the VFD speed output mean value is 93.9.']

列出获胜的理由。你知道吗

master_list = [['the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.3.', 'the supply fan is running, the VFD speed output mean value is 94.3.'], None, ['the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.2.', 'the supply fan is running, the VFD speed output mean value is 94.2.'], None, ['the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.1.', 'the supply fan is running, the VFD speed output mean value is 94.1.'], None, ['the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.0.', 'the supply fan is running, the VFD speed output mean value is 94.0.'], None, ['the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 93.9.', 'the supply fan is running, the VFD speed output mean value is 93.9.'], None]
master_list = [i for x in master_list if x for i in x]

您想将列表展平,因此[[1, 2], [3, 4]]变成[1, 2, 3, 4]。一种方法是通过列表理解:[x for sublist in my_list for x in sublist]。你知道吗

但是,您的数据也包含None而不是列表,因此需要将其过滤掉。此外,子列表还可能包含None,这也需要删除。所以[[1, 2], None, [None, 3, ""]]变成了[1, 2, 3]。你知道吗

为了完成第一部分(在需要列表时删除None值),我们可以使用or操作符sublist or []用空列表有效地替换这些none。我们不能迭代None,但我们可以迭代空列表。你知道吗

为了完成第二部分(除去列表中包含的None值,以及其他“假”值,如空字符串或零),我们在列表末尾添加了一个条件:[... if x]。你知道吗

最后的结果是:

>>> [x for sublist in master_list for x in sublist or [] if x]
['the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.3.',
 'the supply fan is running, the VFD speed output mean value is 94.3.',
 'the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.2.',
 'the supply fan is running, the VFD speed output mean value is 94.2.',
 'the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.1.',
 'the supply fan is running, the VFD speed output mean value is 94.1.',
 'the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 94.0.',
 'the supply fan is running, the VFD speed output mean value is 94.0.',
 'the supply fan speed mean is over 90% like the fan isnt building static, mean value recorded is 93.9.',
 'the supply fan is running, the VFD speed output mean value is 93.9.']

相关问题 更多 >

    热门问题