zoukankan      html  css  js  c++  java
  • 笨方法学Python——习题39

    dict  字典{ }:用单引号把Key与Value标记,用 : 连接,‘Key’ :‘Value’ 是一个字典元素,调用x['Key']可获取Value值

    1 dict_a = {'x' : '1', 'y' : '2'} # 这是赋值
    2 dict_a['z'] = '3' # 这是增加
    3 dict_a['x'] = '4' # 这是改变
    4 dict_a.pop('y')   # 这是删除
    5 'z' in dict_a     # 判断是否存在 True  False

    list   列表[ ]:append( )增加、list_a.insert(1,'x'):在指定位置增加、list_a.pop()默认删除最后一位,可指定

    tuple元组( ):不可更改,但可以把列表插入元组,通过改变元组中列表元素,进行变更

    set            :类似于dict,也是一组Key的集合,但不存储Value,由于Key不能重复,故无重复(自动过滤)

     1 # create a mapping of state to abbreviation
     2 states = {
     3     'Oregon': 'OR',
     4     'Florida': 'FL',
     5     'California': 'CA',
     6     'New York': "NY",
     7     'Michigan': 'MI'
     8 }
     9 
    10 # create a basic set of states and some cities in them
    11 cities = {
    12     'CA': 'San Francisco',
    13     'MI': 'Detroit',
    14     'FL': 'Jacksonville'
    15 }
    16 
    17 # add some more cities
    18 cities['NY'] = 'New York'
    19 cities['OR'] = 'Portland'
    20 
    21 # print out some cities
    22 print '-' * 10
    23 print "NY State has: ", cities['NY']
    24 print "OR State has: ", cities['OR']
    25 
    26 # print some states
    27 print '-' * 10
    28 print "Michigan's abbreviation is: ",states['Michigan']
    29 print "Florida's abbreviation is: ", states['Florida']
    30 
    31 # do it by using the state then cities dict
    32 print '-'* 10
    33 print "Michigan has: ", cities[states['Michigan']]
    34 print "Florida has: ", cities[states['Florida']]
    35 # 这里 states['Michigan'] = MI,而cities['MI'] = detroit
    36 # print every state abbreviation
    37 print '-' * 10
    38 for state, abbrev in states.items():
    39     print "%s is abbreviated %s" % (state, abbrev)
    40 
    41 # print every city in state
    42 print '-' * 10
    43 for abbrev, city in cities. items():
    44     print "%s has the city %s" % (abbrev, city)
    45 # items 是遍历字典内所有元素
    46 # now do both at the same time
    47 print '-' * 10
    48 for state, abbrev in states.items():
    49     print "%s state is abbreviated %s and has city %s" % (state, abbrev, cities[abbrev])
    50 
    51 
    52 print '-' * 10
    53 # safely get a abbreviation by state that might not be there
    54 state = states.get('Texas', None)
    55 # get 作用与 in 类似,都是判断是否存在,如果存在,则正常打印,如果不存在,返回指定值
    56 if not state:
    57     print "Sorry, no Texas."
    58 
    59 # get a city with a default value
    60 city = cities.get('TX', 'Does Not Exist')
    61 print "The city for the state 'TX' is: %s" % city

    小知识:缩进是4个空格,不是Tab

  • 相关阅读:
    (转)nginx的root和alias指令的区别
    (转)Bash 快捷键 完整版
    (转)curl 命令使用
    Ansible 部署
    (转)把Sublime Text 2 加入右键菜单(带图标),Edit with Sublime Text
    配置IP地址及HOSTNAME脚本
    Linux Shell : Test命令参数解析
    计算阶乘 n! = 1 * 2 * 3 * ... * n
    .编写一个函数,输入n为偶数时,调用函数求1/2+】1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n
    简单的ATM机的取款过程
  • 原文地址:https://www.cnblogs.com/hejianlong/p/7897523.html
Copyright © 2011-2022 走看看