1.列表

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

定义列表

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

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

 print(Country[0])
>>>'China'
print(Country[1])
>>>'England'
print(Country[2])
>>>'America'

同时取多个元素(切片)

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

追加:

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

插入:

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

修改:

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

删除:

 Country = ['China','England','America','New Zealand','Germany']
print(Country)
>>>['China','England','America','New Zealand','Germany']
del Country[1]           #选择要删除的下标
print(Country)
>>>['China', 'America', 'New Zealand', 'Germany']
Country.remove('America')     #删除制定值
print(Country)
['China', 'England', 'New Zealand', 'Germany']
Country.pop()            #删除最后一个
print(Country)
['China', 'England', 'America', 'New Zealand']

扩展:

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

拷贝:

 Country = ['China','England','America','New Zealand','Germany']
print(Country)
>>>['China', 'England', 'America', 'New Zealand', 'Germany']
Country_copy = Country
print(Country_copy)
>>>['China', 'England', 'America', 'New Zealand', 'Germany']

统计:

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

排序&翻转:

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

获取下标:

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

2.元组

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

语法

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

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

3.字典

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

 info = {
'':'faker',
'':'dopa',
'':'xiaohu',
'':'uzi',
}

字典的特性:

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

增加:

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

修改:

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

删除:

 del info['']
print(info)
>>>{'': 'faker', '': 'xiaohu', '': 'uzi', '': 'godv'}
info.pop['']
print(info)
>>>{'': 'faker', '': 'white', '': 'uzi', '': 'godv'}
info.popitem() #随机删一个

查找:

 info = {
'':'faker',
'':'dopa',
'':'xiaohu',
'':'uzi',
}
print(info[''])
>>>faker
print(info.get('')) #有就返回,没有就None
>>>faker

多级操作:

 LOL = {
'LCK':{
'SKT':['Huni','Blank','Faker','Bang','Wolf'],
'Samsung':['Semb','Dandy','Crown','Proy','Madlife'],
'KT':['Impact','Kakao','Pown','Deft','Mata']
},
'LPL':{
'RNG':['Letme','Mlxg','Xiaohu','Uzi','Ming'],
'EDG':['Mouse','Clearlove7','Souct','Iboy','Mikeo'],
'WE':['','Condi','Xiye','Mysitc','Zero']
},
'LCS':{
'TSM':['Svenskeren','Bjergsen','WildTurtle','Biofrost','Yellowstar'],
'FNC':['Soaz','Reignover','Reignover','Steelback','Yellowstar'],
'AHQ':['ZIV','Mountain','Westdoor','AN','Albis']
},
}
LOL['LCK']['SKT'][0] = 'QWE' #修改
print(LOL['LCK']['SKT'])
>>>['QWE', 'Blank', 'Faker', 'Bang', 'Wolf']

