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 - 5661

    /*H E A D*/ int main(){ int T=read(); while(T--){ ll a=read(); ll b=read(); ll c=read(); ll d=read() ...

  2. (转)SSH批量分发管理&非交互式expect

    目录 1 SSH批量分发管理 1.1 测试环境 1.2 批量管理步骤 1.3 批量分发管理实例 1.3.1 利用sudo提权来实现没有权限的用户拷贝 1.3.2 利用sudo提权开发管理脚本 1.3. ...

  3. centos7 中文乱码问题解决方法

    1.查看是否安装中文包 可以使用下面的命名查看系统是否安装了中文安装包. locale -a |grep "zh_CN" 没有输出,说明没有安装,输入下面的命令安装: yum gr ...

  4. jquery 添加关键字小插件

    模块化封装,兼容seajs /** * User: c3t * Date: 14-3-25 * Time: 下午4:16 */ define(function (require, exports, m ...

  5. unity5.5 5.6 使用c#6.0或7.0

    http://blog.csdn.net/davied9/article/details/77281393

  6. Python快速入门_1

    注释 # 用#号字符开头注释单行 """ 三个引号可以注释多行 三个引号可以注释多行 三个引号可以注释多行 """ 原始数据类型和运算符 ( ...

  7. 【Postman】Postman的安装和使用

    Postman一款非常流行的API调试工具.其实,开发人员用的更多.因为测试人员做接口测试会有更多选择,例如Jmeter.soapUI等.不过,对于开发过程中去调试接口,Postman确实足够的简单方 ...

  8. newInstance和new的区别(good)

    从JVM 的角度看,我们使用关键字new创建一个类的时候,这个类可以没有被加载.但是使用newInstance()方法的时候,就必须保证:1.这个 类已经加载:2.这个类已经连接了.而完成上面两个步骤 ...

  9. Log4Net 之初体验

    今天试了一下关于日志的一个插件——Log4Net 关于这个插件就不过多描述了,有很多人用,也挺好用比较方便,所以在此记录下使用过程. 一.建一个mvc 空网站 名字叫 Log4NetTest 二.下载 ...

  10. java程序: 倒计时的小程序 (GridPane, Timer, Calendar, SimpleDateFormat ...)

    倒计时程序 涉及到的东西: javafx简单的界面,布局,按钮,文本框,事件响应 java.util.Timer,用于定时 SimpleDateFormat用于在String和Date之间转换. ja ...