首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从嵌套字典列表中删除元素

从嵌套字典列表中删除元素
EN

Stack Overflow用户
提问于 2022-04-26 20:32:16
回答 2查看 42关注 0票数 -2

我有以下清单:

代码语言:javascript
复制
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}}]}
           
]

我希望能够从这份清单中删除以下内容:

代码语言:javascript
复制
 {'type': 'message',
  'subtype': 'channel_join',
  'ts': '1650897264.344889',
  'user': 'U03CTDZ4MA6',
  'text': '<@T19CTAB4MA6> has joined the channel',
  'inviter': 'T049HGBCW'},

我做了,而不是想按索引下降,因为这个块可以在任何地方都是列表(其中包含数百个元素)。

我使用的方法如下:

代码语言:javascript
复制
trimmed = [[elem for elem in dat['type'] if elem['type']['subtype'] != 'channel_join'] for dat in raw_data]

这给了我一个TypeError

代码语言:javascript
复制
TypeError: string indices must be integers

做这件事最好的方法是什么?

谢谢!

EN

Stack Overflow用户

发布于 2022-04-26 20:42:50

如果您想要使用列表理解,这应该会很好。

代码语言:javascript
复制
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)
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72020197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档