Chapter 3 python 容器

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)```
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,916评论 0 23
  • 姐姐带着女儿来上海过暑假。抽了一天时间,两个妈领着两个孩子,在上海迪士尼体验了一把“血战到底”。早上11点入园,晚...
    长发的阿柯阅读 775评论 1 1
  • 逃课,一直是大学生活的重大课题之一。都说没逃过课的大学生活是不完美的,可偏偏还有那么多人不知道怎么逃课。 毕业已经...
    赵莲贵阅读 11,359评论 14 35
  • 从小就会责怪时间过的好慢,我还要等好久好久才能拿到妈妈高高放在橱子上的糖果,等好久好久才能捏到爸爸的耳朵。就这样,...
    酒起阅读 199评论 0 0