其它:

 LOL = {
'LPL':{
'RNG':{},
'EDG':{},
'We':{},
},
'LCK':{
'SKT':{},
'Samsung':{},
'KT':{},
},
'LCS':{
'FNC':{},
'TSM':{},
'G2':{},
}
} #values
print(LOL.values())
>>>dict_values([{'RNG': {}, 'EDG': {}, 'We': {}}, {'SKT': {}, 'Samsung': {}, 'KT': {}}, {'FNC': {}, 'TSM':{},'G2':{}} #keys
print(LOL.keys())
>>>dict_keys(['LPL', 'LCK', 'LCS']) #setdefault
print(LOL.setdefault('LPL','LCK'))
>>>{'RNG': {}, 'EDG': {}, 'We': {}} #update
A = {1:2,3:4,'LCS':'AHQ'}
LOL.update(A)
print (LOL)
>>>{'LPL': {'RNG': {}, 'EDG': {}, 'We': {}}, 'LCK': {'SKT': {}, 'Samsung': {}, 'KT': {}}, 'LCS': 'AHQ', 1: 2, 3: 4} #items
print (LOL.items())
>>>dict_items([('LPL', {'RNG': {}, 'EDG': {}, 'We': {}}), ('LCK', {'SKT': {}, 'Samsung': {}, 'KT': {}}), ('LCS', {'FNC': {}, 'TSM': {}, 'G2': {}})]) #通过一个列表生成默认dict,有个没办法解释的坑,少这个
print(dict.fromkeys([1,2,3,4],'faker'))
>>>{1: 'faker', 2: 'faker', 3: 'faker', 4: 'faker'} #循环字典
1
for LOL_list in LOL:
print(LOL_list)
>>> LPL
LCK
LCS
2
for LOL_list,LOL_lis2 in LOL.items():
print(LOL_list,LOL_lis2)
LPL {'RNG': {}, 'EDG': {}, 'We': {}}
LCK {'SKT': {}, 'Samsung': {}, 'KT': {}}
LCS {'FNC': {}, 'TSM': {}, 'G2': {}}

4.模块

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

sys:

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

os:

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

两者结合:

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

自己写个模块:

python tab补全模块

 import sys
import readline
import rlcompleter if sys.platform == 'darwin' and sys.version_info[0] == 2:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete") # linux and python3 on mac

for mac

 import sys
import readline
import rlcompleter
import atexit
import os
# tab completion
readline.parse_and_bind('tab: complete')
# history file
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter

for linux

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


day3-基础 列表,元组,字典,模块的更多相关文章

  1. python基础-列表元组字典

    1.列表和元组 列表可以对数据实现最方便的存储.修改等操作 names=["Alex","tenglan","Eric","Rai ...

  2. python中列表 元组 字典 集合的区别

    列表 元组 字典 集合的区别是python面试中最常见的一个问题.这个问题虽然很基础,但确实能反映出面试者的基础水平. (1)列表 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单.比如,统计 ...

  3. Python第三天 序列 5种数据类型 数值 字符串 列表 元组 字典 各种数据类型的的xx重写xx表达式

    Python第三天 序列  5种数据类型  数值  字符串  列表  元组  字典 各种数据类型的的xx重写xx表达式 目录 Pycharm使用技巧(转载) Python第一天  安装  shell ...

  4. **python中列表 元组 字典 集合

    列表 元组 字典 集合的区别是python面试中最常见的一个问题.这个问题虽然很基础,但确实能反映出面试者的基础水平. 1.列表 列表是以方括号“[]”包围的数据集合,不同成员以“,”分隔. 列表的特 ...

  5. python3笔记十八:python列表元组字典集合文件操作

    一:学习内容 列表元组字典集合文件操作 二:列表元组字典集合文件操作 代码: import pickle  #数据持久性模块 #封装的方法def OptionData(data,path):    # ...

  6. Python第三天 序列 数据类型 数值 字符串 列表 元组 字典

    Python第三天 序列  数据类型  数值  字符串  列表  元组  字典 数据类型数值字符串列表元组字典 序列序列:字符串.列表.元组序列的两个主要特点是索引操作符和切片操作符- 索引操作符让我 ...

  7. python_列表——元组——字典——集合

    列表——元组——字典——集合: 列表: # 一:基本使用# 1.用途:存放多个值 # 定义方式:[]内以逗号为分隔多个元素,列表内元素无类型限制# l=['a','b','c'] #l=list([' ...

  8. 2.9高级变量类型操作(列表 * 元组 * 字典 * 字符串)_内置函数_切片_运算符_for循环

    高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...

  9. python的学习笔记01_4基础数据类型列表 元组 字典 集合 其他其他(for,enumerate,range)

    列表 定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素 特性: 1.可存放多个值 2.可修改指定索引位置对应的值,可变 3.按照从左到右的顺序定义列表元素,下标从0开始顺序访问 ...

  10. Python 列表,元组,字典

    0)字符串切片 py_str = 'python' >>>py_str[0] #取第一个字符串,返回值为"p",超出范围会报错 >>>py_st ...

随机推荐

  1. HDU 1232 (畅通工程) 并查集经典模板题

    Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通 ...

  2. centos yum安装高版本php,apache,mysql

    1.检查当前安装的PHP包 yum list installed | grep php 或者   yum list installed php* 如果要删除,可执行 yum remove php.x8 ...

  3. 解决windows10活动历史记录删除问题

    Windows10日常办公过程中系统会记录很多活动历史记录信息,我是不希望我的活动历史记录随时可以被别人查看,虽然电脑有设置密码,但是办公电脑还是习惯将历史记录删除掉,并且永远禁用windows10的 ...

  4. java多线程-synchronized

    一.线程安全问题 多线程操作各自线程创建的资源的时候,不存在线程安全问题.但多线程操作同一个资源的时候就会出现线程安全问题.下例为两个线程操作同一个name资源时发生的问题. class TestSy ...

  5. oled屏幕配套取字模软件使用

    oled屏幕配套取字模软件使用 作者:李剀 出处:https://www.cnblogs.com/kevin-nancy/p/10531368.html欢迎转载,但也请保留上面这段声明.谢谢! **P ...

  6. 进入保护模式(一)——《x86汇编语言:从实模式到保护模式》读书笔记12

    之前已经做了一些理论上的铺垫,这次我们就可以看代码了. 一.代码清单 ;代码清单11-1 ;文件名:c11_mbr.asm ;文件说明:硬盘主引导扇区代码 ;创建日期:2011-5-16 19:54 ...

  7. 8086中断系统——《x86汇编语言:从实模式到保护模式》读书笔记04

    80X86中断系统 能够处理256个中断 用中断向量号0-255区别 可屏蔽中断还需要借助专用中断控制器Intel 8259A实现优先权管理 1.中断的分类 中断可以分为内部中断和外部中断. (1)内 ...

  8. 【linux】基础操作命令

    1.查找文件或者程序位置 root@ROUTER:~# which is hostapd/sbin/hostapd

  9. Android开发过程中部分报错解决方法。

    初学Android,最近在使用zxing开发一个条码扫描解析的安卓项目中,遇到以下几个问题.贴出来以供参考. 1.Http请求错误    Android4.0以上要求不能把网络请求的操作放在主线程里操 ...

  10. C# HashTable 使用用法详解

    C#中如何操作HashTable类呢?本文将给你答案,哈希表(Hashtable)简述在.NET Framework中, 一,Hashtable是System.Collections命名空间提供的一个 ...