zoukankan      html  css  js  c++  java
  • day3-基础 列表,元组,字典,模块


    1.列表

    列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作

    定义列表

     1 Country = ['China','England','America'] 

    通过下标访问列表中的元素,下标从0开始计数

    1 print(Country[0])
    2 >>>'China'
    3 print(Country[1])
    4 >>>'England'
    5 print(Country[2])
    6 >>>'America'

    同时取多个元素(切片)

     1 Country = ['China','England','America','New Zealand','Germany']
     2 print(Country[0:3])       #取下标0-3的值,包括0,不包括3
     3 >>>'China','England','America'
     4 print(Country[0:-1])       #取小标0--1的值,-1是倒数第一个
     5 >>> ['China', 'England', 'America', 'New Zealand']
     6 print(Country[0:4])            
     7 >>> ['China', 'England', 'America', 'New Zealand']
     8 print(Country[:4])        #从头开始取,同上
     9 >>> ['China', 'England', 'America', 'New Zealand']
    10 print(len(Country))       #查询一共有多少个值
    11 >>>5
    12 print(len(Country)-1)      #取最后一个下标
    13 >>>4
    14 print(Country[::2])       #2是条件。每隔两个取一次值
    15 >>>['China', 'America', 'Germany']
    16 print(Country[0::2])      #从头开始取,同上
    17 >>>['China', 'America', 'Germany']

    追加:

    1 Country = ['China','England','America','New Zealand','Germany']
    2 print(Country)
    3 >>>['China', 'England', 'America', 'New Zealand', 'Germany']
    4 Country.append('France')      #输入所要追加的,每次只能一个,追加到最后
    5 print(Country )
    6 >>> ['China', 'England', 'America', 'New Zealand', 'Germany', 'France']

    插入:

    1 Country = ['China','England','America','New Zealand','Germany']
    2 print(Country)
    3 >>>['China', 'England', 'America', 'New Zealand', 'Germany']
    4 Country.insert(2,'Thailand')        #2代表要插入的位置的下标
    5 print(Country)
    6 >>> ['China', 'England', 'Thailand', 'America', 'New Zealand', 'Germany']

    修改:

    1 Country = ['China','England','America','New Zealand','Germany']
    2 print(Country)
    3 >>> ['China', 'England', 'America', 'New Zealand', 'Germany']
    4 Country[1] = 'Russia'      #选择要修改的位置,赋值
    5 print(Country)
    6 >>> ['China', 'Russia', 'America', 'New Zealand', 'Germany']

    删除:

     1 Country = ['China','England','America','New Zealand','Germany']
     2 print(Country)
     3 >>>['China','England','America','New Zealand','Germany']
     4 del Country[1]           #选择要删除的下标
     5 print(Country)
     6 >>>['China', 'America', 'New Zealand', 'Germany']
     7 Country.remove('America')     #删除制定值
     8 print(Country)
     9 ['China', 'England', 'New Zealand', 'Germany']
    10 Country.pop()            #删除最后一个
    11 print(Country)
    12 ['China', 'England', 'America', 'New Zealand']

    扩展:

    1 Country = ['China','England','America','New Zealand','Germany']
    2 print(Country)
    3 >>>['China', 'England', 'America', 'New Zealand', 'Germany']
    4 A = [1,2,3,4,5,6]
    5 Country.extend(A)
    6 print(Country)
    7 >>>['China', 'England', 'America', 'New Zealand', 'Germany', 1, 2, 3, 4, 5, 6]

    拷贝:

    1 Country = ['China','England','America','New Zealand','Germany']
    2 print(Country)
    3 >>>['China', 'England', 'America', 'New Zealand', 'Germany']
    4 Country_copy = Country
    5 print(Country_copy)
    6 >>>['China', 'England', 'America', 'New Zealand', 'Germany']

    统计:

    1 Country = ['China','China','China','England','America','New Zealand','Germany']
    2 print(Country)
    3 >>>['China', 'China', 'China', 'England', 'America', 'New Zealand', 'Germany']
    4 print(Country.count('China'))   #选择要统计的数据
    5 >>>3

    排序&翻转:

     1 Country = ['China','England','America','New Zealand','Germany',1]
     2 print(Country)
     3 >>>['China', 'England', 'America', 'New Zealand', 'Germany', 1]
     4 Country.sort()
     5 >>>Traceback (most recent call last):
     6                             File "list.py", line 11, in <module>
     7        TypeError: '<' not supported between instances of 'int' and 'str'    #不是同一数据类型会报错
     8 Country[-1] = '1'
     9 Country.sort()
    10 print(Country)
    11 ['1', 'America', 'China', 'England', 'Germany', 'New Zealand']
    12 Country.reverse()     #反转
    13 print(Country)
    14 >>> ['New Zealand', 'Germany', 'England', 'China', 'America', '1']

    获取下标:

    1 Country = ['China','China','England','America','New Zealand','Germany',1]
    2 print(Country)
    3 >>>['China', 'China', 'England', 'America', 'New Zealand', 'Germany', 1]
    4 print(Country.index('China'))        #选择所要获取的值
    5 >>>0        #返回第一个所遇到的值的下标        

    2.元组

    元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表

    语法

    1 Country = ('China','China','England','America','New Zealand','Germany',1)

    它只有2个方法,一个是count,一个是index,完毕。  

     3.字典

    字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。

    1 info = {
    2     '1':'faker',
    3     '2':'dopa',    
    4     '3':'xiaohu',
    5     '4':'uzi',
    6 }

    字典的特性:

    • dict是无序的
    • key必须是唯一的,so 天生去重

    增加:

    1 info['5'] = ‘godv’
    2 print(info)
    3 >>>{'1': 'faker', '2': 'dopa', '3': 'xiaohu', '4': 'uzi', '5': 'godv'}

     修改:

    1 info['2'] = 'white'       #指定索引,输入所要修改的
    2 print(info)
    3 >>>{'1': 'faker', '2': 'white', '3': 'xiaohu', '4': 'uzi', '5': 'godv'}        

    删除:

    1 del info['2']
    2 print(info)
    3 >>>{'1': 'faker', '3': 'xiaohu', '4': 'uzi', '5': 'godv'}
    4 info.pop['3']
    5 print(info)
    6 >>>{'1': 'faker', '2': 'white', '4': 'uzi', '5': 'godv'}
    7 info.popitem()  #随机删一个

    查找:

     1 info = {
     2     '1':'faker',
     3     '2':'dopa',
     4     '3':'xiaohu',
     5     '4':'uzi',
     6 }
     7 print(info['1'])
     8 >>>faker
     9 print(info.get('1'))      #有就返回,没有就None
    10 >>>faker

    多级操作:

     1 LOL = {
     2     'LCK':{
     3         'SKT':['Huni','Blank','Faker','Bang','Wolf'],
     4         'Samsung':['Semb','Dandy','Crown','Proy','Madlife'],
     5         'KT':['Impact','Kakao','Pown','Deft','Mata']
     6     },
     7     'LPL':{
     8         'RNG':['Letme','Mlxg','Xiaohu','Uzi','Ming'],
     9         'EDG':['Mouse','Clearlove7','Souct','Iboy','Mikeo'],
    10         'WE':['957','Condi','Xiye','Mysitc','Zero']
    11     },
    12     'LCS':{
    13         'TSM':['Svenskeren','Bjergsen','WildTurtle','Biofrost','Yellowstar'],
    14         'FNC':['Soaz','Reignover','Reignover','Steelback','Yellowstar'],
    15         'AHQ':['ZIV','Mountain','Westdoor','AN','Albis']
    16     },
    17 }
    18 LOL['LCK']['SKT'][0] = 'QWE'   #修改
    19 print(LOL['LCK']['SKT'])
    20 >>>['QWE', 'Blank', 'Faker', 'Bang', 'Wolf']
    View Code

    其它:

     1 LOL = {
     2     'LPL':{
     3         'RNG':{},
     4         'EDG':{},
     5         'We':{},
     6     },
     7     'LCK':{
     8         'SKT':{},
     9         'Samsung':{},
    10         'KT':{},
    11     },
    12     'LCS':{
    13         'FNC':{},
    14         'TSM':{},
    15         'G2':{},
    16     }
    17 }
    18 
    19 #values
    20 print(LOL.values())
    21 >>>dict_values([{'RNG': {}, 'EDG': {}, 'We': {}}, {'SKT': {}, 'Samsung': {}, 'KT': {}}, {'FNC': {}, 'TSM':{},'G2':{}}
    22 
    23 #keys
    24 print(LOL.keys())
    25 >>>dict_keys(['LPL', 'LCK', 'LCS'])
    26 
    27 
    28 #setdefault
    29 print(LOL.setdefault('LPL','LCK'))
    30 >>>{'RNG': {}, 'EDG': {}, 'We': {}}
    31 
    32 #update
    33 A = {1:2,3:4,'LCS':'AHQ'}
    34 LOL.update(A)
    35 print (LOL)
    36 >>>{'LPL': {'RNG': {}, 'EDG': {}, 'We': {}}, 'LCK': {'SKT': {}, 'Samsung': {}, 'KT': {}}, 'LCS': 'AHQ', 1: 2, 3: 4}
    37 
    38 #items
    39 print (LOL.items())
    40 >>>dict_items([('LPL', {'RNG': {}, 'EDG': {}, 'We': {}}), ('LCK', {'SKT': {}, 'Samsung': {}, 'KT': {}}), ('LCS', {'FNC': {}, 'TSM': {}, 'G2': {}})])
    41 
    42 #通过一个列表生成默认dict,有个没办法解释的坑,少这个
    43 print(dict.fromkeys([1,2,3,4],'faker'))
    44 >>>{1: 'faker', 2: 'faker', 3: 'faker', 4: 'faker'}
    45 
    46 #循环字典
    47 1
    48 for LOL_list in LOL:
    49     print(LOL_list)
    50 >>> LPL
    51         LCK
    52         LCS
    53 2
    54 for LOL_list,LOL_lis2 in LOL.items():
    55     print(LOL_list,LOL_lis2)
    56 LPL {'RNG': {}, 'EDG': {}, 'We': {}}
    57 LCK {'SKT': {}, 'Samsung': {}, 'KT': {}}
    58 LCS {'FNC': {}, 'TSM': {}, 'G2': {}}
    View Code

    4.模块

    Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持,以后的课程中会深入讲解常用到的各种库,现在,我们先来象征性的学2个简单的。

    sys:

    1 import sys
    2 print(sys.argv)
    3 
    4 python.exe  import.py hello world
    5 >>>['/usr/local/import,py','hello','world']   #把执行脚本时传递的参数获取到了

    os:

    1 import os
    2 os.system('mkdir 123')
    3 #调用系统命令
    4 #在win上也适用

    两者结合:

     1 import os,sys 2 3 os.system(''.join(sys.argv[1:])) #把用户的输入的参数当作一条命令交给os.system来执行 

    自己写个模块:

    python tab补全模块

    1 import sys
    2 import readline
    3 import rlcompleter
    4    
    5 if sys.platform == 'darwin' and sys.version_info[0] == 2:
    6     readline.parse_and_bind("bind ^I rl_complete")
    7 else:
    8     readline.parse_and_bind("tab: complete")  # linux and python3 on mac
    for mac
     1 import sys
     2 import readline
     3 import rlcompleter
     4 import atexit
     5 import os
     6 # tab completion 
     7 readline.parse_and_bind('tab: complete')
     8 # history file 
     9 histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
    10 try:
    11     readline.read_history_file(histfile)
    12 except IOError:
    13     pass
    14 atexit.register(readline.write_history_file, histfile)
    15 del os, histfile, readline, rlcompleter
    for linux

    你会发现,上面自己写的tab.py模块只能在当前目录下导入,如果想在系统的何何一个地方都使用怎么办呢? 此时你就要把这个tab.py放到python全局环境变量目录里啦,基本一般都放在一个叫 Python/2.7/site-packages 目录下,这个目录在不同的OS里放的位置不一样,用 print(sys.path) 可以查看python环境变量列表。


  • 相关阅读:
    新零售解决方案体系架构
    设计模式-分类
    设计模式-设计原则
    一天一个 Linux 命令(12):tree 命令
    RabbitMQ中如何保证消息的可靠传输?如果消息丢了怎么办
    为什么使用MQ?
    一天一个 Linux 命令(11):cp命令
    数据结构和算法-线性查找-二分查找
    作图工具汇总
    Git 命令大全,Git命令汇总,Git命令说明
  • 原文地址:https://www.cnblogs.com/wazy/p/7737996.html
Copyright © 2011-2022 走看看