【问题标题】:Remove all items after a specific item in a nested list python删除嵌套列表python中特定项目之后的所有项目
【发布时间】:2020-05-04 19:31:38
【问题描述】:

我有以下嵌套列表,我想删除 我的个人资料 之后的所有项目(包括 我的个人资料)。

my_lst = [['John C, CEO & Co-Funder, ABC company','Eric P, CFO, QWE company','My Profile','Herber W, CTO, PPP company'],
['Eli S, AVP, ACV Company', 'My Profile','Brian M, Analyst, LPL company'],
['Diana F, Managing Director, MS company','Alan X, Associate, JPM company','My Profile', 'Jame R, Manager, AL company']]

我尝试了编码[[i for i in nested if i != 'My Profile'] for nested in my_lst],但只能从每个列表中删除我的个人资料

我的预期输出:

[['John C, CEO & Co-Funder, ABC company','Eric P, CFO, QWE company'],
['Eli S, AVP, ACV Company'],
['Diana F, Managing Director, MS company','Alan X, Associate, JPM company']]

提前感谢您的帮助!

【问题讨论】:

    标签: python-3.x list nested list-comprehension


    【解决方案1】:

    使用列表推导,您可以:

    my_lst = [['John C, CEO & Co-Funder, ABC company','Eric P, CFO, QWE company','My Profile','Herber W, CTO, PPP company'],
    ['Eli S, AVP, ACV Company', 'My Profile','Brian M, Analyst, LPL company'],
    ['Diana F, Managing Director, MS company','Alan X, Associate, JPM company','My Profile', 'Jame R, Manager, AL company']]
    results = [nested[:nested.index('My Profile')] if 'My Profile' in nested else nested for nested in my_lst]
    

    没有列表理解:

    my_lst = [['John C, CEO & Co-Funder, ABC company','Eric P, CFO, QWE company','My Profile','Herber W, CTO, PPP company'],
        ['Eli S, AVP, ACV Company', 'My Profile','Brian M, Analyst, LPL company'],
        ['Diana F, Managing Director, MS company','Alan X, Associate, JPM company','My Profile', 'Jame R, Manager, AL company']]
    results = []
    for nested in my_lst:
        filtered = nested[:nested.index('My Profile')] if 'My Profile' in nested else nested
        results.append(filtered)
    

    【讨论】:

    • 如果每个子列表中都有'My Profile',这将起作用。
    • 感谢@Andrej Kesely 指出这一点,我编辑了我的答案。
    【解决方案2】:

    您可以使用itertools.takewhile 执行此任务:

    from itertools import takewhile
    
    my_lst = [['John C, CEO & Co-Funder, ABC company','Eric P, CFO, QWE company','My Profile','Herber W, CTO, PPP company'],
    ['Eli S, AVP, ACV Company', 'My Profile','Brian M, Analyst, LPL company'],
    ['Diana F, Managing Director, MS company','Alan X, Associate, JPM company','My Profile', 'Jame R, Manager, AL company']]
    
    out = []
    for i in my_lst:
        out.append([*takewhile(lambda k: k!='My Profile', i)])
    
    from pprint import pprint
    pprint(out)
    

    打印:

    [['John C, CEO & Co-Funder, ABC company', 'Eric P, CFO, QWE company'],
     ['Eli S, AVP, ACV Company'],
     ['Diana F, Managing Director, MS company', 'Alan X, Associate, JPM company']]
    

    编辑(列表理解版本):

    out = [[*takewhile(lambda k: k!='My Profile', i)] for i in my_lst]
    

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      相关资源
      最近更新 更多