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. hdu1286 找新朋友 欧拉函数模板

    首先这一题用的是欧拉函数!!函数!!不是什么欧拉公式!! 欧拉函数求的就是题目要求的数. 关于欧拉函数的模板网上百度一下到处都是,原理也容易找,这里要介绍一下另一个强势模板. 在这一题的讨论里看到的. ...

  2. jar 启动关闭

    1.后台启动 startTest.sh #设置工程路径project_path=/root/testcd $project_path#nohup后台启动,输出日志到test.lognohup java ...

  3. python3.6 子类的__init__调用父类的__init__

    python3.6 子类的__init__调用父类的__init__ 父类 class worker: def __init__(self): self.a=1 self.b=2 if __name_ ...

  4. PIE SDK专题制图另存模板

    1.功能简介 在PIE SDK中,所有的制图元素.视图范围以及排版等都可以保存成一个模板,以供多次重复使用.使用模板时只需要打开该模板,加载相应数据,就可以直接出图,省去了重复制作图幅的麻烦,方便快捷 ...

  5. PIE SDK小波变换

    1.算法功能简介 小波变换是一种信号的时间——尺度分析方法,具有多分辨率分析的特点,而且在时频两域都具有表征信号局部特征的能力,是一种窗口大小固定不变但其形状可变,时间窗和频率窗都可变的时频局部化分析 ...

  6. 完全原生javascript简约日历插件,js、html

    效果图: 效果如图所示,尽管看上去并不是很美观,但是,基本上的功能还是已经完成了,码了一天多的时间,权当做复习一下js吧. 整个做下来差不多码了500多行代码~其实只是很多的样式也包括了在其中了,虽然 ...

  7. 有关tensorflow一些问题

    1.python版本 采用64位的python 2.系统不支持高版本tensorflow(>1.6),运行报错如下: 问题描述如下: ImportError: DLL load failed: ...

  8. LINUX学习之一:

    学好linux的基础:C语言(GNU C语言与GCC):硬件基础:熟悉操作系统内核代码,熟悉多线程和网络知识.分驱动开发(驱动程序模型即框架)和应用程序开发,目标是驱动开发 驱动开发特点: 不能使用标 ...

  9. pycharm + git实现两台电脑代码同步

    说明:此篇文章是作者方便日后查阅所有,请不要按本文的方法搭建 1.下载msysygit,https://git-for-windows.github.io/ 点击download获取下载链接,可能由于 ...

  10. (转)Linux 系统性能分析工具图解读(一、二)

    Linux 系统性能分析工具图解读(一.二) 原文:http://oilbeater.com/linux/2014/09/08/linux-performance-tools.html 最近看了 Br ...