简体   繁体   English

在python中从字符串列表列表转换为int列表列表

[英]Casting from list of lists of strings to list of lists of ints in python

I'm reading some numbers from a data source that represent xy coordinates that I'll be using for a TSP-esque problem. 我正在从数据源读取一些数字,这些数字表示将用于TSP式问题的xy坐标。 I'm new to python, so I'm trying to make the most of lists. 我是python的新手,所以我想尽量利用列表。 After reading and parsing through the data, I'm left with a list of string lists that looks like this: 读取并解析数据后,剩下的字符串列表如下所示:

[['565.0', '575.0'], ['1215.0', '245.0'], ...yougetthepoint... ['1740.0', '245.0']] [['565.0','575.0'],['1215.0','245.0'],... yougetthepoint ... ['1740.0','245.0']]

I would rather be dealing with integer points. 我宁愿处理整数点。 How can I transform these lists containing strings to lists containing ints? 如何将这些包含字符串的列表转换为包含int的列表? They don't seem to be casting nicely, as I get this error: 他们似乎表现不佳,因为出现此错误:

ValueError: invalid literal for int() with base 10: '565.0' ValueError:以10为基数的int()的无效文字:'565.0'

The decimal seems to be causing issues. 小数点似乎引起了问题。

x = [['565.0', '575.0'], ['1215.0', '245.0'], ['1740.0', '245.0']]
x = [[int(float(j)) for j in i] for i in x]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM