forked from scharlton2/20221024USGS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_comprehensions.py
43 lines (35 loc) · 1.35 KB
/
list_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
fruits = ["pomegranate", "cherry", "apricot", "apple",
"lemon", "kiwi", "orange", "lime", "watermelon", "guava",
"papaya", "fig", "pear", "banana", "tamarind", "persimmon",
"elderberry", "peach", "blueberry", "lychee", "grape", "date" ]
f0 = []
for f in fruits:
f0.append(f.upper())
print(f"f0: {f0}\n")
f1 = [f.upper() for f in fruits] # list comprehension
print(f"f1: {f1}\n")
f2 = [f.upper() for f in fruits if f.startswith('p')]
print(f"f2: {f2}\n")
f3 = [f for f in fruits if f.startswith('p')]
print(f"f3: {f3}\n")
f4 = [f.title() for f in fruits if len(f) < 7]
print(f"f4: {f4}\n")
people = [
('Melinda', 'Gates', 'Gates Foundation', '1964-08-15'),
('Steve', 'Jobs', 'Apple', '1955-02-24'),
('Larry', 'Wall', 'Perl', '1954-09-27'),
('Paul', 'Allen', 'Microsoft', '1953-01-21'),
('Larry', 'Ellison', 'Oracle', '1944-08-17'),
('Grace', 'Hopper', 'COBOL', '1906-12-09'),
('Bill', 'Gates', 'Microsoft', '1955-10-28'),
('Mark', 'Zuckerberg', 'Facebook', '1984-05-14'),
('Sergey','Brin', 'Google', '1973-08-21'),
('Larry', 'Page', 'Google', '1973-03-26'),
('Linus', 'Torvalds', 'Linux', '1969-12-28'),
]
dobs = [p[3] for p in people]
print(f"dobs: {dobs}\n")
ranks = '2 3 4 5 6 7 8 9 10 J Q K A'.split()
suits = 'Clubs Diamonds Hearts Spades'.split()
cards = [(r, s) for s in suits for r in ranks]
print(f"cards: {cards}\n")