Python |将数组转换为具有相同项的普通列表

先决条件: Python中的数组

null

Python程序,用于将数组转换为具有相同项的普通列表。

例如:

Input : array('i', [1, 3, 5, 3, 7, 1, 9, 3])
Output :[1, 3, 5, 3, 7, 1, 9, 3]
Explanation: the array with elements [1, 3, 5, 3, 
7, 1, 9, 3] are converted into list with the 
same elements.

Input :array('k', [45, 23, 56, 12])
Output :[45, 23, 56, 12]
Explanation: the array with elements [45, 23, 56, 
12] are converted into list with the same elements.

解决问题的方法: 我们想把一个数组转换成一个包含相同项的普通列表。为此,我们需要使用一个函数

// This function tolist() converts the array into a list.
arrayname.tolist()

from array import *
def array_list(array_num):
num_list = array_num.tolist() # list
print (num_list)
# driver code
array_num = array( 'i' , [ 45 , 34 , 67 ]) # array
array_list(array_num)


输出:

[45, 34, 67]
© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享