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. bzoj3196 二逼平衡树 树状数组套线段树

    题目传送门 思路:树状数组套线段树模板题. 什么是树状数组套线段树,普通的树状数组每个点都是一个权值,而这里的树状数组每个点都是一颗权值线段树,我们用前缀差分的方法求得每个区间的各种信息, 其实关键就 ...

  2. H5常见问题 微信踩过得坑

    微信页面内 click事件 只在a链接的时候有效,如果是div或者span之类  一定要加上样式 cursor:pointer 点击事件才生效. <div style="cursor: ...

  3. 转 Monitoring Restore/Recovery Progress

    ora-279 是可以忽略的报错 In general, a restore should take approximately the same time as a backup, if not l ...

  4. MongoChef

    简介 开源且免费,有商业版 可自动化生成查询语句 使用 最下面的 _id 是自动生成的,手动指定 {      .0,      "_id" : ObjectId("58 ...

  5. [TimesTen]TT7001: User authentication failed

    在使用sqldeveloper连接TimesTen一直报[TimesTen][TimesTen 11.2.2.8.0 ODBC Driver][TimesTen]TT7001: User authen ...

  6. TOJ 3488 Game Dice

    描述 In the game of Dungeons & Dragons, players often roll multi-sided dice to generate random num ...

  7. pat1004. Counting Leaves (30)

    1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...

  8. [Matlab] fprintf

    %s format as a string%d format with no fractional part (integer format)%f format as a oating-point v ...

  9. Ubuntu通过xinput禁用及启用联想笔记本的触摸板

    查看设备列表 通过xinput先查看一些都有哪些设备 xinput     #或者 xinput list 显示结果如下 ddd@ddd:~$ xinput list   Virtual core p ...

  10. Javaweb的get请求乱码解决

    get方式请求:即将参数放在URL中,因此这就涉及到URL的编码了 方式一:[推荐] 方式二: 前端编码: encodeURI(encodeURI("")) 后端解码: java. ...