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. hdu6183 Color it 线段树动态开点+查询减枝

    题目传送门 题目大意: 有多次操作.操作0是清空二维平面的点,操作1是往二维平面(x,y)上放一个颜色为c的点,操作2是查询一个贴着y轴的矩形内有几种颜色的点,操作3退出程序. 思路: 由于查询的矩形 ...

  2. HDU1286

    欧拉函数测试题 期末考试完了做下水题 更好的方法是phi[i]==i时就直接筛选,不改了 #include<bits/stdc++.h> using namespace std; cons ...

  3. Oracle PL/SQL之GROUP BY GROUPING SETS

    [转自] http://blog.csdn.net/t0nsha/article/details/6538838 使用GROUP BY GROUPING SETS相当于把需要GROUP的集合用UNIO ...

  4. casper爬虫操作记录

    phantom.outputEncoding = "gbk"; var fs=require("fs") //var data = fs.read(" ...

  5. 【总结】MYSQL注入

    关于MYSQL注入的总结,网上的资料很多,这里和大家简单分享下自己实战中常用的思路和命令 0x00   UNION联合查询型注入常用语句 order by n //定字段,n为正整数 union se ...

  6. Nginx + Lua搭建文件上传下载服务

    收录待用,修改转载已取得腾讯云授权 最新腾讯云技术公开课直播,提问腾讯W3C代表,如何从小白成为技术专家?点击了解活动详情 作者 | 庄进发 编辑 | 迷鹿 庄进发,信息安全部后台开发工程师,主要负责 ...

  7. cloudermanger安装时需要安装或彻底正确卸载再安装orcal-java7-installer、oracle-java7-set-default(ubuntu14.04版本)(图文详解)

    不多说,直接上干货! 安装orcal-java7-installer和oracle-java7-set-default 安装JDK1.7 (所有节点)CDH要求至少是Oracle JDK7,Ubunt ...

  8. 【shell】《shell学习指南》读书笔记

    一.使用shell脚本 优点:脚本语言能够轻易处理文件与目录之间的对象,如把文件从所有目录拷贝到另一个目录 缺点:效率不如编译型语言 二.简单的脚本 1.查看现在系统有谁登录 $who 2.算出行数 ...

  9. UWP 应用获取各类系统、用户信息 (1) - 设备和系统的基本信息、应用包信息、用户数据账户信息和用户账户信息

    应用开发中,开发者时常需要获取一些系统.用户信息用于数据统计遥测.问题反馈.用户识别等功能.本文旨在介绍在 Windows UWP 应用中获取一些常用系统.用户信息的方法.示例项目代码可参见 Gith ...

  10. java版两人聊天程序

    server.java import java.io.*; import java.net.*; import java.text.SimpleDateFormat; import java.util ...