The second lesson of the basic Python tutorial 0120

Source: Internet
Author: User

Learning lists and tuples today

1. Sequence

>>> a=[' RP ', 22]
>>> b=[' RY ', 55]
>>> Database=[a,b]
>>> Database
[' RP ', [' RY ', 55]]
>>>

2. Index

>>> hello= ' Nihao '
>>> Hello[0]
N
>>> #也可以这样写
>>> ' Nihao ' [0]
N
>>>

3. sharding

>>> numbers=[1,2,3,4,5,6,7,8,9,10]//number of use, positive count is starting from 0 counting, counting is also starting from 0. So the general last element is not good to take.
>>> Numbers[3:6]
[4, 5, 6]
>>> #分片
>>> Numbers[0:1]
[1]
>>> Numbers[7:10]
[8, 9, 10]
>>> Numbers[-1:-3]
[]
>>> Numbers[-3:-1]
[8, 9]
>>> numbers[-3:]//output until the last element
[8, 9, 10]
>>> Numbers[:3]//output from the first element to the specified element
[1, 2, 3]
>>> numbers[:]//Output all elements
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

A simple example:

>>> url=raw_input (' Please enter the URL: ')
Please enter the url:http://www.python.org
>>> Domain=url[11:-4]
>>> print "Domain name:" +domain
Domain Name:python
>>>

3.1 Shards--In larger steps

The above example increases the step size by 1, and the following can be used in different steps.

>>> Numbers[0:10:1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, ten]//A:B:C where A is the specified starting bit, B is the specified bottom, and C is the specified The step size.
>>> Numbers[0:10:3]
[1, 4, 7, 10]
>>> Numbers[3:6:3]
[4]
>>>

A little Tip

>>> Numbers[::4]//need to extract the first of every 4, just set the step to 4.
[1, 5, 9]

The step can be negative, not 0; (negative is inverted output)
>>> Numbers[8:3:-2]
[9, 7, 5]
>>>

Simple summary: If you want to get the output you need to know how to set the ABC parameter.

4. Sequence addition

>>> #序列相加
>>> [1,2,3,4]+[5,6,7]
[1, 2, 3, 4, 5, 6, 7]
>>> [1,2,3]+ ' World '//list and string cannot be joined together

Traceback (most recent):
File "<pyshell#35>", line 1, in <module>
[1,2,3]+ ' World '
Typeerror:can only concatenate list (not "str") to List
>>>

5. Multiplication

>>> ' Nihao '//String multiplication
' Nihao Nihao nihao nihao Nihao '
>>> [1,5]*3//List multiplication
[1, 5, 1, 5, 1, 5]
>>> x= ' Hello '//String multiplication
>>> x*5
' Hellohellohellohellohello '
>>> wo=[you]*6

Traceback (most recent):
File "<pyshell#42>", line 1, in <module>
Wo=[you]*6
Nameerror:name ' You ' are not defined
>>> wo =[none]*2//Here you can see that only the Pyhton definition of the keyword can not add ', but not the keyword will need to add ' ' to the character of the shape In-band operation.
>>> wo
[None, none]
>>>

7. Membership--in
>>> u=[' foo ', ' wo ', ' koo ']//list not allowed to find non-strings
>>> ' O ' in U
False
>>> ' foo ' in U
True
>>> sub= ' ni$ $wo%%ta '
>>> ' $ ' in sub
True
>>> ' P ' in ' Python '
True
>>>

8. Length minimum and maximum value


>>> members=[100,34,68]
>>> Len (members)
3
>>> min (members)
34
>>> Max (members)
100
>>> Len ([45,26])
2
>>> min ([45,26])
26
>>> len (' Jack ')
4
>>> min (' abcxyz ')
A
>>> Max (2+3,5+6)
11
>>>

9. List

9.1 List

