简体   繁体   中英

How to extract certain items from list?

Suppose I have these lists:

a = ['2009','2010a','2010b','2011','2012a','2012b','2012c','2013a','2014b'] 
b = ['2008a','2008b'] 

and I need to write a code that give me the following lists:

#If I provide list a as the input:
a1 = ['2010a','2010b']
a2 = ['2012a','2012b','2012c']
a3 = ['2013a']
a4 = ['2014b']

#If I provide list b as the input:
b1 = ['2008a','2008b'] 

Currently, I do the followings:

  1. Iterate over every item in the input list
  2. Check if the last string of each item is alphabet (eg a of '2010a')
  3. Check if the last string of the next item is also alphabet (eg '2010a' and '2010b')
  4. Keep checking until the last string of the next item is not alphabet (eg '2010a','2010b','2011') and collect the previous items that end with alphabets (eg '2010a' and '2010b')
  5. Check if the collected items have the same digits (eg '2010a' and '2010b', but not '2013a' and '2014b') and produce the sub-lists as appropriate.

The above steps work, but are quite long. I wonder if there is any code/trick in python libraries that can make the code shorter and look cleaner/ more elegant.

You first need to check if the last character is a letter:

>>> a = ['2009','2010a','2010b','2011','2012a','2012b','2012c','2013a','2014b']
>>> number_and_letters = [x for x in a if x[-1].isalpha()]
['2010a', '2010b', '2012a', '2012b', '2012c', '2013a', '2014b']

You can then use itertools.groupby with a lambda of x[:4] to group the strings by numbers:

>>> import itertools
>>> list(list(words) for year, words in itertools.groupby(number_and_letters, lambda x: x[:4]))
[['2010a', '2010b'], ['2012a', '2012b', '2012c'], ['2013a'], ['2014b']]

groupby expects strings to be already sorted, which seems to be the case here.

Finally, you get a list as an output, not 4 distinct variables. It's usually much easier to work with a list of 4 elements than with 4 distinct variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM