Advanced Python (iteration, function programming, the Collections class)

Advanced PYTHON

PYTHON iteration

@ Generator (Generator)

List List Builder generates very take up memory space, every time we are operating on a single element in the calculation using the time, so other elements occupy space is wasted. So if the elements in the list can be extrapolated according to some algorithm, so that we can continue in the cycle calculate the next element (only calculated once a), in order to avoid creating a complete list and memory intensive .
In Python we while circulating the calculation mechanism known as a generator: generator.

Grammar Builder created

List builder brackets [] to wrap parentheses () wrapping

# 列表生成器
data = [x + 1 for x in range(0, 10)]
print(data)             # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# 生成器
g = (x * x for x in range(10))
print(g)                # <generator object <genexpr> at 0x10a4d6318>

Builder traversal

Use generator elements are also very simple, it can be printed out directly recycled

Note: The generator creates over, consume only once. That is when the traversal once, when traversing a second time, the list is already empty

# 使用for循环
for i in g:
    print(i)
    
# 直接使用list打印输出
g = (x for x in range(10))
list(g)                         # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

You can also call Next function until the calculated position of the last element, but it is clear that this approach does not apply, and finally throws StopIteration error

g = (x * x for x in range(10))
next(g)                         # 逐个输出元素,遍历完后会抛出StopIteration异常

So we created a generator, basically never call next (), but loop to iterate through for, and do not care about StopIteration error, generator is very powerful, if extrapolated algorithm is complex, with a similar type of list generation time for circulation can not be achieved, can also be implemented as a function.

@ Iterator

What is iterable (Iterable)?

Object can be used for cycles collectively referred to as iterable: Iterable. Before learning of such aggregate data type, list, tuple, dict, set, str like; as well as the above described generator is iterable.

What is an iterator (Iterator)?

May be next () function call to the object and returns the next value continuously referred iterator. For example, the generator is a kind of iterator, but list, dict, str, not the Iterator. What mechanisms blanket? Iterator belongs data stream, an inert sequence of unknown length (just under a),

from collections.abc import Iterable,Iterator
a = [1,2,3,4]
g = (i for i in range(10))
isinstance(a, Iterable)             # True
isinstance(a, Iterator)             # False
isinstance(g, Iterator)             # True

# iter方法可以将生成器转化为迭代器
next(iter(a))                           # 1

Functional Programming

@ Higher-order functions

Variables can point to a function, the function name is variable , the function name is the variable to point to a function, such as for len () function, we can be seen as a variable len

Passed to the function , since variables can point to function, the function can accept variable, a function can accept another function as a parameter passed

x = len
x([i for i in range(5)])                # 5

def add(x, y, z):
    return z(x) + z(y)
add([1,2,3,4], [4,3,1,5,6], max)        # 10

@map/reduce

map receiving two arguments, a function, a subject is an iterative (the Iterable), each element of a sequence of function acts.

map function returns still iterable (Iterable).

# 将下面的序列元素与自身相乘
map(lambda x: x * x, [1, 2, 3])         
# 别忘记之前通过list包裹可迭代对象的例子
list(map(lambda x: x * x, [1, 2, 3]))   # [1, 4, 9]

reduce also the receiving two arguments, a function, a subject is an iterative (the Iterable), but this function must accept two parameters, reduce the calculations continue to the next element to make a cumulative basis .

reduce function returns the final calculation result.

from functools import reduce
reduce(lambda a,b: a+b, [1,2,3,4,5])        # 15

@filter function

filter is also receiving two arguments, a function, a subject is an iterative (the Iterable), but the results must be returned by the function is a Boolean, filter elements for retention and deletion according to true or false

filter function returns still iterable (Iterable).

# 对下列数据只保留能被2整除的数
list(filter(lambda x: x % 2 == 0, [1,2,3,4,5]))    # [2, 4]

@ Anonymous function Lambda

