Datacamp-python-Dictionaries

来源:互联网 发布:财达大智慧软件下载 编辑:程序博客网 时间:2024/06/01 07:39

1.

Motivation for dictionaries--list中index()的应用,即返回索引,zero-based

# Definition of countries and capital
countries = ['spain', 'france', 'germany', 'norway']
capitals = ['madrid', 'paris', 'berlin', 'oslo']


# Get index of 'germany': ind_ger
ind_ger=countries.index('germany')


# Use ind_ger to print out capital of Germany
print(capitals[ind_ger])


<script.py> output:
    berlin


2.

Access dictionary

# Definition of dictionary
europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo' }


# Print out the keys in europe
print(europe.keys())


# Print out value that belongs to key 'norway'
print(europe['norway'])


3.

原创粉丝点击