和小姐姐撸Python

Chapter 3 python 容器

2017-12-11  本文已影响0人  Modelstrategy
things = ['mozzarella', 'cinderella', 'salmonella']`

for name in things:
    print(name.capitalize())`

## capitalize all letters in every item of the list below`

for name in things:
    print(name.upper())

## remove an appointed item from the list`
things.remove('salmonella')
# list is immutable?`
print(things)`

surprise = ['Groucho', 'Chico', 'Harpo' ]
surprise[-1] = surprise[-1].lower()
surprise[-1] = surprise[-1][:: -1]
print(surprise[-1])

surprise_new = ['Groucho', 'Chico', 'Harpo' ]

print( surprise_new[-1].lower()[:: -1] )
print( surprise_new[-1])

## create a dict
e2f = {'dog': 'chien', 'cat': "cha", 'walrus': 'morse'}`
print(e2f['walrus'])

# create a dict with method items
f2e = {}
for key, value in e2f.items():
    f2e [value] = key

print(f2e)
print(f2e['chien'])

## create a set with the values in dict above
new_set = set()
for value in f2e.values():
    new_set.add(value)

print(new_set)

# Multilevel dictionary


life = {'animals':
                {'cats': ['Henri', 'Grumpy', 'Lucy'],
                 'octopi':{} ,
                 'emus':{}},
            'plants': {},
            'others': {}}

for key in life.keys():
    print(key)

print('-----')

for key in life['animals'].keys():
    print(key)

print('-----')

for element in life['animals']['cats']:
    print(element)```
上一篇下一篇

猜你喜欢

热点阅读