>>> #list functions
>>> list (' hello ')//Use list to convert a string into a list
[' H ', ' e ', ' l ', ' l ', ' O ']
>>> x=[1,21,1]
>>> x[1]=2
>>> x
[1, 2, 1]
>>> name=[' Alice ', ' Bob ', ' Tom '
>>> del name[2]//delete elements from a list
>>> Name
[' Alice ', ' Bob ']
>>>

9.2 Shard Assignment

>>> name=list (' Perl ')
>>> Name
[' P ', ' e ', ' r ', ' L ']
>>> name[2:]
[' R ', ' L ']
>>> ' arr ' =name[2:]//string cannot be directly assigned if not list
Syntaxerror:can ' t assign to literal
>>> name[2:]=list (' arr ') can be assigned after//list
>>> Name
[' P ', ' e ', ' a ', ' R ', ' R ']

Note: The assignment of lists and lists

>>> number=[1,5]
>>> number[2:]=[2,3,4]
>>> number
[1, 5, 2, 3, 4]
>>> number=[1,2,3,4,5,6,7,8,9,10]
>>> number[2:9:2 ]=[11,12,13,14 "              //To specify a step assignment
>>> number
[1, 2, one, 4, ten, 6,, 8, all, ten]
>>> number[2:9:2]= [11,12,13,14,15 ]

Adding a 15 to the range of the original list can cause errors.
Traceback (most recent):
File "<pyshell#12>", line 1, in <module>
NUMBER[2:9:2]=[11,12,13,14,15]
Valueerror:attempt to assign sequence of size 5 to extended slice of size 4
>>>

9.3 List Methods

Append

>>> #列表方法--append Call Method: Object. Method (Parameter)
>>> lst=[1,2,3]
>>> Lst.append (4)
>>> LST
[1, 2, 3, 4]
>>> #append用于在列表末尾添加新的对象
>>>

Count

>>> #count用于统计某个元素在列表中的出现次数
>>> [' to ', ' being ', ' or ', ' not ', ' to ', ' being ', ' is ', ' a ', ' Question '].count (' to ')
2
>>> x=[1,2,[3,4],5,[3,4],[3,[7,8]]
>>> X.count (3)
0
>>> X.count ([3,4])
2
>>> X.count ([7,8])
0
>>>

Extend

>>> #extend用于在列表的末尾一次性追加另一个序列中的多个值 (You can expand your existing list with a new list)
>>> a=[1,2,3]
>>> b=[10,11,12]
>>> A.extend (b)
>>> A
[1, 2, 3, 10, 11, 12]
>>> #此操作看起来像连接, the difference is: The Extend method modifies the extended sequence, and the original connection returns a completely new list
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> a+b
[1, 2, 3, 4, 5, 6]
>>> A
[1, 2, 3]
>>>

Insert

>>> #insert用于将对象插入到列表中
>>> numbers=[1,2,3,4,5,6,7,8]
>>> Numbers.insert (3, ' four ')
>>> numbers
[1, 2, 3, ' Four ', 4, 5, 6, 7, 8]
>>> numbers[1:7:2]=[' R ']//There is a problem here, I specify that the start is 1, the end point is 7, every two steps will be ' R ' inserted, but doing so will cause the end of the new element with the insertion of the change,

Traceback (most recent calls last):// to not complete this requirement.
File "<pyshell#46>", line 1, in <module>
numbers[1:7:2]=[' R ']
Valueerror:attempt to assign sequence of size 1 to extended slice of size 3
>>>

Index

>>> #index用于从列表中找出某个值第一个匹配项的索引位置
>>> a=[' wo ', ' he ', ' ni ', ' hai ', ' You ', ' ta ']
>>> a.index (' wo ')
0
>>> a.index (' ji ')//Call index function not found, return error

Traceback (most recent):
File "<pyshell#52>", line 1, in <module>
A.index (' Ji ')
ValueError: ' Ji ' isn't in list
>>>

Pop

>>> #pop移除列表中的一个元素 (the last one by default)
>>> x=[1,2,3]
>>> X.pop ()
3
>>> x
[1, 2]
>>> X.pop (0)
1
>>> x
[2]
>>> x=[1,2,3,4,5,6,7,8,9]
>>> X.pop ([1:8:2])
Syntaxerror:invalid syntax

Remove

>>> #remove The first occurrence of a value in the list to be removed
>>> a=[' to ', ' being ', ' or ', ' not ', ' to ', ' being ', ' is ', ' a ', ' question ']
>>> a.remove (' be ')
>>> A
[' to ', ' or ', ' no ', ' to ', ' being ', ' is ', ' a ', ' question ']

Sort

>>> #sort用于在原位置对列表进行排序, sorting in the original position means changing the original list so that the elements can be arranged in a certain order, rather than simply returning a copy of the sorted list
>>> x=[1,2,3]
>>> X.sort ()
>>> x
[1, 2, 3]
>>> x=[5,9,4]
>>> X.sort ()
>>> x
[4, 5, 9]
>>> Sort (5,9,8)

Sorted//Is a function, called directly, without. Calling

>>> x=[7,8,2,9,4,0,9,8,6]
>>> y=x[:]
>>> y
[7, 8, 2, 9, 4, 0, 9, 8, 6]
>>> Y.sort ()
>>> y
[0, 2, 4, 6, 7, 8, 8, 9, 9]
>>> y=sorted (x)
>>> y
[0, 2, 4, 6, 7, 8, 8, 9, 9]
>>> sorted (' python ')
[' H ', ' n ', ' o ', ' P ', ' t ', ' Y ']
>>> sorted (' x ', ' BD ', ' C ')

Traceback (most recent):
File "<pyshell#98>", line 1, in <module>
Sorted (' x ', ' BD ', ' C ')
TypeError: ' str ' object is not callable
>>> Sorted (' d ', ' a ', ' C ')

Traceback (most recent):
File "<pyshell#99>", line 1, in <module>
Sorted (' d ', ' a ', ' C ')
TypeError: ' str ' object is not callable
>>>

Reverse//Just the opposite of sort

>>> #reverse将列表中的元素反向存放
>>> x=[3,2,1]
>>> X.reverse ()
>>> x
[1, 2, 3]
>>>

10. Advanced Sorting

CMP, which can be analogous to the strcmp function in a C-language string function

>>> CMP (42,39)
1
>>> CMP (' A ', ' Z ')
-1
>>> cmp (' iOS ', ' UMP ')
-1
>>> numbers=[8,2,9,7]
>>> Numbers.sort (CMP)
>>> numbers
[2, 7, 8, 9]
>>>

11. Tuples: Immutable sequences

>>>
(1, 2, 3)
>>> (a)
(1, 2, 3)
>>> (42.)
42.0
>>> (42,)
(42,)
>>> 6+ (40.)
46.0
>>> (42/8)
15
>>> (42)
126
>>> (40+2.)
126.0
>>> (40+2,)
(42, 42, 42)
>>>

Tuple

This function is to turn a sequence into a tuple

>>> tuple ([+])
(1, 2, 3)
>>> tuple (' abc ')
(' A ', ' B ', ' C ')
>>> tuple ((' Uji '))
(' U ', ' j ', ' I ')
>>>

The second lesson of the basic Python tutorial 0120

Related Article

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.