Python Base Three, pythonbasethree

Source: Internet
Author: User

Python Base Three, pythonbasethree

// Sixth day to study python (2016/8/7)

32. In python, there are have an special type dictionary, it is same with oc.

Such:

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': 'ieve 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 shoshould 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 =

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:

Num2 = {1, 2, 3, 4, 5, 6}

Num2

->

{1, 2, 3, 4, 5, 6}

Num3 = set ([1, 2, 4])

Num3

->

{1, 2, 3, 4}

Now how can remove repeat value from list

Such:

A = [1, 2, 3, 4, 5, 6]

B = []

For each in:

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 ())

->

[1, 2, 3, 4, 5, 6]

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.