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

[experience sharing] derivation of Python skills

編輯:Python

brief introduction

List derivation (List Comprehensions) Also from functional programming languages , You can filter and process a sequence of data gracefully to get a result list .

such as , Put all the items in a list that are greater than 0 The number of squares after the production of a new list .

num = [-2, 3, 5, -4, 10, 7]

As usual , We will

num = [-2, 3, 5, -4, 10, 7]
result = []
for x in num:
if x > 0:
result.append(x**2)
print result

then , In the form of list derivation , It only needs

print [x**2 for x in num if x > 0]

form

How to interpret this formula ?

image.png

Divide the equation into three parts : Output processing 、for loop 、if Judge . among if The judgment section is optional .

if The judgment part is used to filter .

The output processing part is flexible , Do whatever you want .

For example, there is a list A = ‘1’, ‘2’, ‘3’, We can int(x) for x in A, Convert all the elements of the list into int type .

The above is the most basic form . There are also slightly more complex forms , How to interpret it ?


Method

The method is , Apart from “ Output processing ” part , The others are from left to right .

1) Multiple for loop

[(x, y) for x in range(3) for y in range(5)]

ad locum , The processing part is (x, y), Press other buttons from left to right . Equivalent to

for x in range(3):
for y in range(5):
# processing section 

2) Nested list derivation

[[0 for y in range(3)] for x in range(5)]

ad locum , The processing part is 0 for y in range(3), Press other buttons from left to right . Equivalent to

for x in range(5):
# processing section 

3) Multiple if Judge

[(x, y) for x in range(3) if x>1 for y in range(5) if y>2]

ad locum , The processing part is (x, y), Press other buttons from left to right . Equivalent to

for x in range(3):
if x>1:
for y in range(5):
if y>2:
# processing section 

expand

1) Other derivation

Except for list derivation . And the generator derivation 、 Set derivation (python2.7 And above )、 Dictionary derivation (python2.7 And above ).

[x for x in range(3)] #-> List derivation
(x for x in range(3)) #-> Generator derivation
{x for x in range(3)} #-> Set derivation
{x:None for x in range(3)} #-> Dictionary derivation 

such as

>>> d = {'a':1, 'b':2, 'c':3}
>>> d2 = {v:k for k,v in d.items()}# Reverse mapping
>>> d2
{1: 'a', 2: 'b', 3: 'c'}
>>> d3 = {k:v.upper() for k,v in d2.items()}# Capitalize all values
>>> d3
{1: 'A', 2: 'B', 3: 'C'}

2)Python3 base64 Encoding and decoding

In the blue whale project , Some interface messages are sent through base64 Encrypted transmission , So in the process of interface automation , It is necessary to modify the transmitted parameters base64 code , Decode the received response message ;

for example : Operation platform fast_execute_script( Execute scripts quickly ) Of script_content

python3.x All Chinese characters are unicode code , and b64encode The argument to the function is byte type , So we must transcode first .

import base64
# encode code
# decode decode
s ='adsvsdega15s1dasda'
encodestr = base64.b64encode(s.encode('utf-8'))
print(encodestr)
# b'YWRzdnNkZWdhMTVzMWRhc2Rh'
print(str(encodestr, 'utf-8'))
# YWRzdnNkZWdhMTVzMWRhc2Rh
decodestr = base64.b64decode(encodestr)
print(decodestr)
# b'adsvsdega15s1dasda'
print(str(decodestr, 'utf-8'))
# adsvsdega15s1dasda

Blue whale wisdom cloud

This article is edited and released by Tencent blue whale Zhiyun , Tencent blue whale Zhiyun ( Short for blue whale ) The software system is a set of systems based on PaaS Technology solutions for , Committed to building an industry-leading one-stop automatic operation and maintenance platform . At present, the community version has been launched 、 Enterprise Edition , Welcome to experience .

  • Official website :https://bk.tencent.com/
  • Download link :https://bk.tencent.com/download/
  • Community :https://bk.tencent.com/s-mart/community/question

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