Python|将数组转换为具有相同项的普通列表
先决条件: Python中的数组
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]