How can I replace a text delimited string list item with multiple list items in a python list?

前端 未结 12 2151
遥遥无期
遥遥无期 2020-12-11 05:17

Given a list:

mylist = [\'dog\', \'cat\', \'mouse_bear\', \'lion_tiger_rabbit\', \'ant\']

I\'d like a one-liner to return a new list:

12条回答
  •  感情败类
    2020-12-11 05:22

    Since so many answers here were posted (over ten), I thought it'd be beneficial to show some timing stats to compare the different methods posted:

    -----------------------------------------
    AChampion time: 2.6322
    -----------------------------------------
    hiro_protagonist time: 3.1724
    -----------------------------------------
    Eugene_Sh time: 1.0108
    -----------------------------------------
    cᴏʟᴅsᴘᴇᴇᴅ time: 3.5386
    -----------------------------------------
    jdehesa time: 2.9406
    -----------------------------------------
    mogga time: 3.1645
    -----------------------------------------
    Ajax1234 time: 2.4659
    -----------------------------------------
    

    Here's the script I used to test:

    from timeit import timeit
    
    setup = """
    from itertools import chain
    mylist = ['dog', 'cat', 'mouse_bear', 'lion_tiger_rabbit', 'ant']
    """
    
    methods = {
        'AChampion': """[animal for word in mylist for animal in word.split('_')]""",
        'hiro_protagonist': """list(chain.from_iterable(item.split('_') for item in mylist))""",
        'Eugene_Sh': """'_'.join(mylist).split('_')""",
        'cᴏʟᴅsᴘᴇᴇᴅ': """list([(yield from x.split('_')) for x in mylist])""",
        'jdehesa': """sum((s.split("_") for s in mylist), [])""",
        'mogga': """[i for sublist in [j.split('_') for j in mylist] for i in sublist]""",
        'Ajax1234': """list(chain(*[[i] if "_" not in i else i.split("_") for i in mylist]))"""
    }
    
    print('-----------------------------------------')
    for author, method in methods.items():
        print('{} time: {}'.format(author, round(timeit(setup=setup, stmt=method), 4)))
        print('-----------------------------------------')
    

    Each method is tested against the sample list given in the question about one million times. To keep things readable, each timing result was rounded to four decimal places.


    Note: If you have a new, unique method that has not been posted here yet, contact me in the comments and I'll try to add a timing for it too.

提交回复
热议问题