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

Quickly master list in Python through 10 examples

編輯:Python

Click on the above “ Xiaobai studies vision ”, Optional plus " Star standard " or “ Roof placement

 Heavy dry goods , First time delivery 

List yes Python Built in data structures in , It can be used to store any data type or a mixture of different data types . In this paper , We will pass 10 An example to introduce python Medium list.

Let's get started !

#example 1
import numpy as np
a = [4,6,7,3,2]
b = [x for x in a if x > 5]
b
[6, 7]

We iterate through a list ( Iteratable object ) And get greater than 5 The elements of ( Conditions ).

The equivalent of for Cycle is :

b = []
for x in a:
if x > 5:
b.append(x)
b
[6, 7]

We can also do something about elements before they are placed in a new list :

#example 2
import numpy as np
a = [4,6,7,3,2]
b = [x*2 for x in a if x > 5]
b
[12, 14]

We multiply the eligible items by 2, Then put it in a list .

The third example is a string list :

#example 3
names = ['Ch','Dh','Eh','cb','Tb','Td']
new_names = [name for name in names if name.lower().startswith('c')]
new_names
['Ch', 'cb']

This time, the condition is to get the string with ”c” Elements that begin with . Because there are cases , So we first convert all the letters to lowercase .

An iteratable object is not necessarily a list , For example, we can iterate over a two-dimensional matrix Numpy Array .

#example 4
import numpy as np
A = np.random.randint(10, size=(4,4))
A
array([[1, 7, 4, 4],
[5, 0, 0, 6],
[7, 5, 8, 4],
[1, 3, 2, 2]])
max_element = [max(i) for i in A]
max_element
[7, 6, 8, 3]

We iterate over the matrix A And take the maximum value .

The list can store any data type .

#example 5
vals = [[1,2,3],[4,5,2],[3,2,6]]
vals_max = [max(x) for x in vals]
vals_max
[3, 5, 6]

We create a list of maximum values in each list .

There can also be multiple filter criteria .

#example 6
names = ['Ch','Dh','Eh','cb','Tb','Td','Chb','Tdb']
new_names = [name for name in names if
name.lower().endswith('b') and len(name) > 2]
new_names
['Chb', 'Tdb']

The above is to get the string with letters ”b” Ends with a length greater than 2.

We can combine multiple conditions with other logical operators :

#example 7
names = ['chb', 'ydb', 'thd', 'hgh']
new_names = [name for name in names
if name.endswith('b') | name.startswith('c')]
new_names
['chb', 'ydb']

Now let's consider the following nested list :

vals = [[1,2,3],[4,5,2],[3,2,6]]

We want to take each element out of the nested list , So the expected output is :

vals = [1,2,3,4,5,2,3,2,6]

This can be done in the following ways :

#example 8
vals = [[1,2,3],[4,5,2],[3,2,6]]
vals_exp = [y for x in vals for y in x]
vals_exp
[1, 2, 3, 4, 5, 2, 3, 2, 6]

Grammar may not seem very intuitive , Equivalent to for Cyclic phase comparison , It will be very intuitive .

For the nested list operation above , We can also add conditions .

text = [['bar','foo','fooba'],['Rome','Madrid','Houston'], ['aa','bb','cc','dd']]

We just need a length greater than 3 String in the nested list of .

#example 9
text_1 = [y for x in text if len(x)>3 for y in x]
text_1
['aa', 'bb', 'cc', 'dd']

We put conditions on nested lists , Not on a single element . therefore , equivalent for/if The loop syntax is as follows .

We can also set a condition for a single element .

#example 10
text_2 = [y for x in text for y in x if len(y)>4]
text_2
['fooba', 'Madrid', 'Houston']

We are now better at 4 Character string . Because the condition is on a single element , So equivalent nesting for/if loop :

Master this 10 An example will lead us to list To a higher level .

The good news !

Xiaobai learns visual knowledge about the planet

Open to the outside world

 download 1:OpenCV-Contrib Chinese version of extension module
stay 「 Xiaobai studies vision 」 Official account back office reply : Extension module Chinese course , You can download the first copy of the whole network OpenCV Extension module tutorial Chinese version , Cover expansion module installation 、SFM Algorithm 、 Stereo vision 、 Target tracking 、 Biological vision 、 Super resolution processing and other more than 20 chapters .
download 2:Python Visual combat project 52 speak
stay 「 Xiaobai studies vision 」 Official account back office reply :Python Visual combat project , You can download, including image segmentation 、 Mask detection 、 Lane line detection 、 Vehicle count 、 Add Eyeliner 、 License plate recognition 、 Character recognition 、 Emotional tests 、 Text content extraction 、 Face recognition, etc 31 A visual combat project , Help fast school computer vision .
download 3:OpenCV Actual project 20 speak
stay 「 Xiaobai studies vision 」 Official account back office reply :OpenCV Actual project 20 speak , You can download the 20 Based on OpenCV Realization 20 A real project , Realization OpenCV Learn advanced .
Communication group
Welcome to join the official account reader group to communicate with your colleagues , There are SLAM、 3 d visual 、 sensor 、 Autopilot 、 Computational photography 、 testing 、 Division 、 distinguish 、 Medical imaging 、GAN、 Wechat groups such as algorithm competition ( It will be subdivided gradually in the future ), Please scan the following micro signal clustering , remarks :” nickname + School / company + Research direction “, for example :” Zhang San  +  Shanghai Jiaotong University  +  Vision SLAM“. Please note... According to the format , Otherwise, it will not pass . After successful addition, they will be invited to relevant wechat groups according to the research direction . Please do not send ads in the group , Or you'll be invited out , Thanks for your understanding ~

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