Python Getting Started list and tuples

Source: Internet
Author: User

Get more information Welcome to my website or csdn or blog park

The previous one mainly learned about the installation of Python, and the first program Helloword, as well as simple input and output functions

Sequence

? This chapter is mainly about lists and tuples, and lists and tuples are the two of the six types of sequences, the main difference. The list can be modified, and tuples cannot. And the sequence is very useful, for example, to operate a group of people's name and age can be:

Peace=[' Peace One ', 23]
rong=[' Sisi ', 22]
Data=[peace,rong]
Data
[[' Peace One ', 23],[' Sisi ', 22]]
Sequences can contain other sequences, such as data containing peace and Rong sequences

Index

? 1, sequence name by ordinal index

PEACE[1]
23
DATA[1][1]
22
? 2, the string is indexed directly by ordinal index also can index the input
' Hello ' [1]
E
Two=raw_input (' Year: ') [1]
year:2015
Both
' 0 '

Shard Index,

? 1 similar to ordinal index, you can use Shard operations to access elements within a certain range. Shards are separated by colons to achieve;

Tag= ' My name is one peace '
TAG[11:20]
' One peace '
Note: The first index is the first element index of the Shard, the second index is the last element of the Shard element index +1, even if the +1 index above does not exist. It is also possible to make the last index in the same way;
Tag[-9:]
' One peace '
? 2 larger step size, followed by a colon and step after two indexes;
Tag[1:11:2]
' Ynm s '
Note: The same step size can also be negative, but when negative, it is output from back to forward. At this point the first index must be followed by the second index, for example:
Tag[11:1:-2]
' Os mn '

Sequence operations

? 1 added also known as connection

[1,2,3]+[3,4,6,]
[1,2,3,4,5,6]

Note: The two same types can be connected, for example: [1,2]+ ' Hello ' returns an error
2 multiply by multiplying by the number x, the resulting new sequence is repeated by the sequence x times

[42]*5
[42,42,42,42,42]
? 3 Membership, check if a value is in the sequence, you can use the in operation.
' P ' in tag
True
' PE ' in tag
True
' px ' in tag
False
? 4 length, maximum value, minimum value
#长度函数len
Len (TAG)
20
#最大值函数max
Max ([3,4,5])
5
MIN ([3,4,5])
3

List function

? 1,list function, create a list;

Str=list (' Hello ')
Str
[' H ', ' e ', ' l ', ' l ', ' O ']

List operations

? 1 change the list, element assignment

Str[1]= ' P '
Str
[' H ', ' P ', ' l ', ' l ', ' O ']
? 2 Delete element using Del to delete
Del Str[1]
Str
[' H ', ' l ', ' l ', ' O ']
? 3 Shard Assignment. The main functions are as follows:
#1, you can replace a shard with a sequence that is unequal to the original sequence
Str[1:]=list (' Peace ')
Str
[' H ', ' P ', ' e ', ' A ', ' C ', ' e ']
#2, you can insert an element without replacing any of the elements
Str[1:1]=list (' one ')
Str
[' H ', ' o ', ' n ', ' e ', ' P ', ' e ', ' A ', ' C ', ' e ']
#3, of course, can also be deleted,
Str[1:4]=list ()
Str
[' H ', ' P ', ' e ', ' A ', ' C ', ' e ']

List method

A method is a function that is closely related to an object. Direct object. Method to invoke
List has append (append at end), Count (count), extend (Append to new list), index (found element known), insert (insert by ordinal)
Pop (deleted by number) remove (delete by value) reverse (element Flip), sort (sort), sorted (get sorted result), special sort:
Sort (CMP) is sorted by comparison type, sort (Key=len) is sorted by the build function (this goes to length Len), and sort (reverse=true) is judged by True and false to determine whether to flip the sort;
The method operates as follows:

