Draft python (list, dictionary, tuple, string method, file read and write)

Source: Internet
Author: User

1. List

Define List

A = []

adding elements

A.append (' xx ') #在list末尾添加

A.insert (0, ' abc ') #在指定某位置添加元素, the specified subscript does not exist and is added at the end

modifying elements

A[0] = ' BB ' #找到元素下标, re-assigned

View

Print (a)

Print (A[0])

A.count (' abc ') #查看元素在list里的个数, does not exist return 0

A.index (' abc ') #查元素下标, repeat multiple, show first, do not exist then an error

Delete Element

A.pop () #默认删除最后一个元素, the value of the deleted element is returned, or the subscript is specified, the subscript is deleted, the error does not exist

A.remove (' xx ') #删除list里元素, if there are multiple, delete only one

Del A[0]

A.clear () #清空整个list

Slice

A[0:3]

#顾头不顾尾, the last one will not be evaluated

#[0:] [: 3] [:: 1] Step

It doesn't matter.

A.reverse () #反转list顺序

A.sort () #排序, default ascending, specified Reverse=true descending

Multidimensional arrays

A = [A]

b = [4,5,6]

A.extend (b) #合并两个list

2. Dictionaries

Dictionaries are unordered.

Defining dictionaries

A = {}

Check

A = {' BB ': ' CC '}

A.get (' BB ') #get不到, return None

Increase

a[' dd '] = ' ee '

A.setdefault (' ff ', ' GG ')

Modify

a[' dd '] = #key存在, re-assigned

By deleting

A.pop (' DD ') #因为字典是无序的, must pass key

A.popitem () #默认随机删除一个

del a[' BB ']

A.clear () #清空字典

3. String method

For i,t in Numerate (a): #可以同时循环下标和值

", strip () #去空格和换行符

", Split () #分割字符串

", Upper () #变成大写

", Lower () #变成小写

", Count (' AA ') #出现的次数

', '. Join () #把list变成字符串并且以, split

". EndsWith #判断以什么什么结尾, return to True,f

". StartsWith #判断以什么什么开头

". IsDigit () #判断是不是纯数字

4. File reading and writing

Open it

f = open (' xx ') #默认只读模式打开

R Read-only r+ read and write, file does not exist will error

W Write w+ Read and write, will empty the file

A + read and write, the file pointer by default at the end

F.seek (0) #移动指针到最前面, a + open need to use this

F.read () #读取文件里所有内容, returns a string, reads the pointer at the end

F.readline () #只读一行内容, return string

F.readlines () #读取文件里所有内容, returns list,list each element is a row of data

F.write (' AA ') #只能写字符串

F.writeslines () #写可迭代对象

F.tell () # Gets the position of the current file pointer

F.close #关闭文件

Draft python (list, dictionary, tuple, string method, file read and write)

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.