We defined function name can not be displayed, so you can use an anonymous function.

Anonymous functions can be saved to a variable, you can not save directly.

# 用变量保存
f = lambda x : x * 2
list(map(f, [1,2,3]))                     # [2, 4, 6]

# 直接使用
list(map(lambda x: x * 2, [1, 2, 3]))     # [2, 4, 6]

@ Ternary operator

  • [true] if [expression] else [false]
print("成年人") if 24 > 18 else print("未成年人")
list(map(lambda x: x ** 2 if x % 2 == 0 else x, [1,2,3,4,5]))

Collections class

@deque

deque and more similar usage list, which is the queue (FIFO) realized stack (last-out) of the can for the ordered sequence data operation ends.

deque supports both ends of the sequence or pop append the time complexity of O (1) is. list can also achieve the same operation, but its complexity is O (N).

from collections import deque

# 创建
d = deque([1,2,3])

# 添加
d.append(4)                 # deque([1, 2, 3, 4])
d.appendleft(0)             # deque([0, 1, 2, 3, 4])


# 删除
d.pop()                     # deque([0, 1, 2, 3])
d.popleft()                 # deque([1, 2, 3])


# 旋转
d.rotate(2)                 # deque([2, 3, 1])

Time computational efficiency, and deque comparison list data is inserted in the left through the magic function timeit

list_a = [x for x in range(1,1000000)]
deque_a = deque(list_a)
%timeit list_a.insert(0, 'a')
%timeit deque_a.appendleft('a')

carried out

1.72 ms ± 196 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
234 ns ± 16.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

@Counter and OrderedDict

Counter

A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts.

from collections import Counter

# 创建
a = list('Jeff')
c = Counter(a)                      # Counter({'J': 1, 'e': 1, 'f': 2})

# 遍历,子类具有父类的全部属性和方法
for k, v in c.items():
    print(k, v)

# 查看Top1的元素
c.most_common(1)                    # [('f', 2)]

OrderedDict

And Counter similar, but OrderedDict are ordered, disordered and Counter

from collections import OrderedDict

# unsorted dictionary
d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}

# sorted by key
OrderedDict(sorted(d.items(), key=lambda t: t[0]))

# sorted by value
OrderedDict(sorted(d.items(), key=lambda t: t[1]))

# sorted by length of the key string
OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))

r.popitem(last=False)           # ('pear', 1)

String Processing

@ Common treatment methods

s = " Hello, World "
len(s)
s.split(' ')
s.lstrip()

# 查找和替换
s.endswith(" ")
s.startswith(" ")
s.find("ll")
s.replace("World", 'Tomorrow')

# 去空格转成大写并按逗号分隔
list(map(lambda t: t.strip(), s.strip().upper().split(",")))    # ['HELLO', 'WORLD']

@format

t = '五'
a = 3.1415
print("星期%s" %t)                            # 星期五
# 保留2位小数
print("pi是%.2f" %a)                         # pi是3.14

# format写法(推荐这样写)
a = '2019-12-12'
b = '10'
print("{0}的温度是{1}度".format(a, b))        # 2019-12-12的温度是10度

Class time: datetime

datime class module is designed for processing time, it is one PYTHON standard library, the contents of complex and powerful, this only involves a number of commonly used functions:

  • Get Time
  • Converting string time
  • Time of extraction
  • Calculated between the date
# 获取时间
from datetime import datetime
print(datetime.now())                       # 2019-12-13 21:32:25.754216    
# 创建指定的时间
dt = datetime(2019, 12, 13, 20, 11)         # 2019-12-13 20:11:00
print(dt)


# 字符串与时间的转换
s1 = '20170901'
s1 = datetime.strptime(s1, '%Y%m%d')
s2 = "2019/05/03"
s2 = datetime.strptime(s2, '%Y/%m/%d')
s3 = "2019-09-17 20"
s3 = datetime.strptime(s3, '%Y-%m-%d %H')


