Python Learning record-1

Source: Internet
Author: User

Change in place, fast:

The first way:

>>> a=[1,2,3]

>>> B=a

>>> a.extend ([4,5])

>>> A

[1, 2, 3, 4, 5]

>>> b

[1, 2, 3, 4, 5]

The second way:

>>> a=[1,2,3]

>>> B=a

>>> a+=[4,5]

>>> A

[1, 2, 3, 4, 5]

>>> b

[1, 2, 3, 4, 5]

Merge:

>>> A

[1, 2, 3]

>>> B=a

>>> A=a+[4]

>>> A

[1, 2, 3, 4]

>>> b

[1, 2, 3]

For the two ways to traverse the dictionary:

>>> d={' A ': 1, ' B ': 2, ' C ': 3}

>>> for key in D:

... print (key, ' = = ', D[key])

...

b = 2

A = 1

c = 3

>>> list (D.items ())

[(' B ', 2), (' A ', 1), (' C ', 3)]

>>> for (Key,value) in D.items ():

... print (key, ' = = ', value)

...

b = 2

A = 1

c = 3

The For loop generally runs faster than the while counter. Because iterators run at the speed of the C language in Python, while the while loop version runs Python bytecode through a Python virtual machine.

ReadLines will read the file in memory at once, not suitable for large files.

Lists, as well as many other built-in objects, are not their own iterators, because they support opening iterators multiple times. For such an object, we must call ITER to start the iteration:

>>> l=[1,2,3]

>>> ITER (L) is L

False

>>> l.__next__ ()

Traceback (most recent):

File "<stdin>", line 1, in <module>

Attributeerror: ' List ' object has no attribute ' __next__ '

>>> A=iter (L)

>>> a.__next__ ()

1

>>> Next (a)

2

Two types of inverse of list parsing:

Slow:

>>> l=[1,2,3,4,5]

>>> for I in range (len (L)):

... l[i]+=10

...

>>> L

[11, 12, 13, 14, 15]

Fast:

>>> l=[x+10 for x in L]

>>> L

[21, 22, 23, 24, 25]

Python Learning record-1

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.