程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python advanced programming Day1

編輯:Python

1. The principle and operation of tuples and lists

Calculate the time required to create tuples and lists :ipython Use in timeit

1.1 The difference between tuples and lists :

In terms of memory occupation and operation time , Tuples have more advantages than lists .

When the content does not change , Tuples are preferred ; When the content needs to change , Priority list .

1.2 Named tuples

collections Module namedtuple function

namedtuple: Receive two parameters , The name of the first type created , Second list

from collections import namedtuple
Fruits = namedtuple('Fruits', ['name', 'color', 'size'])
f = Fruits('watermelon', 'green', '3')
print(f.name)
# The output is : watermelon

1.3 Python There are three types of data :

  • The number :23
  • Sequence : character string , list , Tuples
  • hash : Dictionaries , aggregate

2. Principles and applications of dictionaries and collections

Dictionary derivation

The process of looking up values in dictionaries

Performance analysis :

Compare... In terms of time : aggregate , Dictionaries , Tuples , list

Compare from memory : Dictionaries , aggregate , list , Tuples

3. The derived type

3.1 List derivation

Take the traversed value to the front

lis_nu = [i for i in range(1,101)]
print(lis_nu)

3.2 Dictionary derivation

dit_nu = {i: i+1 for i in range(10)}
print(dit_nu)
> {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10}

3.3 generator

3.3.1 ()

advantage : To save memory , Improve performance

ge = (i for i in range(100))
a = next(ge)
print(a)
> 0

3.3.2 yield

yield adopt next() Value

3.4 iterator

An iterator can pass through next() Value

Iteratable object : Sure for Loop traversal is an iterative object

Convert iteratable objects to iterators :iter( Iteratable object )

A generator is a kind of iterator

3.4.2 send() function

def s_d():
for i in range(6):
j = yield i
print(j)
# send: Interact with the generator
dd = s_d()
print(next(dd))
print(dd.send(10))
》 The output is :
0
10
1

send(num) function , amount to next() function , also send(num) Function passed in num The value is equal to yield i


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved