我有以下清单:
raw_data = raw_data = [{'type': 'message',
'subtype': 'bot_message',
'text': "This content can't be displayed.",
'ts': '1650899428.077709',
'username': 'Typeform',
'icons': {'image_01': 'www.example.com/foo.png'},
'bot_id': 'BPD1K2SJW',
'app_id': 'AD6SC3RT6',
'blocks': [{'type': 'section',
'block_id': 'Smd',
'text': {'type': 'mrkdwn',
'text': 'You have a new response.',
'verbatim': False}},
{'type': 'section',
'block_id': '6YaLt',
'text': {'type': 'mrkdwn',
'text': '*Where did you first hear about us?*\nOnline Search',
'verbatim': False}},
{'type': 'section',
'block_id': 'w3o',
'text': {'type': 'mrkdwn',
'text': '*Direction: *\nNorth',
'verbatim': False}},
{'type': 'section',
'block_id': 'PTQ',
'text': {'type': 'mrkdwn',
'text': '*Location? *\nNew York',
'verbatim': False}},
{'type': 'section',
'block_id': 'JCfSP',
'text': {'type': 'mrkdwn',
'text': '*What can we do better? *\nTo Nothing',
'verbatim': False}},
{'type': 'section',
'block_id': 'aATCO',
'text': {'type': 'mrkdwn',
'text': '*What is your age? *\n32',
'verbatim': False}},
{'type': 'section',
'block_id': 'FbB',
'text': {'type': 'mrkdwn',
'text': '*Can we speak with you?*\nNo',
'verbatim': False}},
{'type': 'section',
'block_id': 'GR+=',
'text': {'type': 'mrkdwn',
'text': '*Order Number:*\n123456',
'verbatim': False}},
{'type': 'actions',
'block_id': '71Q',
'elements': [{'type': 'button',
'action_id': '+hZp',
'text': {'type': 'plain_text', 'text': 'View results', 'emoji': True},
'url': 'www.example.com/form/abcd/results'}]},
{'type': 'section',
'block_id': 'RJOA',
'text': {'type': 'mrkdwn', 'text': ' ', 'verbatim': False}}]},
{'type': 'message',
'subtype': 'channel_join',
'ts': '1650897264.344889',
'user': 'U03CTDZ4MA6',
'text': '<@T19CTAB4MA6> has joined the channel',
'inviter': 'T049HGBCW'},
{'type': 'message',
'subtype': 'bot_message',
'text': "This content can't be displayed.",
'ts': '1650899428.077709',
'username': 'Typeform',
'icons': {'image_01': 'www.example.com/foo.png'},
'bot_id': 'BPD1K2SJW',
'app_id': 'AD6SC3RT6',
'blocks': [{'type': 'section',
'block_id': 'Smd',
'text': {'type': 'mrkdwn',
'text': 'You have a new response.',
'verbatim': False}},
{'type': 'section',
'block_id': '6YaLt',
'text': {'type': 'mrkdwn',
'text': '*Where did you first hear about us?*\nOnline Search',
'verbatim': False}},
{'type': 'section',
'block_id': 'w3o',
'text': {'type': 'mrkdwn',
'text': '*Direction: *\nNorth',
'verbatim': False}},
{'type': 'section',
'block_id': 'PTQ',
'text': {'type': 'mrkdwn',
'text': '*Location? *\nNew York',
'verbatim': False}},
{'type': 'section',
'block_id': 'JCfSP',
'text': {'type': 'mrkdwn',
'text': '*What can we do better? *\nTo Nothing',
'verbatim': False}},
{'type': 'section',
'block_id': 'aATCO',
'text': {'type': 'mrkdwn',
'text': '*What is your age? *\n32',
'verbatim': False}},
{'type': 'section',
'block_id': 'FbB',
'text': {'type': 'mrkdwn',
'text': '*Can we speak with you?*\nNo',
'verbatim': False}},
{'type': 'section',
'block_id': 'GR+=',
'text': {'type': 'mrkdwn',
'text': '*Order Number:*\n123456',
'verbatim': False}},
{'type': 'actions',
'block_id': '71Q',
'elements': [{'type': 'button',
'action_id': '+hZp',
'text': {'type': 'plain_text', 'text': 'View results', 'emoji': True},
'url': 'www.example.com/form/abcd/results'}]},
{'type': 'section',
'block_id': 'RJOA',
'text': {'type': 'mrkdwn', 'text': ' ', 'verbatim': False}}]}
]我希望能够从这份清单中删除以下内容:
{'type': 'message',
'subtype': 'channel_join',
'ts': '1650897264.344889',
'user': 'U03CTDZ4MA6',
'text': '<@T19CTAB4MA6> has joined the channel',
'inviter': 'T049HGBCW'},我做了,而不是想按索引下降,因为这个块可以在任何地方都是列表(其中包含数百个元素)。
我使用的方法如下:
trimmed = [[elem for elem in dat['type'] if elem['type']['subtype'] != 'channel_join'] for dat in raw_data]这给了我一个TypeError
TypeError: string indices must be integers做这件事最好的方法是什么?
谢谢!
发布于 2022-04-26 20:39:05
使用filter()函数:
def myfilter(item):
if item.get('type') == 'message' and item.get('subtype') == 'channel_join':
return False
return True
filtered_data = list(filter(myfilter, raw_data))
print(len(raw_data))
print(len(filtered_data))或者作为一份清单:
filtered_data = [x for x in raw_data if myfilter(x)]代码中的主要问题是:elem['type']['subtype']
elem‘’type‘是字符串"message",该字符串没有名为'subtype’的元素
发布于 2022-04-26 20:42:50
如果您想要使用列表理解,这应该会很好。
drop = {'type': 'message',
'subtype': 'channel_join',
'ts': '1650897264.344889',
'user': 'U03CTDZ4MA6',
'text': '<@T19CTAB4MA6> has joined the channel',
'inviter': 'T049HGBCW'}
data = [x for x in raw_data if(x!=drop)]
print(data)https://stackoverflow.com/questions/72020197
复制相似问题