Python Basics-Lists, Ganso, dictionaries, collections

Source: Internet
Author: User

List 1, list features

  The list is mutable!!

List is an ordered sequence of elements that can be added, removed, and accessed by subscript (index)

2. Simple and common operation

A. Accessing elements through the following table

1 Print (List1[0])      # #正序 2 Print (List1[-1])     # #倒序 3 5 ' 123 '     # #修改指定位置的值

B, Slice

1 Print (List[1:3])
3. Common operation:
1 ##append: Append element2List1.append ('Zhao Four')3 4 ##extend: Appends a sequence to the list, which can be a list, Ganso5List1.extend ([A])6 7 ##insert: Insert element at specified index8List1.insert (0,'haha')9 Ten ##pop: Delete the last element and return the deleted element OneA =List1.pop () # # Here's a value for the deleted element A  - ##删除指定索引处元素 - delList1[0] the  - ##remove: Delete the specified element if the element does not exist, an exception!!  - list1.remove (' haha ') -  + ##index: Gets the index of the element - Print(List1.index (1)) +  A ##count: There are several specified elements at Print(List1.count (1)) -  - ##reverse: Content reversal - List1.reverse () -  - ##sort: Sorting in List3.sort () -List3.sort (Reverse=true)#whether to reverse

Tuple (tuple) 1, definition

    Ganso is immutable!!

Another ordered list is called a tuple:a tuple. tuple and list are very similar, but once a tuple is initialized, the content cannot be modified

2, commonly used to exercise
1 Print(T1[0])#access elements by subscript2 Print(T1.index (1))#return element Index3 Print(T1.count (3))#returns the number of elements4 5 ##如下定义方式容易产生歧义, variables are treated as Integer data6T2 = (1)7 Print(Type (t2))8 ##如果元祖中只有一个元素, define as follows9t3= (1,)Ten Print(Type (T3)) One  A ##注意: The contents of the Yongzu element cannot be modified, but the element at the Subscript 2 position is list,list element can be modified -T = ('a','b', ['A','B']) -T[2][0] ='X' theT[2][1] ='Y' -  - ## Tuples are also available for slicing operations - Print(T[:2])

Dictionary (Diect)

1. Definition

use key - value (key-value) storage for extremely fast lookup speeds

The key value can be a string, integer, floating-point, Boolean

2. Common operation

#1, press key to access value: #2, Length Len #3, member operation in and not in
1 #get: Access element according to key value, if not present, return none2 Print(person1['name'])3 Print(Person1.get ('name'))4 Print(Person1.get ('name1','haha'))#If it does not exist, you can also set a default value5  6 7 dictionary operations: adding, modifying8 (if key does not exist, a new key-value pair will be added, and if present, the values of the key corresponding will be modified)9D = {'name':'Tom',' Age': 22}TenC |'Sex']='Mans' One Print(d)##输出结果: {' name ': ' Tom ', ' age ': $, ' sex ': ' Man '} A   -  - Dictionary operations: deleting the ##删除元素 - delPerson1[1] -   - ##pop: Delete the specified element + person1.pop (' age ') -  + ##删除整个字典 A delPerson1 at  - ##clear: Empty the dictionary - person1.clear () -  -  - ##update () in ##把两个字典合并在一起 -  tob=='name':'Tom',' Age':' A'} +D2 = {'Height': 170,'Tel': 110} - d.update (D2) the Print(d)

Collection (SET)

1. Definition

Do not support subscript, slice and other operations

Features: value unique, unordered (cannot be accessed with subscript)

2. Common operation

1 adding elements2Set4.add (100)3 4 splits the element to be passed in as an individual into the collection5Set4.update ('ABC')6 7 Delete Element8Set5.remove ('a')9 Ten gets the intersection of two sets OneS2 = S &S1 A  - Collection -S2 = S |S1 the  - difference sets, from s the same element minus and S1 -S2 = s-s1

  

Python Basics-Lists, Ganso, dictionaries, collections

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.