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]

时间: 2024-10-07 06:32:59

Python Base Three的相关文章

Python Base Four

35. In python, file operation syntax is similar to c. open(file,'r',……) //the first parameters is necessary, other is optional ,the second parameters is 'r' by default if you want to open a file, you can use: f = open('//Users//wyg_mac//Desktop//Acco

Python Base Two

//fourth day to study python 24. In python , how to create funcation. we can use def to define funcation. such as: def MyFirstFuncation(): print('this is my first funcation') MyFirstFuncation() -> this is my first funcatio. if there are no MyFirstFun

Python base

//this is my first day to study python, in order to review, every day i will make notes (2016/7/31) 1. In python , there are many bulit-in funcation. you can user follow to find out hou many built-in funcation: dir(__builtins__) if you want get BIF d

Python Base Five

// 8 day(2016/8/11) 38. In python , it is oop. class Baskball:         def setName(self, name):                self.name = name         def kick(self):                print('my name is %s' % self.name)      baskball = Baskball()      baskball.setName

Python Base HTTP Server

import BaseHTTPServerimport cgi, random, sysMESSAGES = ["That's as maybe, it's still a frog.","Albatross! Albatross! Albatross!","It's Wolfgang Amadeus Mozart.","A pink form from Reading.","Hello people, and we

python base 64

引言: 在一些项目中,接口的报文是通过base64加密传输的,所以在进行接口自动化时,需要对所传的参数进行base64编码,对拿到的响应报文进行解码: Base64编码是一种“防君子不防小人”的编码方式.广泛应用于MIME协议,作为电子邮件的传输编码,生成的编码可逆,后一两位可能有“=”,生成的编码都是ascii字符.优点:速度快,ascii字符,肉眼不可理解缺点:编码比较长,非常容易被破解,仅适用于加密非关键信息的场合python2中进行Base64编码和解码>>> import ba

基于Python+协程+多进程的通用弱密码扫描器

听说不想扯淡的程序猿,不是一只好猿.所以今天来扯扯淡,不贴代码,只讲设计思想. 0x00 起 - 初始设计 我们的目标是设计一枚通用的弱密码扫描器,基本功能是针对不同类型的弱密码,可方便的扩展,比如添加SSH.SVN.phpmyadmin的弱密码扫描功能.我们设定启动方法是命令行,可以通过命令行指定扫描对象,以及扫描哪些弱密码. 既然是要求可扩展,那我们首先来编写一个通用的框架,然后通过添加POC的方法来实现扩展.在这个框架中,我们需要处理的事情包括: 初始化扫描对象(格式化URL.从文件或数据

python中super的使用

转自:http://python.jobbole.com/86787/ super() 的入门使用 在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能,这时,我们就需要调用父类的方法了,可通过使用 super 来实现,比如: class Animal(object): def __init__(self, name): self.name = name def greet(self): print 'Hello, I am %s.' % self.

ParisGabriel:Python无止境 day02

ParisGabriel Python 入门基础 补充: 主流3操作大系统 Windows: Winxp   Win7 Win8 Win10 Unix: Solaris(SUN) IOS(Apple移动端) Mac OS Linux  :(linux基于Unix 独立出来的系统) 安卓(Android) Ubuntu 16.04(当前教学版本) ReadHat CentOS 回顾: Ctrl + Alt + Enter :虚拟机全屏/退出全屏 Ctrl + Alt :释放鼠标 ctrl + sh