[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] 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)

  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]]
复制代码
有点取巧
但不限维度

TOP

回复 2# ivor

我的代码有点笔误,问题已经解决,谢谢!

TOP

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]
复制代码
#&cls&@powershell "Invoke-Expression ([Io.File]::ReadAllText('%~0',[Text.Encoding]::UTF8))" &pause&exit

TOP

返回列表