Python built-in ordered collection: list and tuple yes, one variable, one immutable

Source: Internet
Author: User

Python's built-in data types are lists: List:#注释list是一种可变的有序的集合, you can add and remove elements at any time

The elements in the list can be different data types such as integers, strings, nested lists

len( )Function: Gets the number of list elements

classmates . Append ( ' element ')   function: Append new element to list footer

classmates.insert(1, ‘元素‘)函数:通过索引得方式将新的元素追加至指定位置

classmates[]: by indexing the elements of each location

< Code>pop (i) function    : to delete the element at the specified position, where i is the index position

classmates[ 1]= ‘Sarah‘ :直接赋值给对应的索引位置替换元素

Case 1: For example, List all the classmates in the class name, you can use a list to represent, as follows: Classmates variable, is a list:

>>> classmates = [‘Michael‘, ‘Bob‘, ‘Tracy‘]

 >>> classmates

 < Span class= "string" >[  ' Michael ',   ' Bob ',   ' Tracy ']  

>>> Len ( Classmates) #列出元素个数
3


    #用索引来访问list中每一个位置的 element, remember that the index is starting from  0       
#当索引超出了范围时 , Python will report a indexerror error, so to ensure that the index does not cross over, you can 1] get it backwards.
>>> classmates[0] ‘Michael‘>>> classmates[1]‘Bob‘>>> classmates[2]‘Tracy‘>>> classmates[3]
>>> classmates[-1]
‘Tracy‘

#追加新元素到末尾

>>> classmates.append(‘Adam‘)>>> classmates[‘Michael‘, ‘Bob‘, ‘Tracy‘, ‘Adam‘]
#通过索引得方式将新的元素插入到指定的位置,比如索引号为1的位置
>>> classmates.insert(1, ‘Jack‘)>>> classmates[‘Michael‘, ‘Jack‘, ‘Bob‘, ‘Tracy‘, ‘Adam‘]
#要删除指定位置的元素,用pop(i)方法,其中i是索引位置:
>>> classmates. Pop ( 1)' Jack '>>> classmates[   ' Michael ',   < Span style= "Font-family:microsoft Yahei; Font-size:13px "> Tracy ']  

#将指定元素替换成别的元素 can be assigned directly to the corresponding index position

>>> classmates[1] = ' Sarah '
>>> classmates [ ' Tracy ']
< Strong> 
#list里面的元素的数据类型也可以不同, the list element can also be another list
#要拿到 ' php ' can write Span style= "COLOR: #ff0000" > p[1] or s[2][1] , So s can be seen as a two-dimensional array, similar to three-dimensional, four-dimension ... arrays, but rarely used.

# to note that s has only 4 elements, where s[2] Another list, which is easier to understand if you disassemble the


>>> L = [ ' Apple ', 123,
[‘asp‘, ‘php‘],True]
>>> len(s)4>>> p = [‘asp‘, ‘php‘]>>> s = [‘python‘, ‘java‘, p, ‘scheme‘]

#空数组

>>> L = []
>>> len(L)
0

Another ordered list in Python is called a tuple: tuple: #tuple和list非常类似, but the tuple does not allow additions and deletions, and so on. When defined, the elements of a tuple must be determined

#比如同样是列出同学的名字

#索引指定位置元素与list的方式相同

#只有1个元素的tuple定义时必须加一个逗号 , , to eliminate ambiguity:

>>> classmates = (‘Michael‘, ‘Bob‘, ‘Tracy‘)

>>> classmates = [i]

# #最后来看一个 "mutable" tuple: On the surface, the elements of a tuple do change, but not the elements of a tuple, but the elements of the list

 >>> t = (‘a‘, ‘b‘, [‘A‘, ‘B‘])

 > >> t[2][0] = ' X '

>>> t[2][1] = ‘Y‘>>> t(‘a‘, ‘b‘, [‘X‘, ‘Y‘])


list和tuple是Python内置的有序集合,一个可变,一个不可变。根据需要来选择使用它们。
 
 

Python built-in ordered collection: list and tuple yes, one variable, one immutable

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.