list, dictionary, tuple, collection data structure in Python _python

Source: Internet
Author: User
Tags pear sin

In this paper, the list, dictionary, tuple and collection data structure of Python are summarized in detail. Share to everyone for your reference. The specific analysis is as follows:

List:

Copy Code code as follows:
Shoplist = [' Apple ', ' mango ', ' carrot ', ' banana ']

Dictionary:
Copy Code code as follows:
Di = {' A ': 123, ' B ': ' Something '}

Collection:
Copy Code code as follows:
Jihe = {' Apple ', ' pear ', ' apple '}

META Group:
Copy Code code as follows:
t = 123,456, ' Hello '

1. List

Empty list: a=[]

Function method:

Copy Code code as follows:
A.append (3) >>>[3]
A.extend ([3,4,5]) >>>[3,3,4,5] #添加一个列表序列
A.insert (1, ' hello ') >>>[3, ' Hello ', 3,4,5]
A.remove (3) >>>[' Hello ', 3,4,5] #删除第一个出现的3, no 3 error
A.pop () >>>[' hello ', 3,4]
A.pop (0) >>>[3,4]
A.index (4) >>>1 #返回出现的第一个4的下标
A.count (3) >>>1 #列表中元素3的个数
A.sort >>>[3,4] #排序
A.reverse () >>>[4,3] #反序

Ways to delete an element:

Copy Code code as follows:
A.remove (3) #通过值删除元素, delete the first parameter worthy element
A.pop () #通过下标删除元素, deletes the last value of the list by default, and deletes the element with the parameter value labeled as a parameter
Del A[0] #通过下标删除元素,
Del A[2:4] #删除a表下标为2, element of 3
Del a[:] #删除a列表所有元素
Del a #删除列表

List derivation:

Copy Code code as follows:
VEC = [2,4,6]
[3*x for X in VEC if X<6] >>>[6,12] 3*2,3*4
VEC2 = [1,2,3]
[X*y for x in Vec for y in VEC2] >>>[2,4,6,4,8,12,6,12,18]

Nested list derivation:

Copy Code code as follows:
Mat = [
[1,2,3],
[4,5,6],
[7,8,9]
]
print ([[[Row[i] for row in mat] for i in [0,1,2]])
>>>[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Think: What will be the difference between list (Zip (MAT)) and list (Zip (*mat))

2. META Group

null tuple: t = ()
Tuple Assignment: T = (123,345)
T[0] >>>123
3. Dictionary

Copy Code code as follows:
D = {' Jack ': ' jack@mail.com ', ' Tom ': ' Tom@main.com '}
d[' Jack '] >>> ' jack@mail.com
d[' jim ' = ' Jim@sin.com ' >>>{' jim ': ' Jim@sin.com ', ' Jack ': ' jack@mail.com ', ' Tom ': ' Tom@main.com '}

Del d[' Jim '] >>>{' Jack ': ' jack@mail.com ', ' Tom ': ' Tom@main.com '}
List (D.keys ()) #将返回一个字典中所有关键字组成的无序列表
Sorted (D.keys ()) #将返回一个字典中所有关键字组成的排序列表
Dict () #构造函数可以直接从key Create a dictionary in-value pairs
Dict ([(' Tim ', 123), (' Tiny ', 234)]) >>>{' Tiny ': 234, ' Tim ': 123}

To create a dictionary in a derived style:

Copy Code code as follows:
{d2:d2+ ' @main. com ' for D2 in List (D.keys ())}
>>>{' Jack ': ' Jack@main.com ', ' Tom ': ' Tom@main.com '}

Exercise: Loop output Dictionary key value pairs:
Copy Code code as follows:
For Name,email in D.items ():
Print (Name,email)

4. Collection

Empty collection: A = set () ※ to create an empty collection, you must use Set ()

Demonstrate:

Copy Code code as follows:
Basket = {' Apple ', ' orange ', ' apple '} >>>{' orange ', ' Apple '} #注意重复的元素只显示一个
' Apple ' in basket >>>true
' Pear ' in basket >>>false

Mathematical operation of a set:

Copy Code code as follows:
A = set (' ABABCDABCA ') >>>{' C ', ' B ', ' A ', ' d '}
b = {' A ', ' B ', ' m '} >>>{' B ', ' A ', ' m '}
A-b >>>{' C ', ' d '}
B-a >>>{' m '}
A | b >>>{' C ', ' d ', ' B ', ' A ', ' m '}
A & B >>>{' A ', ' B '}
A ^ b >>>{' C ', ' d ', ' m '}

Set Derivation:

Copy Code code as follows:
{x for x on a if x not in ' AB '} >>>{' C ', ' d '}

I hope this article will help you with your Python programming.

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.