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 起 - 初始设计 我们的目标是设计一枚通用的弱密码扫描器,基本功能是针对不同类型的弱密码,可方便的扩展,比如添 ...
随机推荐
- 使用FileSystemWatcher组件监视日志文件
实现效果: 知识运用: FileSystemWatcher组件的Path属性 Filter属性 //要监视那些文件 默认为*.* Endinit方法 //结束在窗体上使用或有另一个组件使用的Fil ...
- How to restrict root user to access or modify a file and directory in Linux
Now in this article I will show you steps to prevent or restrict access of root user to access certa ...
- web.xml 中 resource-ref 的注意事项
配置说明: web.xml 中配置 <resource-ref> <description>Employees Database for HR Applications< ...
- HTML5<aside>元素
HTML5<aside>元素用来定义页面文档中主区域内容之外的内容,但之外的内容是与主区域内容相关的. 实例: <article> <h1>这个页面是我开始用htm ...
- HTTP无状态协议和session原理(access_token原理)
无状态协议是指协议对务处理没有记忆能力.缺少状态意味着如果后续处理需要前面的信息,则它必须重传,这样可能导致每次连接传送的数据量增大.另一方面,在服务器不需要先前信息时它的应答就较快. Http协议不 ...
- 【SAM】bzoj5084: hashit
做得心 力 憔 悴 Description 你有一个字符串S,一开始为空串,要求支持两种操作 在S后面加入字母C 删除S最后一个字母 问每次操作后S有多少个两两不同的连续子串 Input 一行一个字符 ...
- python--以1-31的数字作为结尾的列表?论英文好的重要性!
一.python基础教程第2板(修订版)[代码清单2-1]中有一段要求打印‘以1-31的数字作为结尾的列表’ 截取代码示例:endings =['st','nd','rd'] +17*['th'] + ...
- 命令行下创建MySQL数据库与创建用户以及授权
先以root用户登录mysql: C:\Users\XXX>mysql -u root -p 输入密码后登录,接下来操作如下: 1.创建数据库 语法:create schema [数据库名称] ...
- 实验二 JSP基本动态元素的使用
实验二 JSP基本动态元素的使用 实验性质:验证性 实验学时: 2学时 实验地点: 一 .实验目的与要求 1.掌握JSP中声明变量.定义方法.java程序片及表达式的使 ...
- Python 中的for,if-else和while语句
for 循环 功能 for 循环是一种迭代循环机制,迭代即重复相同的逻辑操作,每次的操作都是基于上一次的结果而进行的.并且for循环可以遍历任何序列的项目,如一个列表或者一个字符串 语法 for 循环 ...