Python entry list and metadata

Source: Internet
Author: User

Python entry list and metadata

The main difference between a list and a group is that a list can be modified, but a list cannot be modified. In general, the list can replace tuples in almost all cases.

For example, the sequence can represent the information of one person in the database (name, age)

Copy codeThe Code is as follows:
>>> Edward = ['edward gumby', 42]

The sequence can also contain other sequences.

Copy codeThe Code is as follows:
>>> Edward = ['edward gumby', 42]
>>> John = ['John Smith ', 50]
>>> Database = [edward, john]
>>> Database
[['Edward gumby', 42], ['John Smith ', 50]

General sequence operations
All sequential operations can perform certain specific operations. These operations include indexing, partitioning, adding, multiplication, and checking whether an element belongs to a Sequence member.

Index

All the elements in the sequence are numbered-starting from 0. These elements can be accessed by numbers separately, as shown below:

Copy codeThe Code is as follows:
>>> Greeting = 'hello'
>>> Greeting [0]
'H'
>>> Greeting [-1]
'O'
>>> 'Hello' [1]
'E'

If a function call returns a sequence, you can index the returned result directly, for example:

Copy codeThe Code is as follows:
>>> Fourth = raw_input ('year: ') [3]
Annual: 2005
>>> Fourth
'5'
View Code
 
Running result:

Copy codeThe Code is as follows:
>>>
Annual: 1974
Month (1-12): 8
Day (1-31): 16
August 16th, 1974

Parts

You can use the sharding operation to access elements within a certain range. The sharding is implemented through two indexes separated by colons:

Copy codeThe Code is as follows:
>>> Tag = '<a herf = "http://www.python.org"> Python web site </a>'
>>> Tag [9: 30]
'Http: // www.python.org'
>>> Tag [32:-4]
'Python website'

The first index is the number of the first element to be extracted, and the last index is the number of the first element in the remaining part After partitioning.

Copy codeThe Code is as follows:
>>> Numbers = [1, 2, 4, 5, 6, 7, 8, 9, 10]
>>> Numbers [3: 6]
[4, 5, 6]
>>> Numbers [0: 1]
[1]

1. Elegant shortcuts

Access the last three elements. Of course, you can perform the display operation.

Copy codeThe Code is as follows:
>>> Numbers [7: 10]
[8, 9, 10]
>>> Numbers [-3:-1]
[8, 9]
>>> Numbers [-3: 0]
[]
>>> Numbers [-3:]
[8, 9, 10]

Only the last part completes the task. This method also applies to the elements starting with the sequence:

Copy codeThe Code is as follows:
>>> Numbers [: 3]
[1, 2, 3]

In fact, if you need to copy the entire sequence, you can leave both indexes blank:

Copy codeThe Code is as follows:
>>> Numbers [:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

2. Larger step size

There is also a third parameter for fragment-step size, which is usually set implicitly. In general, the step size is 1 and cannot be 0, but can be negative, that is, extract elements from right to left.

Test code
Sequence Addition

You can use the plus sign to connect the sequence:

Copy codeThe Code is as follows:
>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> 'Hello. '+ 'World! '
'Hello. world! '
>>> [1, 2, 3] + 'World! '

Traceback (most recent call last ):
File "<pyshell #107>", line 1, in <module>
[1, 2, 3] + 'World! '
TypeError: can only concatenate list (not "str") to list

Multiplication

Multiply the number x by a sequence to generate a new sequence. In the new sequence, the original sequence will be repeated x times.

Copy codeThe Code is as follows:
>>> 'Python' * 5
'Pythonpythonpythonpythonpython'
>>> [42] * 10
[42, 42, 42, 42, 42, 42, 42, 42, 42, 42]

Member qualifications

To check whether a value is in a sequence, you can use the in operator, which returns a Boolean value.

Copy codeThe Code is as follows:
>>> Permissions = 'rw'
>>> 'W' in permissions
True
>>> 'X' in permissions
False
Enter your name: mlh
True
>>> Subject = '$ Get rich now !!! $'
>>> '$' In subject
True

Length, minimum, and maximum

Built-in functions len, min, max, and len return the number of elements in the sequence. min and max return the largest and smallest elements in the sequence, respectively.

Copy codeThe Code is as follows:
>>> Numbers = [678, 34,]
>>> Len (numbers)
3
>>> Max (numbers)
678
>>> Min (numbers)
34
>>> Max (2, 3)
3
>>> Min (9, 3, 2, 5)
2

List Function

The list function can create a list based on strings.

Copy codeThe Code is as follows:
>>> List ('hello ')
['H', 'E', 'l', 'l', 'O']

Basic list operations:
1. Change the list: Element assignment

Index tags are used to assign values to specific and location-specific elements:

Copy codeThe Code is as follows:
>>> X = [1, 1]
>>> X [1] = 2
>>> X
[1, 2, 1]

2. Delete Elements

Use the del statement:

Copy codeThe Code is as follows:
>>> Names = ['Alice ', 'beth', 'ceil ', 'dee-dee', 'earl']
>>> Del names [2]
>>> Names
['Alice ', 'beth', 'dee-dee', 'earl']

Note: Cecil is completely deleted, and the list length also changes from 5 to 4.

3. multipart assignment

View Code
List Method:

A method is a function closely related to some objects. objects may be lists, numbers, strings, or other types of objects. method call method: object. Method (parameter)

1. append

The append method is used to append a new object to the end of the list:

Copy codeThe Code is as follows:
>>> Lst = [1, 2, 3]
>>> Lst. append (4)
>>> Lst
[1, 2, 3, 4]

2. cout

The count method is used to count the number of times an element appears in the list:

Copy codeThe Code is as follows:
>>> ['To', 'be', 'or', 'not', 'to', 'be']. count ('to ')
2
>>> X = [[], [, []
>>> X. count (1)
2
>>> X. count ([1, 2])
1

3. extend

The extend method can append multiple values in another sequence at a time at the end of the list.

Copy codeThe Code is as follows:
>>> A = [1, 2, 3]
>>> B = [4, 5, 6]
>>> A. extend (B)
>>>
[1, 2, 3, 4, 5, 6]
>>># Differentiate connection operations
>>> A = [1, 2, 3]
>>> B = [4, 5, 6]
>>> A + B
[1, 2, 3, 4, 5, 6]
>>>
[1, 2, 3]

4. index

The index method is used to locate the index location of a matching item from the list:

Copy codeThe Code is as follows:
>>> Knights = ['we', 'are ', 'the', 'knigths', 'who ', 'Say', 'ni']
>>> Knights. index ('who ')
4
>>> Knights = ['we', 'are ', 'the', 'knigths', 'who ', 'Say', 'ni']
>>> Knights. index ('herring ')

Traceback (most recent call last ):
File "<pyshell #184>", line 1, in <module>
Knights. index ('herring ')
ValueError: 'heller' is not in list

If it is not found, an exception is thrown.

5. insert

The insert method is used to insert objects to the list:

Copy codeThe Code is as follows:
>>> Numbers = [1, 2, 3, 5, 6, 7]
>>> Numbers. insert (3, 'four ')
>>> Numbers
[1, 2, 3, 'four ', 5, 6, 7]
>>># Same as the extend method, the insert method operation can also be implemented using the multipart assignment function.
>>> Numbers = [1, 2, 3, 5, 6, 7]
>>> Numbers [3: 3] = ['four ']
>>> Numbers
[1, 2, 3, 'four ', 5, 6, 7]

6. pop

The pop method removes an element from the list (the last one by default) and returns the value of this element:

Copy codeThe Code is as follows:
>>> X = [1, 2, 3]
>>> X. pop ()
3
>>> X
[1, 2]
>>> X. pop (0)
1
>>> X
[2]

Note: The pop method is the only list method that can modify the list and return element values (except None ).

7. remove

The remove method is used to remove the first match of a value in the list:

Copy codeThe Code is as follows:
>>> X = ['to', 'be', 'or', 'not ', 'to', 'be']
>>> X. remove ('be ')
>>> X
['To', 'or', 'not ', 'to', 'be']
>>> X. remove ('bee ')

Traceback (most recent call last ):
File "<pyshell #19>", line 1, in <module>
X. remove ('bee ')
ValueError: list. remove (x): x not in list

8. reverse

The reverse method reversely stores the elements in the list. This method also changes the list but does not return values.

Copy codeThe Code is as follows:
>>> X = [1, 2, 3]
>>> X. reverse ()
>>> X
[3, 2, 1]
9. sort

The sort method is used to sort the list in the original position and change the original list so that the elements in the list are sorted according to a certain number

Copy codeThe Code is as follows:
>>> X = [,]
>>> X. sort ()
>>> X
[1, 2, 4, 6, 7, 9]

Tuples
Like lists, tuples are also a sequence. The only difference is that tuples cannot be modified:

Ordered Set of any object
Offset storage
It belongs to an unchangeable sequence type
Fixed Length, heterogeneous, arbitrary nesting
Object reference array
If some values are separated by commas, A tuples are automatically created:

Copy codeThe Code is as follows:
>>> 1, 2, 3
(1, 2, 3)
>>> ()
()
>>> 42
42
>>> 42,
(42 ,)
>>> (42 ,)
(42 ,)

Tuples are also enclosed by parentheses (most of the time). Empty tuples can be expressed by two parentheses that do not contain content:

Tuple Function

The functions of the tuple function are basically the same as those of the list function: Use a sequence as a parameter and convert it to a tuples.

Copy codeThe Code is as follows:
>>> Tuple ([1, 2, 3])
(1, 2, 3)
>>> Tuple ('abc ')
('A', 'B', 'C ')
>>> Tuple (1, 2, 3 ))
(1, 2, 3)

Conversion between lists and tuples:

Copy codeThe Code is as follows:
>>> T = ('cc', 'AA', 'dd', 'bb ')
>>> Tmp = list (T)
>>> Tmp
['Cc', 'AA', 'dd', 'bb']
>>> T = tuple (tmp)
>>> T
('Cc', 'AA', 'dd', 'bb ')


How to convert lists, tuples, and strings in python

Python has three built-in functions: list, tuples, and strings. They are converted to each other using three functions: str (), tuple (), and list (), an example is as follows: >>> s = "xxxxx" >>> list (s) ['x ', 'X'] >>> tuple (s) ('x', 'x') >>> tuple (list (s) ('x', 'x') >>> list (tuple (s )) ['x', 'x'] to convert a list and a group to a string, you must rely on the join function.

Python problem list and tuples

X = abs (int (x ))
Print whole [x]

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.