如何从嵌套列表中删除空行?



我从Google Sheets调用数据,我得到一个带有嵌套字典的字典形式的答案,因为我有两个字符串组合,服务器的响应返回空字符串,我如何获得编辑后的回复并将其写入新列表?

{'majorDimension': 'COLUMNS',
'range': "'Sheet1'!A3:A964",
'values': [['Sticker | Fnatic | 2020 RMR',
'',
'Sticker | Fnatic (Holo) | 2020 RMR',
'',
'Sticker | FaZe | 2020 RMR',
'',
'Sticker | FaZe (Holo) | 2020 RMR',
'',
'Sticker | TYLOO | 2020 RMR',
'',
'Sticker | TYLOO (Holo) | 2020 RMR',
'',
'Sticker | G2 | 2020 RMR',
'',
'Sticker | G2 (Holo) | 2020 RMR',
'',
'Sticker | Ninjas in Pyjamas | 2020 RMR',
'',
'Sticker | Ninjas in Pyjamas (Holo) | 2020 RMR',
'',
'Sticker | Astralis | 2020 RMR',
'',
'Sticker | Astralis (Holo) | 2020 RMR',
'',
'Sticker | Natus Vincere | 2020 RMR',
'',
'Sticker | Natus Vincere (Holo) | Katowice 2015']]}

我需要:

new_list =
['Sticker | Fnatic | 2020 RMR',
'Sticker | Fnatic (Holo) | 2020 RMR',
'Sticker | FaZe | 2020 RMR',
'Sticker | FaZe (Holo) | 2020 RMR',
'Sticker | TYLOO | 2020 RMR',
'Sticker | TYLOO (Holo) | 2020 RMR',
'Sticker | G2 | 2020 RMR',
'Sticker | G2 (Holo) | 2020 RMR',
'Sticker | Ninjas in Pyjamas | 2020 RMR',
'Sticker | Ninjas in Pyjamas (Holo) | 2020 RMR',
'Sticker | Astralis | 2020 RMR',
'Sticker | Astralis (Holo) | 2020 RMR',
'Sticker | Natus Vincere | 2020 RMR',
'Sticker | Natus Vincere (Holo) | Katowice 2015']

您可以使用filter()

将其过滤掉
new_list = list(filter(None, c['values'][0]))

带检查器的简单列表推导就可以完成这项工作(假设data是存储响应的变量名):

new_ls = [each for each in data['values'][0] if each != '']

new_ls看起来像:

['Sticker | Fnatic | 2020 RMR', 'Sticker | Fnatic (Holo) | 2020 RMR', 'Sticker | FaZe | 2020 RMR', 'Sticker | FaZe (Holo) | 2020 RMR', 'Sticker | TYLOO | 2020 RMR', 'Sticker | TYLOO (Holo) | 2020 RMR', 'Sticker | G2 | 2020 RMR', 'Sticker | G2 (Holo) | 2020 RMR', 'Sticker | Ninjas in Pyjamas | 2020 RMR', 'Sticker | Ninjas in Pyjamas (Holo) | 2020 RMR', 'Sticker | Astralis | 2020 RMR', 'Sticker | Astralis (Holo) | 2020 RMR', 'Sticker | Natus Vincere | 2020 RMR', 'Sticker | Natus Vincere (Holo) | Katowice 2015']

相关内容

  • 没有找到相关文章

最新更新