forked from mazlum/python-tricks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyield.py
48 lines (40 loc) · 766 Bytes
/
yield.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
import random
months = ['Jan','Feb','Mar','Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
#yield kullanarak
def random_month_yield(max_len=10):
for i in range(1,max_len+1):
yield {i: random.choice(months)}
print(list(random_month_yield()))
#Output
"""
[{1: 'Jul'},
{2: 'May'},
{3: 'Jun'},
{4: 'Dec'},
{5: 'Nov'},
{6: 'Jun'},
{7: 'May'},
{8: 'Sep'},
{9: 'Jan'},
{10: 'Dec'}]
"""
#yield kullanmadan
def random_month(max_len=10):
data = []
for i in range(1, max_len+1):
item = {i: random.choice(months)}
data.append(item)
return data
print(random_month())
"""
[{1: 'Apr'},
{2: 'Jan'},
{3: 'Feb'},
{4: 'Aug'},
{5: 'Mar'},
{6: 'May'},
{7: 'Aug'},
{8: 'Oct'},
{9: 'Jun'},
{10: 'May'}]
"""