-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tuples.py
61 lines (40 loc) · 1.82 KB
/
Tuples.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
# “Example 2-7. Tuples used as records.”
lax_coordinates = (33.9425, -118.408056)
city, year, pop, chg, area = ('Tokyo', 2003, 32450, 0.66, 8014)
traveler_ids = [('USA', '31195855'), ('BRA', 'CE342567'), ('ESP', 'XDA205856')]
for passports in traveler_ids:
print('%s/%s' % passports)
for passports in sorted(traveler_ids):
print(passports)
for passports in sorted(traveler_ids):
print('country - {0} ** passport number - {1}'.format(*passports))
# here i can manipulate the position of the unpacked data
for passports in sorted(traveler_ids):
print('country - {1} ** passport number - {0}'.format(*passports))
# Unpacking and formating tuples as records: Nested tuple unpacking
metro_areas = [
('Tokyo', 'JP', 36.933, (35.689722, 139.691667)),
('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),
('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),
('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),
('Sao Paulo', 'BR', 19.649, (-23.547778, -146.635833)),
]
print('{:15} | {:^9} | {:^9}'.format('', 'lat.', 'long.'))
fmt = '{:15} | {:9.4f} | {:9.4f}'
for name, cc, pop, (latitude, longitude) in metro_areas:
if longitude <= 0: #
print(fmt.format(name, latitude, longitude))
# Named Tuples
from collections import namedtuple
City = namedtuple('City', 'name country population coordinates')
tokyo = City('Tokyo', 'JP', 33.933, (35.689722, 139.691667))
moscow = City('Moscow', 'RUS', 12.228, (55.75, 37.616667))
# useful methods
# ._fields(), ._make(). ._asdict()
City._fields # tuple with the field names of the class
LatLong = namedtuple('LatLong', 'lat long')
delhi_data = ('Delhi NCR', 'IN', 21.935, LatLong(28.613889, 77.208889))
delhi = City._make(delhi_data) # or City(*delhi_data)
delhi._asdict()
for key, value in delhi._asdict().items():
print(key + ':', value)