3.8练习题 Python 学习笔记008

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()互换

方法一:

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'}

方法二:使用字典推导

e2f = {'dog':'chien','cat':'chat','walrus':'morse'}
f2e = {v: k for k, v in e2f.items()}
print(f2e)
{'chien': 'dog', 'chat': 'cat', 'morse': 'walrus'}





你可能感兴趣的:(课题学习)