#append方法>>> name =List("Scott") >>> name [' s ',' C ',' O ',' t ',' t '] >>> Name.append (List("Tiger")) >>> name [' s ',' C ',' O ',' t ',' t ', ["',' t ',' I ',' G ',' E ',' R ']]>>> name =List("Scott") >>> name[' s ',' C ',' O ',' t ',' t ']>>> Name.append ("A","B")#添加多个元素即将报错Traceback (most recent): File"<stdin>", line1, in? Typeerror:append () takes exactly one argument (2given) >>> Name.append ("A") >>> name[' s ',' C ',' O ',' t ',' t ',' A ']#count方法>>> name =List("Scott") >>> name[' s ',' C ',' O ',' t ',' t ']>>> Name.count (' s ')1>>> Name.count ("T")2>>> Name.count ("A")0>>> Name.append (List("Python")) >>> name[' s ',' C ',' O ',' t ',' t ', [' P ',' y ',' t ',' h ',' O ',' n ']]>>> Name.count ([' P ',' y ',' t ',' h ',' O ',' n '])1#extend方法>>> name =List("Scott") >>> name [' s ',' C ',' O ',' t ',' t '] >>> Name.extend (List("Tiger")) >>> name [' s ',' C ',' O ',' t ',' t ',"',' t ',' I ',' G ',' E ',' R ']#index方法>>> name =List("Scott") >>> name[' s ',' C ',' O ',' t ',' t ']>>> Name.index (' t ')# #第一个字母t的索引位置是33>>> Name.index (' A ') Traceback (most recent): File"<stdin>", line1, in? ValueError:List. Index (x): X not inList>>>' A 'In nameFalse>>>' A 'Not in NameTrue#insert方法>>> name =List("Scott") >>> name [' s ',' C ',' O ',' t ',' t '] >>> Name.insert (2,' Tiger ')# #在索引为2的地方插入字符串tiger>>> name [' s ',' C ',' Tiger ',' O ',' t ',' t ']#pop方法>>> name =List("Scott") >>> name[' s ',' C ',' O ',' t ',' t ']>>> Name.pop ()' t '>>> name[' s ',' C ',' O ',' t ']>>> Name.append ("T") >>> name[' s ',' C ',' O ',' t ',' t ']#remove方法>>> name =List("Scott") >>> name[' s ',' C ',' O ',' t ',' t ']>>> Name.remove ("T")#去掉第一个t>>> name[' s ',' C ',' O ',' t ']>>> Name.remove ("A")#不存在会报错Traceback (most recent): File"<stdin>", line1, in? ValueError:List. Remove (x): X not inList>>>"A"Not in NameTrue>>> Name.remove ("S","C")#一次只能移除一个元素Traceback (most recent): File"<stdin>", line1, in? Typeerror:remove () takes exactly one argument (2Given#reverse方法>>> name =List("Scott") >>> name [' s ',' C ',' O ',' t ',' t '] >>> name.reverse () >>> name [' t ',' t ',' O ',' C ',' s ']#sort方法>>> result = [8,5,5,3,9] >>> result.sort () >>> result [3,5,5,8,9]#sorted方法>>> result = [8,5,5,3,9] >>> result2 = sorted (Result) >>> result [8,5,5,3,9] >>> RESULT2 [3,5,5,8,9]
Meta-group

Meta-group
Tuples, like lists, are also a sequence, the only difference is that tuples cannot be modified:

Tuple operations
>>> 1,2,3(123)>>> ()()#对于单个元素必须加上逗号,加上逗号后就表示数字是元组了>>> 4242>>> 42,(42,)>>> (42,)(42,)
Tuple tuples function
>>> tuple([1,2,3])(123)>>> tuple(‘abc‘)(‘a‘‘b‘‘c‘)>>> tuple((1,2,3))(123)
The mutual transformation between list and meta-group
>>> T=(‘cc‘,‘aa‘,‘dd‘,‘bb‘)>>> tmp=list(T)>>> tmp[‘cc‘‘aa‘‘dd‘‘bb‘]>>> T=tuple(tmp)>>> T(‘cc‘‘aa‘‘dd‘‘bb‘)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python Getting Started list and tuples

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.