python中的列表与数组转换

Python025

python中的列表与数组转换,第1张

列表转换成数组或者数组转换成列表,操作如下(使用函数array 和 tolist):

from numpy import *

listS = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [20, 30, 40, 50, 60, 70, 80, 90, 100]]

print(listS)

temp_array = array(listS, dtype=object)

print(temp_array)

listR = temp_array.tolist()

print(listR)

python 读取文本文件内容转化为python的list列表,案例如下:

1、目的

读取cal.txt内容,然后通过python脚本转化为list内容

2、文件内容

cal.txt

12

13

14

15

16

3、Python编写cal.py脚本内容

#!/usr/bin/python

#coding = UFT-8

result=[]

fd = file( "cal.txt", "r" )

for line in fd.readlines():

    result.append(list(map(int,line.split(','))))

print(result)

for item in result:

    for it in item:

       print it

4、执行转换为List列表结果内容:

[[12], [13], [14], [15], [16]]

12

13

14

15

16

python中把int类型转换成列表的方法:首先将整数转换为字符串;然后输入【map((int,str(num))】命令转换为列表即可。

首先将整数转换为字符串,然后使用map()函数转换:

或者使用如下方法:

推荐课程:Python 手册