Skip to content

Commit 9c7bbc5

Browse files
committed
comprehension
1 parent 3a566ec commit 9c7bbc5

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python
2+
3+
# Comprehension Lab
4+
5+
#Count Even Numbers
6+
def count_evens(array):
7+
8+
updated_array = [x for x in array if x%2 == 0 or x == 0 ]
9+
return len(updated_array)
10+
11+
12+
# comprehension test for dict and set
13+
food_prefs = {"name": "Chris",
14+
"city": "Seattle",
15+
"cake": "chocolate",
16+
"fruit": "mango",
17+
"salad": "greek",
18+
"pasta": "lasagna"}
19+
20+
def comprehension(dict):
21+
print("{name} is from {city}, and he likes {cake} cake, {fruit} fruit, {salad} salad, and {pasta} pasta".format(**dict))
22+
23+
24+
num = [x for x in range(16)]
25+
nhex = [hex(x) for x in range(16)]
26+
27+
for x in num:
28+
for y in nhex:
29+
numhex[x] = y
30+
31+
32+
numhex = {x:hex(x) for x in range(16)}
33+
34+
35+
#fp_copy = food_prefs
36+
fp_copy = {}
37+
38+
def countA(v):
39+
count = 0
40+
for x in v:
41+
if x == 'a' or x == 'A':
42+
count+=1
43+
return count
44+
45+
for k,v in food_prefs.items():
46+
fp_copy[k] = countA(v)
47+
48+
49+
Nlist = [i for i in range(21)]
50+
51+
Dlist = [2,3,4]
52+
53+
s2 = set(x for x in Nlist if x%2 == 0)
54+
55+
s3 = set(x for x in Nlist if x%3 == 0)
56+
57+
s4 = set(x for x in Nlist if x%4 == 0)
58+
59+
#slist = [s2,s3,s4]
60+
61+
slist = [set(x for x in Nlist if x%t == 0) for t in Dlist ]

0 commit comments

Comments
 (0)