Set Comprehensions¶
Let us see how we can process Python collections using set comprehensions.
- Set comprehensions can be applied on top of any iterable.
- We will be using orders data set which will be available by running notebook related to preparing data sets.
- We will see how to get all unique months from orders data set using set comprehensions.
- Also we will see how to apply filtering to get months related to year 2014 using the same orders data set.
In [1]:
%run 07_preparing_data_sets.ipynb
In [2]:
orders[:10]
Out[2]:
['1,2013-07-25 00:00:00.0,11599,CLOSED', '2,2013-07-25 00:00:00.0,256,PENDING_PAYMENT', '3,2013-07-25 00:00:00.0,12111,COMPLETE', '4,2013-07-25 00:00:00.0,8827,CLOSED', '5,2013-07-25 00:00:00.0,11318,COMPLETE', '6,2013-07-25 00:00:00.0,7130,COMPLETE', '7,2013-07-25 00:00:00.0,4530,COMPLETE', '8,2013-07-25 00:00:00.0,2911,PROCESSING', '9,2013-07-25 00:00:00.0,5657,PENDING_PAYMENT', '10,2013-07-25 00:00:00.0,5648,PENDING_PAYMENT']
In [3]:
len(orders)
Out[3]:
68883
In [4]:
order = orders[0]
In [5]:
order
Out[5]:
'1,2013-07-25 00:00:00.0,11599,CLOSED'
In [6]:
order.split(',')[1][:7]
Out[6]:
'2013-07'
In [7]:
order_months = {order.split(',')[1][:7] for order in orders}
In [8]:
order_months
Out[8]:
{'2013-07', '2013-08', '2013-09', '2013-10', '2013-11', '2013-12', '2014-01', '2014-02', '2014-03', '2014-04', '2014-05', '2014-06', '2014-07'}
In [9]:
type(order_months)
Out[9]:
set
In [10]:
order_months = {order.split(',')[1][:7] for order in orders if order.split(',')[1].startswith('2014')}
In [11]:
order_months
Out[11]:
{'2014-01', '2014-02', '2014-03', '2014-04', '2014-05', '2014-06', '2014-07'}