forked from CoreyMSchafer/code_snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomprehensions.py
73 lines (55 loc) · 1.47 KB
/
comprehensions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
nums = [1,2,3,4,5,6,7,8,9,10]
# I want 'n' for each 'n' in nums
my_list = []
for n in nums:
my_list.append(n)
print my_list
print [n for n in nums]
# I want 'n*n' for each 'n' in nums
# my_list = []
# for n in nums:
# my_list.append(n*n)
# print my_list
# Using a map + lambda
# my_list = map(lambda n: n*n, nums)
# print my_list
# I want 'n' for each 'n' in nums if 'n' is even
# my_list = []
# for n in nums:
# if n%2 == 0:
# my_list.append(n)
# print my_list
# Using a filter + lambda
# my_list = filter(lambda n: n%2 == 0, nums)
# print my_list
# I want a (letter, num) pair for each letter in 'abcd' and each number in '0123'
# my_list = []
# for letter in 'abcd':
# for num in range(4):
# my_list.append((letter,num))
# print my_list
# Dictionary Comprehensions
names = ['Bruce', 'Clark', 'Peter', 'Logan', 'Wade']
heros = ['Batman', 'Superman', 'Spiderman', 'Wolverine', 'Deadpool']
# print zip(names, heros)
# I want a dict{'name': 'hero'} for each name,hero in zip(names, heros)
# my_dict = {}
# for name, hero in zip(names, heros):
# my_dict[name] = hero
# print my_dict
# If name not equal to Peter
# Set Comprehensions
# nums = [1,1,2,1,3,4,3,4,5,5,6,7,8,7,9,9]
# my_set = set()
# for n in nums:
# my_set.add(n)
# print my_set
# Generator Expressions
# I want to yield 'n*n' for each 'n' in nums
nums = [1,2,3,4,5,6,7,8,9,10]
# def gen_func(nums):
# for n in nums:
# yield n*n
# my_gen = gen_func(nums)
# for i in my_gen:
# print i