Temporary variable within list comprehension

后端 未结 3 2134
[愿得一人]
[愿得一人] 2021-02-19 05:07

It happens to me quite often to have a piece of code that looks like this.

raw_data  = [(s.split(\',\')[0], s.split(\',\')[1]) for s in all_lines if s.split(\',         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-19 05:18

    If you have two actions for processing, you may embed another list comprehension:

    raw_data  = [(lhs, rhs) 
                for lhs, rhs 
                in [s.split(',')[:2] for s in all_lines]
                if rhs != '"NaN"']
    

    You can use generator inside (it gives a small performance gain too):

                in (s.split(',')[:2] for s in all_lines)
    

    It will even be faster than your implementation:

    import timeit
    
    setup = '''import random, string;
    all_lines = [','.join((random.choice(string.letters),
                        str(random.random() if random.random() > 0.3 else '"NaN"')))
                        for i in range(10000)]'''
    oneloop = '''[(s.split(',')[0], s.split(',')[1]) 
                  for s in all_lines if s.split(',')[1] != '"NaN"']'''
    twoloops = '''raw_data  = [(lhs, rhs) 
                    for lhs, rhs 
                    in [s.split(',') for s in all_lines]
                    if rhs != '"NaN"']'''
    
    timeit.timeit(oneloop, setup, number=1000)  # 7.77 secs
    timeit.timeit(twoloops, setup, number=1000) # 4.68 secs
    

提交回复
热议问题