Self-hing AI------------the basics of Python entry (2) list and Ganso

Source: Internet
Author: User

A. General sequence operation:

All sequences in the list can perform specific operations, including indexes (indexing). Shards (slicing). sequence addition (adding). Multiplication, membership, length, minimum, maximum, these are described below.

1. Index: The sequence is the most basic data structure in Python, all the sequences in the sequence are numbered, starting from 0, the elements of the sequence can be accessed by number, and positive and negative indexes are supported.

>>>string=' Hello '           #自定义变量并赋值 >>>print string[0]                 # Take elements by number
>>>print String[-1]

The final output is subscript 0 of the element ' H ' and ' O '

2. Shards: An index accesses a single element, using a shard to access a range of elements (without wrapping the packet), and the Shard is implemented by two indexes separated by a colon. In interactive mode, enter:

>>>number=[1,2,3,4,5,6,7,8 ]>>>nub=number[1:3]              #索引为正数 >>>nub2=numbwe[-3:- 1 ]           #索引为负数, the small index is placed in front, and the value from the countdown starts
>>>nub3=number[:] #也可不输入索引, the entire array is obtained
>>>nub4=number[0:7:2] #对于这种情况python为我们提供了另一个参数----Steps, the parameter is stealth set, also can understand the default step is 1>>>print nub
[2,3]>>>print NUB2
[6,7]
>>>print NUB3
[1,2,3,4,5,6,7,8]
>>>print NUB4
[1,3,5,7]

3. Sequence addition: Use Plus

>>>a=[1,2,3]>>>b=[4,5,6]>>>print A +b[1,2,3,4,5,6]>>>a='Hello'>>>b='Word'>>>c='!''Hello word!'

4. Multiplication:

>>>a=[5]*5>>>print a[5,5,5,5, 5 ]>>>b='None'*3>>>print b'  nonenonenone'

5. Membership: In order to detect whether a value exists with the sequence, the ' in ' operator, which is called a Boolean operator, returns the truth value called a Boolean.

>>>a='Hello Word'>>>print'L' inchatrue>>>b=['Zhangsan','Lisi','Wangwu']>>>print'Lisi' inchbtrue>>>c=[1,2,3,4,5]print6 inchCfalse

6. Length, maximum, minimum value:

>>>a=[100,500,300,900,1]>>>print  len (a)5>>>Print Max (a)900>>>print  min (b)1>>>print min (5,2,-7,-10) -10

Two. list (method)

1.append (): Add a new element at the end of the list

Syntax: list.append (obj)

2.count (): Calculates the number of occurrences of an element in a list

Syntax: list.count (obj)

3.extend (): Used to append multiple values from another sequence at the end of the list (the original list is expanded with a new list).

Syntax: list.extend (seq) list, SEQ stands for List of elements

4.index (): Used to find the index where a value matches the first of the list

Syntax: list.index (seq)

5.insert (): Used to insert an object into a list

Syntax: list.insertindex,obj) Index represents the index position at which the object obj needs to be inserted, and obj represents the object inserted in the list

6.pop (): Used to remove an element from the list (the last element by default), and returns the value of the element

Syntax: list.pop (obj=list[-1])

7.remove (): The first matching value to remove a value from a list

Syntax: list.remove (obj)

8.reverse (): Used to reverse the elements in the list

Syntax: list.reverse ()

9.sort (): Used to sort the original list

Syntax: list.sort (funce) funce as an optional parameter

10.clear (): Used to empty the list.

Syntax: list.clear ()

11.copy (): For copy list, similar to a[:]

Syntax: list.copy ()

12. Advanced sorting: If you want the elements to be sorted in a specific way (not the sort method by default in ascending order of the elements), you can customize it more conveniently

The sort method has two optional parameters, key and reverse, which are to be specified by name, which we call the keyword argument.

>>>field=['study','python',' is','Happy']>>>filed.sort (Key=len)#Sort by string by short to long>>>filed>>>filed.sort (key=len,reverse=true) #按字符串由长到短排序, passing two parameters>>>filed['python','study','Happy',' is'][' is','Happy','study','python']>>>num=[5,8,1,3,6]>>>num.sort (reverse=True) #排序后逆序>>>num[8,6,5,3,1]

Three. Tuples

A tuple in Python is similar to a list, except that tuples cannot be modified (the string mentioned earlier cannot be modified). The way to create tuples is simple: if you separate some values with commas, tuples are created automatically, such as:

>>>1,2,3($)>>>'hello','Word  '('hello','word')

You can also create an empty tuple:

>>> ()()>>> (all-in-all)
>>> (1,)
(1,)

1.tuple function: The function of the tuple function is basically the same as the list function, which takes a sequence as an argument and transforms it into a Narimoto group, and if the argument is a tuple, the parameter is returned as it is:

>>>tuple (['Hello', ' Word ']) ('Hello', ' Word ')>>>tuple ('Hello')('h','e','L','L','o')>>>tuple (('Hello', ' Word ')) ('Hello', ' Word ')

2. Basic operations of tuples:

1) Access to tuples: You can use the subscript index to access the values in the tuple

2) Modify tuples: The number of tuples in the tuple can not be modified, but the tuple can be connected together, using ' + ' to connect

3) Delete a tuple: the number in the tuple is not allowed to be deleted, but you can delete the entire tuple with the DEL statement only

4) tuple index, intercept: Because the tuple is also a sequence, you can access the element in the specified position in the tuple and also intercept an element in the index

3. Meta-set built-in functions:

1) Len (tuple) for calculating the number of tuples

2) max (tuple) used to calculate the maximum value in a tuple

3) min (tuple) is used to calculate the minimum value in a tuple

4) tuple (SEQ) to convert list to Narimoto Group

Code is more secure because tuples are immutable. If possible, you can use tuples instead of lists as much as possible. Such as:

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

Nested lists are used here, one list contains another list, or it can be called a two-dimensional array.

Self-hing AI------------the basics of Python entry (2) list and Ganso

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.