Python beginner-A few small examples of lists and dictionaries

Source: Internet
Author: User
Tags stdin

1. Decoding and encoding

Unicode

Utf-8

Gbk

Conversion syntax format between character encodings: variable. Decode (' GBK '). Encode (' Utf-8 ') #decode解码, encode encoding

Expandtabs () #将tab处转换空格, default one tab=8 space

Example:>>> name = ' sy l '

>>> name.expandtabs (1) #如果括号内设定参数, parameters are converted into a few spaces

' Sy l '

>>> name.expandtabs (0)

' Syl '

Format () #字符串格式化

Example 1:>>> name = "I m {0},age {1}"

>>> Name.format (' Syl ', 73)

' I m syl,age 73 '

Example 2:>>> name = "I m {ss},age {DD}"

>>> Name.format (ss = ' syl1 ', dd=29)

' I m syl1,age 29 '

Example 3:>>> li = [2232,3333]

>>> name = "I m {0},age {1}"

>>> Name.format (*li) #如果格式化的时候传的参数是列表或元祖, add a * before the parameter

' I m 2232,age 3333 '

Example 4:

>>> name = "I m {ss},age {DD}"

>>> dic = {' ss ': 123, ' DD ': 456}

>>> Name.format (**dic) #如果格式化字符串的时候传的参数是字典, add two in front of the parameter *

' I m 123,age 456 '

Example 5:find () differs from index ()

>>> name = ' Syl1 '

>>> name.find (' s ')

0

>>> name.find (' B ')

-1

>>> Name.index (' s ')

0

>>> Name.index (' B ')

Traceback (most recent):

File "<stdin>", line 1, in <module>

Valueerror:substring not found

Example 6:join ()

>>> li = [' s1 ', ' S2 ']

>>> ' _ '. Join (LI) #列表连接

' S1_s2 '

2. List

Example 1:pop () #删除列表中的最后一个元素 and returns the contents of the deleted element

[' Oracle ', one, one, a, ' syl ', ' Wu ']

>>> Li.pop ()

' Wu '

>>> Li

[' Oracle ', one, A, a, ' syl ']

>>> name = Li.pop ()

>>> Name

' Syl '

>>> Li

[' Oracle ', 11, 22, 44]

3. Dictionaries

Example 1:

>>> dic = {' K1 ': 1234} #定义一个字典

>>> dic[' K1 '] #通过KEY获取字典的值

1234

>>> dic[' K2 '] #测试获取字典中没有的key是否会报错, resulting in an error, reflecting the advantages of the Get () method

Traceback (most recent):

File "<stdin>", line 1, in <module>

Keyerror: ' K2 '

>>> dic.get (' K1 ')

1234

>>> dic.get (' K2 ') #利用该方法获取没有的key值不会报错

>>> print dic.get (' K2 ')

None

>>> print dic.get (' K2 ', ' OK ') #如果获取的key没有, you can return the specified content by setting a second parameter

Ok

Example 2:

>>> ([A.fromkeys], ' t ')

{1: ' t ', 2: ' t ', 3: ' t '}

Update () #将两个字典的内容进行整合, if the consolidated dictionary key exists in the consolidated key during the integration process, replace the original value to ensure that there are no duplicate keys in the dictionary

Scan two-dimensional code, Changbai Mountain Souvenir

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/8D/E4/wKioL1iuilSRyP1mAAH4lkqQ0cE404.png-wh_500x0-wm_ 3-wmp_4-s_2600682946.png "title=" qq picture 20170217155213.png "Width=" "height=" 208 "border=" 0 "hspace=" 0 "vspace=" 0 " Style= "WIDTH:200PX;HEIGHT:208PX;" alt= "Wkiol1iuilsryp1maah4lkqq0ce404.png-wh_50"/>

This article from "No special in, lifelong Learning" blog, please be sure to keep this source http://996665552.blog.51cto.com/11199442/1900569

Python beginner-A few small examples of lists and dictionaries

Related Article

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.