Board logo

标题: [问题求助] Python二维数组如何转换类型 [打印本页]

作者: netdzb    时间: 2020-12-4 11:55     标题: Python二维数组如何转换类型

[['1', '255']][['256', '509'], ['510', '763'], ['764', '1017'], ['1018', '1271'], ['1272', '1526']]
如何转换成
===>
[[1, 255]][[256, 509], [510, 763], [764, 1017], [1018, 1271], [1272, 1526]]

下面的代码直接报错
data = [['1', '255']][['256', '509'], ['510', '763'], ['764', '1017'], ['1018', '1271'], ['1272', '1526']]
datadata = int(data)
作者: ivor    时间: 2020-12-4 13:26

data = [['1', '255']][['256', '509'], ['510', '763'], ['764', '1017'], ['1018', '1271'], ['1272', '1526']]
你发的这个语法不正确,并非二维数组
  1. data = [['256', '509'], ['510', '763'], ['764', '1017'], ['1018', '1271'], ['1272', '1526']]
  2. data = [[int(y) for y in x] for x in data]
复制代码

作者: netdzb    时间: 2020-12-4 13:33

回复 2# ivor

我的代码有点笔误,问题已经解决,谢谢!
作者: wujunkai    时间: 2020-12-6 12:14

  1. >>> data = [['256', '509'], ['510', '763'], ['764', '1017'], ['1018', '1271'], ['1272', '1526']]
  2. >>> data = eval(str(data).replace("'",'').replace('"',''))
  3. >>> data
  4. [[256, 509], [510, 763], [764, 1017], [1018, 1271], [1272, 1526]]
复制代码
有点取巧
但不限维度




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2