我不知道不需要的数据将在哪里弹出或特定字符串是什么时,从数据列表中删除不需要的项目?

这是我编写的输入,但结构与我正在使用的数据相同。我需要删除“一些我不想要的东西”,但我不知道它将出现在数据中的哪些位置。我还需要将剩余的数据放入 7 个项目的子列表中。数据从 PDF 的文本布局中逐个字符提取并放入“输入”列表。我想做的是查看列表中的第一项,检查它是否是小于 3 位的整数。如果为 True,则将该项目和接下来的 6 个放入子列表中。如果为 False,我希望它忽略该项目并检查下一个项目。我希望它持续执行此操作,直到它用完要检查的数据并将其放入子列表中。

输入:

['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '4', '3.00', '43.00 NC', '1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3','43.00 NC', 'some stuff I dont want', '1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC']

输出应如下所示: 输出:

[['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '4', '3.00', '43.00 NC'], ['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC'], ['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC']]

我尝试使用 for 循环和 while 循环,但我似乎无法获得正确的语法,只将我想要的数据放入子列表而忽略我不想要的数据。有没有办法做到这一点,也许我错过了?


jeck猫
浏览 80回答 1
1回答

繁星淼淼

这样的事情可能会让你开始:data = ['1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '4', '3.00', '43.00 NC', '1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3','43.00 NC', 'some stuff I dont want', '1','1','2', '11" Some Words symbols and numbers mixed 3-4-2#', '3.00', '3', '43.00 NC']all_sublists = []i = 0while i < len(data):&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; if int(data[i]) < 100:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; all_sublists.append(data[i:i+7])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i += 7&nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; i += 1all_sublists返回[['1',&nbsp; '1',&nbsp; '2',&nbsp; '11" Some Words symbols and numbers mixed 3-4-2#',&nbsp; '4',&nbsp; '3.00',&nbsp; '43.00 NC'],&nbsp;['1',&nbsp; '1',&nbsp; '2',&nbsp; '11" Some Words symbols and numbers mixed 3-4-2#',&nbsp; '3.00',&nbsp; '3',&nbsp; '43.00 NC'],&nbsp;['1',&nbsp; '1',&nbsp; '2',&nbsp; '11" Some Words symbols and numbers mixed 3-4-2#',&nbsp; '3.00',&nbsp; '3',&nbsp; '43.00 NC']]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python