Fifth lesson: Application of Python list

Source: Internet
Author: User
Tags python list

First, built-in list method
A = ' ASDSF '
List (a)

Returns a list of parameters that are optional iteration objects. The contents of the output still keep the elements and the order of the incoming iteration object.
If the argument is empty, an empty list is returned.

Ii. differences between xrange and range

2.1 Iteration meaning: Repeat many times to do something. Objects that implement the __iter__ method in Python are iterative, such as List,tuple,dict
The usage of 2.2 xrange
Xrange (Start, end, step)
Xrange generates the concept of a Xrange object, the generator.

>>> a = xrange (1,10)
>>> A
Xrange (1, 10)
>>> print Type (a)
<type ' xrange ' >
>>> Print A[0]
1
>>> B = Range (1,10)
>>> print type (b)
<type ' list ' >
>>> B[0]
1

2.3 Comparison

Range: Generate a List object directly
Xrange: It is a list object that is generated
Usage of xrange:
1. When we need to operate a very large data, and the memory is more tight, we can use Xrange to save memory.
2. Xrange generally used in the loop, for example, we only need to manipulate some of the data, instead of returning all the data to complete the operation, we recommend the use of xrange

For M in range (1000): [0-999]
if m = = 10:
print ' SSS '
Break

For M in Xrange (1000): [0-10] 11
for m = = 10:
print ' SSS '
Break

Third, the re-application of the list deduction
3.1 Take out 1-100 of all straight squared
[X*x for X in range (101)]

3.2 What's inside is not just numbers.
Generate string [' the%s '% x for x in range (10)]
Generating tuples [(x, Y) for x in range (2) for Y in range (2)]
Generate Dictionary Dict ([(x, Y) for x in range (2) for Y in range (2)])

Four, over and over again the reference-list is a mutable object


>>> a = [' I ', ' am ', ' lilei ']
>>> B = A
>>> ID (a)
38575480
>>> ID (b)
38575480
>>> a[2]= ' xiaoming '
>>> Print a
[' I ', ' am ', ' xiaoming ']
>>> Print B
[' I ', ' am ', ' xiaoming ']
>>> del A
>>> b
[' I ', ' am ', ' xiaoming ']
>>>

>>> a = ' ABCDEFG '
>>> B = A
>>> ID (a)
38588992
>>> ID (b)
38588992
>>> A[2]
C
>>> a[2] = ' D '
Traceback (most recent):
File "<stdin>", line 1, in <module>
TypeError: ' str ' object does not support item assignment

Discussion on the minor skills delete

A = []
Del a basic Delete

Del a[:] A large number of deletions, suitable for a in the data too much time to use. Performance is much better than the above.

Del C and del c[:] There's still a difference.

>>> a = [+ +]
>>> B = A
>>> del A
>>> b
[1, 2, 3]
>>> C = b
>>> C
[1, 2, 3]
>>> del c[:]
>>> C
[]
>>> b
[]

Modify and delete values, and the referenced content will change. Directly delete the variable, the original reference position does not change, but the reference quantity getrefcount decrease. This point must be noted.

Fifth lesson: Application of Python list

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.