3.8練習問題Python学習ノート008

1643 ワード

things = ["mozzaralla","cinderella","salmonella"]
def normalize(name):
    return name[0].upper() + name[1:].lower()
def name_upper(name):
    return name[:].upper()
def name_lower(name):
    return name[:].lower()
Up_things = list(map(normalize,things))
print(Up_things)
b = Up_things.copy()
for i in range(len(things)):
    b[i] = Up_things[len(things)-i-1]
print(b)
b = list(map(name_upper,b))
print(b)
b = list(map(name_lower,b))
print(b)
['Mozzaralla', 'Cinderella', 'Salmonella']
['Salmonella', 'Cinderella', 'Mozzaralla']
['SALMONELLA', 'CINDERELLA', 'MOZZARALLA']
['salmonella', 'cinderella', 'mozzaralla']

リストを逆にして、頭文字を大文字にして、すべての文字を大文字にして、すべての文字を小文字にします.
辞書key()、values()交換
方法1:
e2f = {'dog':'chien','cat':'chat','walrus':'morse'}
e2f['walrus']
print(e2f.items())
b = zip(e2f.values(),e2f.keys())
f2e = dict(b)
f2e
dict_items([('dog', 'chien'), ('cat', 'chat'), ('walrus', 'morse')])
{'chat': 'cat', 'chien': 'dog', 'morse': 'walrus'}

方法2:辞書を用いて導く
e2f = {'dog':'chien','cat':'chat','walrus':'morse'}
f2e = {v: k for k, v in e2f.items()}
print(f2e)
{'chien': 'dog', 'chat': 'cat', 'morse': 'walrus'}