Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the list derivation of Python?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "what is the list of Python?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the list of Python" article.

Grammar

1. Ordinary

[expression for target in iterable]

two。 With conditions

[expression for target in iterable if condition]

3. Nesting

[expression for target1 in iterable1 if condition1 for target2 in iterable2 if condition2... For targetN in iterableN if conditionN] instance x = [1, 2, 3, 4, 5] x = [I * 2 for i in x] print (x) # [2, 4, 6, 8, 10] y = [i for i in range (10)] print (y) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # get the second column of the matrix matrix = [1, 2, 3], [3, 5, 6], [7, 8 9]] col2 = [row [1] for row in matrix] print (col2) # [2,5,8] # get matrix principal diagonal elements matrix = [[1,2,3], [3,5,6], [7,8] 9]] diag = [matrix [I] [I] for i in range (len (matrix))] print (diag) diag2 = [matrix [I] [len (matrix)-I-1] for i in range (len (matrix))] print (diag2) # [1,5,9] # [3,5,7] # create an embedded list a = [0] * 3 for i in range (3)] print (a) a [1] [1] = 5print (a) # [0,0,0] [0,0,0], [0,0,0]] # [[0,0,0], [0,5,0]] # list b = [i for i in range (20) if I% 2 = = 0] print (b) # [0,2,4,6,8,10,12,14,16,18] # filter the word words = ['Great',' FishC', 'Brilliant',' Excellent'] at the beginning of F 'Fantistic'] res = [word for word in words if word.startswith (' F')] print (res) # [FishC', 'Fantistic'] # expand the 2D list matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] flatten = [col for row in matrix for col in row] print (flatten) # [1, 2, 3, 4, 5, 6, 7, 8, 9]

Equivalent to

Flatten = [] for row in matrix: for col in row: flatten.append (col) # Cartesian product fulljoin = [x + y for x in '12345' for y in 'abcde'] print (fulljoin) # [' 1a,'1b,'1c,'1d,'1e,'2a,'2b,'2b,'2e, 3a, 3b, 3c Ans = [[x, y] for x in range (10) if x% 2 = = 0 for y in range (10) if y% 3 = 0] print (ans) # [[0,0], [0,3] [2, 0], [2, 3], [2, 6], [2, 9], [4, 0], [4, 3], [4, 6], [4, 9], [6, 0], [6, 3], [6, 6], [6, 9], [8, 0], [8, 3], [8, 6], [8 9]] this is the content of the article "what is the list derivation of Python?" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report