Python - List and Tuple (ordered set)

List: Python built-in data type is a list, an ordered set,

  • The number of elements used len () to get the list of
  • Data element type list may be the same or different, may be another list
>> s = ['python', 'java', ['asp', 'php'], 'scheme'] 
>>> len(s) 
4

 

Find:

Use the index to access elements (index starts from 0), the last element is the need to access, it can be used directly to obtain -1, and so on, the penultimate position -2 ...

Add to:

  1. Use append () method into the list footer
>>>class.append('admin')

     2. Insert (,) like insert elements specified index

>>>class.insert(1,'jk')

delete:

Delete the specified location only elements with pop (i), where i is the index position

>>>class.pop(1);

modify:

An element should replace other elements, it can be assigned directly to the corresponding index position

>>>class[0]='jungkook'

 

 

 

Tuple:

  • Tuple element may comprise a list table, an ordered set
  • Tuple with the same list is accessed by an index, but can not be modified
  • Tuple relatively safe, the element must be initialized in the definition
>>> t = (1) 
>>> t 
1 //因为这样进行定义,具有歧义,所以在python中这样进行定义 
>>> t = (1,) 
>>> t 
(1,)

空Tuple:

>>> t = () 
>>> t 
()

It contains two elements of Tuple:

>>> t = (1, 2) 
>>> t 
(1, 2)

"Variable" Tuple:

Tuple in constant refers to the point it unchanged, but point to List, you can modify the list of elements

>>> t = ('a', 'b', ['A', 'B']) 
>>> t[2][0] = 'X' 
>>> t[2][1] = 'Y' 
>>> t 
('a', 'b', ['X', 'Y'])

 

Published 40 original articles · won praise 4 · Views 844

Guess you like

Origin blog.csdn.net/weixin_44919969/article/details/103956006