Python Base Three
//sixth day to study python(2016/8/7)
32. In python , there are have an special type dictionary , it is same with oc.
such as:
dicOne = {'wyg':'write code change world', 'roy':'you cand do it', 'tom':'just do it'}
dicOne
->
{'wyg':'write code change world', 'roy':'you cand do it', 'tom':'just do it'}
dicOne['wyg']
->
'wirte code change world'
dicOne['wyg'] = 'believe youself'
dicOne
->
{'wyg':'believe youself', 'roy':'you cand do it', 'tom':'just do it'}
dicTwo = dict(wyg = 'just do it', roy = 'you can do it')
dicTwo
->
{'wyg':'just do it', 'roy':'you can do it'}
dicThree = dict((('r':'rrr'),('t':'ttt')))
dicThree
->
{'r':'rrr','t':'ttt'}
33. In python ,dictionary type we can use everywhere, so we should learn it more deeply.
fromkeys()
dict1 = {}
dict1.fromkeys((1, 2, 3))
->
{1:None, 2:None, 3:None}
dict1.fromkeys((1,2,3),'Number')
->
{1:'Number', 2:'Number', 3:'Number'}
dict1.fromkeys((1,2,3),('one','two','three'))
->
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
keys()
dict1 = dict.fromkey(range(5),'roy')
dict1
->
{0:'roy',1:'roy',2:'roy',3:'roy',4:'roy'}
for eachKey in dict1.keys():
print(eachKey)
->
0
1
2
3
4
5
values()
for eachValue dict1.values():
print(eachValue)
->
roy
roy
roy
roy
roy
items()
for eachItem in dict1.items():
print(eachItem)
->
(0, 'roy')
(1, 'roy')
(2, 'roy')
(3, 'roy')
(4, 'roy')
get()
dict1.get(0)
->
'roy'
print(dict1.get(100))
->
None
dict1.get(1,'no keyvalue')
->
'roy'
dict1.get(100,'no keyvalue')
->
'no keyvalue'
in , not in (key)
3 in dict1
True
100 in dict1
False
clear()
dict1.clear()
->
{}
copy() (light copy)
a = {1:'one',2:'two'}
b = a.copy()
c = a
id(a) id(b) id(c)
->
4346314824 4386886856 4346314824
c[3] = 'three'
a
->
{1:'one',2:'two',3:'three'}
b
->
{1:'one',2:'two'}
c
->
{1:'one',2:'two',3:'three'}
pop()
a.pop(2)
->
{1:'one',2:'three'}
popitem()
a.popitem()
-> rand pop an object
setdefault()
a = {1:'one'}
a.setdefault(2)
a
->
{1:'one',2:None}
a.setdefault(3,'three')
{1:'one',2:None,3:'three}
update()
b = {'roy':'wyg'}
a.update(b)
->
{1:'one',2:None,3:'three,'roy':'wyg'}
34. we have learned dictionary ,now we learn set continue.
num = {}
type(num)
<class 'dict' at 0x100229b60>
num = {1,2,3}
type(num)
<class 'set' at 0x10022e420>
in set ,all value is only but no support index. such as:
num2 = {1,2,3,4,5,5,6}
num2
->
{1,2,3,4,5,6}
num3 = set([1,2,3,4])
num3
->
{1,2,3,4}
now how can remove repeat value from list
such as:
a = [1,2,3,4,5,5,6]
b = []
for each in a:
if each not in b:
b.append(each)
b
->
[1,2,3,4,5,6]
now that we have learned set ,how to achieve it by set
a = list(set(a))
->
[1,2,3,4,5,6]
Python Base Three的更多相关文章
- Python Base of Scientific Stack(Python基础之科学栈)
Python Base of Scientific Stack(Python基础之科学栈) 1. Python的科学栈(Scientific Stack) NumPy NumPy提供度多维数组对象,以 ...
- Python Base Four
35. In python, file operation syntax is similar to c. open(file,'r',……) //the first parameters is ne ...
- Python Base One
//this is my first day to study python, in order to review, every day i will make notes (2016/7/31) ...
- Python Base Five
// 8 day(2016/8/11) 38. In python , it is oop. class Baskball: def setName(self, name): ...
- Python Base Two
//fourth day to study python 24. In python , how to create funcation. we can use def to define funca ...
- 2019-04-18 Python Base 1
C:\Users\Jeffery1u>python Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64 ...
- python base 64
python中base64编码与解码 引言: 在一些项目中,接口的报文是通过base64加密传输的,所以在进行接口自动化时,需要对所传的参数进行base64编码,对拿到的响应报文进行解码: Bas ...
- Python Base HTTP Server
import BaseHTTPServer import cgi, random, sys MESSAGES = [ "That's as maybe, it's still a frog. ...
- 基于Python+协程+多进程的通用弱密码扫描器
听说不想扯淡的程序猿,不是一只好猿.所以今天来扯扯淡,不贴代码,只讲设计思想. 0x00 起 - 初始设计 我们的目标是设计一枚通用的弱密码扫描器,基本功能是针对不同类型的弱密码,可方便的扩展,比如添 ...
随机推荐
- 二、antd pro 删除eslint检测
删除package.json 里 " pre-commit": "npm run lint-staged" 这个对象就可以.
- Unity3d 中键值监听方法
unity3d的api中没有负责监听键值的方法,不过unity的input类是通过c#类获取各类监听事件,所以我们可以通过c#类监听,方法如下: void OnGUI() { Event e = Ev ...
- 【Machine Learning is Fun!】1.The world’s easiest introduction to Machine Learning
Bigger update: The content of this article is now available as a full-length video course that walks ...
- 科技庄园(背包dp)---对于蒟蒻来说死了一大片的奇题
题目描述: Life种了一块田,里面种了一些桃树. Life对PFT说:“我给你一定的时间去摘桃,你必须在规定的时间之内回到我面前,否则你摘的桃都要归我吃!” PFT思考了一会,最终答应了! 由于PF ...
- 零拷贝详解 Java NIO学习笔记四(零拷贝详解)
转 https://blog.csdn.net/u013096088/article/details/79122671 Java NIO学习笔记四(零拷贝详解) 2018年01月21日 20:20:5 ...
- Python爬虫系列-Selenium+Chrome/PhantomJS爬取淘宝美食
1.搜索关键字 利用Selenium驱动浏览器搜索关键字,得到查询后的商品列表 2.分析页码并翻页 得到商品页码数,模拟翻页,得到后续页面的商品列表 3.分析提取商品内容 利用PyQuery分析源码, ...
- cols
题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵中 ...
- ccf 201712-2 游戏(Python实现)
一.原题 问题描述 试题编号: 201712-2 试题名称: 游戏 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 有n个小朋友围成一圈玩游戏,小朋友从1至n编号,2号小朋友坐 ...
- 2.什么是composer与packgist,composer的安装
目录 学习地址: composer与packgist关系图片 composer的安装; 配置composer 修改国内镜像 用composer安装与卸载插件 composer插件升级后报错 学习地址: ...
- MAC系统里安装 Python
首先MAC系统自带Python2.6/2.7.这是因为有些系统文件需要.但是对于我来说,我需要用到Python3,所以需要自己下载安装.这时候,就有一个非常强大的软件 Homebrew.(安装方法见官 ...