tuple

Ekkart Kleinod  • 

Tuple Generator Expression

Tupel erstellen, die man sonst über die for-Schleife erstellen würde.

symbols = '$¢£¥€¤'
codes = tuple(ord(symbol) for symbol in symbols)
print(codes)
(36, 162, 163, 165, 8364, 164)

Umgang mit tuples

traveler_ids = [('USA', '31195855'), ('BRA', 'CE342567'), ('ESP', 'XDA205856')]
for passport in sorted(traveler_ids):
    print('%s/%s' % passport)
BRA/CE342567
ESP/XDA205856
USA/31195855
traveler_ids = [('USA', '31195855'), ('BRA', 'CE342567'), ('ESP', 'XDA205856')]
for country, _ in traveler_ids:
    print(country)
USA
BRA
ESP

Unpacking

lax_coordinates = (33.9425, -118.408056)
latitude, longitude = lax_coordinates
print(latitude)
print(longitude)
33.9425
-118.408056
import os
_, filename = os.path.split('/home/luciano/.ssh/id_rsa.pub')
print(filename)
id_rsa.pub
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)),
    ('São Paulo', 'BR', 19.649, (-23.547778, -46.635833)),
]

print(f'{"":15} | {"latitude":>9} | {"longitude":>9}')
for record in metro_areas:
    match record:
        case [name, _, _, (lat, lon)] if lon <= 0:
                print(f'{name:15} | {lat:9.4f} | {lon:9.4f}')
                |  latitude | longitude
Mexico City     |   19.4333 |  -99.1333
New York-Newark |   40.8086 |  -74.0204
São Paulo       |  -23.5478 |  -46.6358