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

The method of creating, converting and accessing Python two-dimensional list

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "Python 2D list creation, conversion and access methods", the content is detailed, the steps are clear, the details are handled properly, I hope this "Python 2D list creation, conversion and access methods" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

I. concept

The element of a two-dimensional list is still a list (nesting of lists), which is called a two-dimensional list.

You need to access the elements of a 2D list through row and column tags

Create a two-dimensional list 1 and append an one-dimensional column to generate a two-dimensional column

Generate a two-dimensional list with four rows and three columns

Row1 = [3,4,5] row2 = [1,5,9] row3 = [2,5,8] row4 = [7,8,9] matrix = [] matrix.append (row1) matrix.append (row2) matrix.append (row3) matrix.append (row4) print (matrix)

Output result:

[[3, 4, 5], [1, 5, 9], [2, 5, 8], [7, 8, 9]]

2. Directly assign values to generate two-dimensional lists

Define a two-dimensional list with three rows and four columns

Matrix = [[], []] matrix [0] = [3,4,5,6] matrix [1] = [8,7,9,5] matrix [2] = [0,2,5,8] print (matrix)

Output result:

[[3, 4, 5, 6], [8, 7, 9, 5], [0, 2, 5, 8]]

Conversion of one-dimensional column label to two-dimensional list 1. Conversion of one-dimensional list to two-dimensional list

Put all the numbers from 1 to 24 into a two-dimensional list of four rows and six columns in order.

# put all numbers from 1 to 24 in a four-row and six-column 2D list nums = [] for i in range (1,25): nums.append (I) martix = [] for k in range (4): row = [] for j in range (1,7): row.append (j + 6 * k) martix.append (row) for arr in martix: print (arr)

Output result:

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

[7, 8, 9, 10, 11, 12]

[13, 14, 15, 16, 17, 18]

[19, 20, 21, 22, 23, 24]

2. convert a two-dimensional list to an one-dimensional list

Flatten a two-dimensional list with three rows and five columns to an one-dimensional list

# flatten a two-dimensional list with three rows and five columns into one-dimensional list nums = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]] arr = [] for i in nums: for j in i: arr.append (j) print (arr)

Output result:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

3. Use NumPy to realize the variable dimension operation of the array.

Using reshape (m, n) provided by NumPy array to realize variable dimension of array

(1) one-dimensional array becomes two-dimensional array

In [31]: import numpy as npIn [32]: arr1 = np.arange (1p25) # arange () create an arithmetic array In [33]: arr2 = arr1.reshape (4,6) # reshape () one-dimensional to two-dimensional in [34]: arr2Out [34]: array ([1Mague 2,3,4,5,6], [7,8,9,10,11,12], [13,14,15,16,17,18] [19, 20, 21, 22, 23, 24]) In [35]: arr2 = arr1.reshape (3,8) In [36]: arr2Out [36]: array ([[1,2,3,4,5,6,7,8], [9, 10, 11, 12, 13, 14, 15, 16], [17, 18, 19, 20, 21, 22, 23, 24])

(2) convert a two-dimensional array to an one-dimensional array

In [36]: arr2Out [36]: array ([[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16], [17, 18, 19, 20, 21, 22, 23, 24]) In [37]: arr1 = arr2.reshape (1, 24) [0] In [38]: arr1Out [38]: array ([1,2,3,4,5,6]) 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]) 4. Access the 2D list

Access the 2D list through row and column tags (rows can be accessed through slicing operations)

1. Visit the line In [36]: arr2Out [36]: array ([[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16], [17, 18, 19, 20, 21, 22, 23, 24]) In [39]: arr2 [1] Out [39]: array ([9, 10, 11, 12, 13, 14, 15] 2. Access elements In [40]: arr2Out [40]: array ([[1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16], [17, 18, 19, 20, 21, 22, 23] 24]]) In [41]: arr2 [1] [2] # second row, third column Out [41]: 113. access to NumPy two-dimensional array In [42]: import numpy as npIn [43]: arr2Out [43]: array ([[1,2,3,4,5,6,7,8], [9, 10, 11, 12, 13, 14, 15, 16], [17, 18, 19, 20, 21, 22, 23] ) In [44]: arr2 [1] # access line Out [44]: array ([9, 10, 11, 12, 13, 14, 15, 16]) In [45]: arr2 [:, 0] # access column Out [45]: array ([1,9,17]) In [46]: arr2 [2,3] # access element Out [46]: 20 supplement: practical application of two-dimensional list

Use a two-dimensional list to output the ancient poem "quiet Night thinking" in different formats.

Horizontal version

There is a bright moonlight in front of the bed

Can it be hoar-frost on the ground

Looking up, I find the moon bright

Bowing, in homesickness I'm drowned

Vertical version

Raise the suspect bed low

The leader is the front.

Thinking about Di Ming

So last month

The village moon frost light

Online warm reminder: horizontal version is read from left to right, vertical version is read from right to left.

We create a new file in IDLE, in which we define four verses whose strings are "Silent Night thoughts", then define a two-dimensional list, use a nested for loop to output the ancient poems in horizontal version, then arrange the two-dimensional list in reverse order, and finally use the nested for loop to output the ancient poems in vertical version, the code is as follows:

Str1 = 'there is bright moonlight in front of the bed' str2 = 'like a cold frost on the ground' str3 = 'look up at the bright moon in the sky' str4 = 'look down and miss your hometown' verse = [list (str1), list (str2), list (str3) List (str4)] # define a two-dimensional list print ('\ nMurray-horizontal--\ n') for i in range (4): # cycle every line of ancient poetry for j in range (5): # loop every word (column) of each line If j = = 4: # if it is the last word in a line print (verse [I] [j]) # newline output else: print (verse [I] [j]) End='') # output verse.reverse () # sort the list in reverse order print ('\ nMusi-Vertical--\ n') for i in range (5): # Loop every word (column) of each line For j in range (4): # first line if j = = 3: # if it is the last line print (verse [j] [I]) # newline output Else: print (verse [j] [I] End='') # output without line wrapping

The running results are as follows:

Horizontal version

There is a bright moonlight in front of the bed

Can it be hoar-frost on the ground

Looking up, I find the moon bright

Bowing, in homesickness I'm drowned

Vertical version

Raise the suspect bed low

The leader is the front.

Thinking about Di Ming

So last month

The village moon frost light

> > >

Read this, the "Python two-dimensional list creation, conversion and access methods" article has been introduced, want to master the knowledge of this article still need to practice and use in order to understand, if you want to know more related articles, welcome to follow 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