Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列表从字符串转换为int,但不存在';这是一个陷阱_Python_List_Class - Fatal编程技术网

Python 将列表从字符串转换为int,但不存在';这是一个陷阱

Python 将列表从字符串转换为int,但不存在';这是一个陷阱,python,list,class,Python,List,Class,首先,我会说我对编程了解不多,我试着搜索答案,但我甚至不知道在搜索引擎中键入什么。这就来了 class Point: def __init__ (self, x, y): self.x = x self.y = y def __str__ (self): return "Members are: %s, %s" % (self.x, self.y) 我有一个类,它用x和y坐标表示一个点 我有一个列表p

首先,我会说我对编程了解不多,我试着搜索答案,但我甚至不知道在搜索引擎中键入什么。这就来了

class Point:
    def __init__ (self, x, y):
        self.x = x
        self.y = y
    
    def __str__ (self):
        return "Members are: %s, %s" % (self.x, self.y)
我有一个类,它用x和y坐标表示一个点

我有一个列表
points=[]
,如果我手动将一个点添加到该列表中,例如
points.append(point(-1.0,3))
输出返回
(-1.0,3)
,我正在使用这些点进行一些计算,但我认为如果我将代码放在这里,这并不重要

事情变得棘手,因为我必须从文件中输入数字。我已经将它们添加到另一个列表中,并使用循环将它们追加。问题是列表在str中,如果我将其转换为int我会得到一个错误,因为十进制
。0
在我的作业中说,我必须保持与输入相同的格式

我不明白的是,当我像这样输入points.append(Point(-1.0,3))时,它是如何保持十进制的
.0
,是否可以从文件中获得与数字相同的输出格式


我试着把它转换成float,但是所有的坐标都有小数点。

伙计,别再发明轮子了。如果需要从文件导入数据,可以使用
numpy
和函数
loadtxt

我不明白你的文件是如何制作的,坐标的格式是什么。在一组代码
点中。追加(点(-1.0,3))
第一个数字是一个
浮点
第二个数字是一个整数。你想要哪种格式

例如,在
file.dat
中,您有
x,y
位置:

1 1
2 3
4 5
其中第一列是
x
位置,第二列表示
y
。然后你可以使用这个代码

import numpy as np
points = np.loadtxt('file.dat', dtype = 'int')

点中
您拥有文件中的所有位置,您可以使用切片来访问它们。

伙计,不要重新发明轮子。如果需要从文件导入数据,可以使用
numpy
和函数
loadtxt

我不明白你的文件是如何制作的,坐标的格式是什么。在一组代码
点中。追加(点(-1.0,3))
第一个数字是一个
浮点
第二个数字是一个整数。你想要哪种格式

例如,在
file.dat
中,您有
x,y
位置:

1 1
2 3
4 5
其中第一列是
x
位置,第二列表示
y
。然后你可以使用这个代码

import numpy as np
points = np.loadtxt('file.dat', dtype = 'int')

点中
您拥有文件中的所有位置,您可以使用切片来访问它们。

您可以使用此代码适当地转换输入,使用此尝试捕捉机制,我们首先尝试
int
,如果我们没有成功,我们将继续使用
浮点

def float_or_int(inp):
    try:
        n = int(inp)
    except ValueError:
        try:
            n = float(inp)
        except ValueError:
            print("it's not int or float")
    return n


input_1 = '10.3'
input_2 = '10.0'
input_3 = '10'

res1 = float_or_int(input_1)  
res2 = float_or_int(input_2)  
res3 = float_or_int(input_3)  

print(res1, type(res1))  # 10.3 <class 'float'>
print(res2, type(res2))  # 10.0 <class 'float'>
print(res3, type(res3))  # 10 <class 'int'>
def float_或_int(inp):
尝试:
n=int(inp)
除值错误外:
尝试:
n=浮点数(inp)
除值错误外:
打印(“它不是int或float”)
返回n
输入_1='10.3'
输入_2='10.0'
输入_3='10'
res1=浮点或整数(输入1)
res2=浮点或整数(输入2)
res3=浮点或整数(输入3)
打印(res1,键入(res1))#10.3
打印(res2,键入(res2))#10.0
打印(res3,键入(res3))#10

我不知道您的输入是如何存储在您正在阅读的文件/另一个列表中的,但是您知道如何解析单个输入。

您可以使用此代码适当地转换输入,使用此try-catch机制,我们首先尝试
int
,如果没有成功,我们将继续使用
float

def float_or_int(inp):
    try:
        n = int(inp)
    except ValueError:
        try:
            n = float(inp)
        except ValueError:
            print("it's not int or float")
    return n


input_1 = '10.3'
input_2 = '10.0'
input_3 = '10'

res1 = float_or_int(input_1)  
res2 = float_or_int(input_2)  
res3 = float_or_int(input_3)  

print(res1, type(res1))  # 10.3 <class 'float'>
print(res2, type(res2))  # 10.0 <class 'float'>
print(res3, type(res3))  # 10 <class 'int'>
def float_或_int(inp):
尝试:
n=int(inp)
除值错误外:
尝试:
n=浮点数(inp)
除值错误外:
打印(“它不是int或float”)
返回n
输入_1='10.3'
输入_2='10.0'
输入_3='10'
res1=浮点或整数(输入1)
res2=浮点或整数(输入2)
res3=浮点或整数(输入3)
打印(res1,键入(res1))#10.3
打印(res2,键入(res2))#10.0
打印(res3,键入(res3))#10
我不知道您的输入是如何存储在您正在阅读的文件/另一个列表中的,但您知道如何解析单个输入。

您可以使用以下方法:

proper_points = []
for x,y in points:
    float_x = float(x)
    int_y = int(y)
    coords = proper_points.append((x,y))
对于您的计算,您可以使用适当的_点列表而不是点

您可以使用以下内容:

proper_points = []
for x,y in points:
    float_x = float(x)
    int_y = int(y)
    coords = proper_points.append((x,y))

对于您的计算,您可以使用适当的_点列表而不是点

转换为
float
而不是
int
?我不理解您的问题……是的,只需使用
float()
。你可以将所有元素都设为浮点数,也可以只设它们为
.0
或类似的值。@Julien就像我在最后写的那样,如果我这样做,那么所有的坐标都会保留小数位。@bad_coder是的,这几乎就是这个问题的解决方案。转换为
浮点数
而不是
int
?我不理解您的问题……是的,只需使用
float()
。您可以将所有元素都设为浮点数,也可以只将它们设为
.0
或类似的值。@Julien就像我在结尾写的那样,如果我这样做,那么所有坐标都会保留小数点。@bad_coder是的,这就是这个问题的解决方案。这是如何回答这个问题的?它回答了这个问题,因为如果需要从文件导入一系列xy点,就没有必要创建一个类。例如,您可以使用一个numpy数组和我提到的函数。您没有格式问题,代码更干净。他说他在编程方面很新,然后我给出了一个有用的提示。第一次使用Python,所以我不知道那个库。谢谢你的提示。@not_so_smart如果你需要float或integer,你可以修改
dtype
参数。它还有其他功能,但这些功能与您的问题相关。这是如何回答问题的