# 时间的提取
dt.date()                                   
datetime.now().date()                       # datetime.date(2019, 12, 13)
# 属性year,month,day,hour,minute,second
dt.year                                     # 2019


# 日期间的计算
from datetime import datetime, timedelta
print(s3 - s1)                                               # 746 days, 20:00:00
s3 - timedelta(days=1, seconds=86400, hours=12, minutes=60)  # datetime.datetime(2019, 9, 15, 7, 0)

I / O

@ Read the file

Our data generally exist in the file, the program will need to load the file into the Python environment, so that it can be analyzed using python.
PYTHON standard library provides an implementation of some standard, we look together.

Read files require open () function, the parameters for the file name and identifier:

f = open("test2.txt", 'r', encoding="gbk")  # 指定文件编码和模式
data = f.readline()
f.close()
print(data)

'R' represents a file read means, after acquiring a file, using the read function to read data, the final step is to call close () method to close the file. After the file must be closed after use, because the file object will occupy the resources of the operating system. But each wrote a lot of trouble, we can use to integrate with keyword:

with open("test2.txt", 'r', encoding="gbk") as handle:
    data = handle.readlines()
print(data)                             # ['hello world\n', '\n', 'Today is 星期二']


list(map(lambda s : s.strip(),data))    # ['hello world', '', 'Today is 星期二']

@ Write file

Write files and read documents almost identical, the only difference is the identifier needs to be changed "w".

The first argument is the name of the file you want to open;
the second argument ( 'w') tells Python, we want to open this file in write mode. When opening a file designated read mode ( 'r'), write mode ( 'w'), additional mode ( 'a') or let you can read and mode ( 'r +') written to the file. If
you omit the mode of argument, Python will default to open the file read-only mode

with open("test2.txt",'w') as handle:
    handle.write("hello world\n")           # 注意,这里手动添加了换行符
    handle.write(" And fate")

If you want to write the file does not exist, open () function will automatically create it. However, in order to write ( 'w') mode when opening a file to be careful, because if the specified file already exists, Python clears the file before returning the file object.

Attached to the file

If you add content to give the file instead of overwriting existing content, you can open the file in append mode. When you open the file in append mode, Python will not clear the file before returning file object, you write to the line of the file will be added to the end of the file. If the specified file does not exist, Python will you create an empty file.

with open("test.txt",'a') as handle:
    handle.write("Today is Nice!\n")
    handle.write("We are happy!!\n")

Exception Handling

Python uses a special object called an exception to management errors that occur during program execution. Whenever an error occurs so that a loss of Python, it creates an exception object. If you write code to handle the exception (ie capture), the program will continue to run; if you are not abnormal processing (not captured), the program will stop and display a traceback, which contains reports about the exceptions.

Exceptions are the use of try-except block processing. try-except block so that Python in the implementation of the specified operation, once the abnormal know how to do. When using a try-except block, even if an exception occurs, the program will continue to run: Show friendly error messages you write, rather than make the user confused traceback.

while True:
    first_number = input("First Number: ")
    if first_number == 'q':
        break
    second_number = input("Second Number: ")
    if second_number == 'q':
        break
    try:
        answer = int(first_number) / int(second_number)
    except ZeroDivisionError as e:
        print(" 除数不可以为0!!")
    print(answer)

carried out

First Number: 10
Second Number: 2
5.0
First Number: 3
Second Number: 5
0.6
First Number: 10
Second Number: 0
 除数不可以为0!!

More than one exception

some_list = [1, 2, 3]
try:
    print(some_list[5])
    some_list.remove(4)
# except Exception as e:    # 也可以用Exception替代,不过不建议这样做 
except (IndexError, ValueError) as e:   # 使用tuple封装多个异常类型
    print("Caught again!")
    print(e)

Guess you like

Origin www.cnblogs.com/wjf0/p/12040590.html