Pattern matching dictionaries in Python 3 -
i'm trying pattern match elements python dictionary, following:
person = {"name": "steve", "age": 5, "gender": "male"} {"name": name, "age": age} = person # line right here drives = is_driving_age(age) print f"{name} {age} years old. {'can' if drives else 'can\'t'} drive." # output: steve 5 years old. can't drive. is there way in python 3? have function returns large dictionary, , rather spending 20 lines deconstructing i'd love able pattern match out data.
edit: first few answers here assumed i'm trying print out data, lack of clarity on part. note i'm not trying print out data, i'm trying use further calculations.
person = {"name": "steve", "age": 5, "gender": "male"} my_str = "{name} {age} years old.".format(**person) print(my_str) steve 5 years old.
Comments
Post a Comment