Python 当我访问对象上的列表时,它错误地返回为空列表
我正在为一个计算物理项目编写代码 我想知道如何通过向类的实例添加数组来实现python中的类 以下是我的原始代码部分:Python 当我访问对象上的列表时,它错误地返回为空列表,python,list,class,Python,List,Class,我正在为一个计算物理项目编写代码 我想知道如何通过向类的实例添加数组来实现python中的类 以下是我的原始代码部分: class Particle(): def __init__(self, (x,y,z), n, radius, neighbours): self.n = n self.x = x self.y = y self.z = z self.radius = radius n
class Particle():
def __init__(self, (x,y,z), n, radius, neighbours):
self.n = n
self.x = x
self.y = y
self.z = z
self.radius = radius
number = loadtxt("final_limited.txt", usecols=(0,), unpack=True, dtype = int)
c1,c2,c3,r = loadtxt("final_limited.txt", usecols=(1,2,3,5), unpack=True, dtype=float)
number_of_particles = len(number)
my_particles = []
overlap = []
contact_number = []
for i in range(number_of_particles):
n = number[i]
x = c1[i]
y = c2[i]
z = c3[i]
radius = r[i]
neighbours = []
particle = Particle((x,y,z), n, radius, neighbours)
my_particles.append(particle)
for particle1 in my_particles:
count = 0
for particle2 in my_particles:
distance = PBCdist(particle1, particle2)
sum_of_radii = Sum_radii(particle1, particle2)
if (distance < sum_of_radii) and (distance>0):
count +=1
olap = (Decimal(sum_of_radii) - Decimal(distance))
overlap.append(olap)
contact_number.append(count)
然后,在嵌套循环中:
for particle1 in my_particles:
count = 0
for particle2 in my_particles:
distance = PBCdist(particle1, particle2)
sum_of_radii = Sum_radii(particle1, particle2)
if (distance < sum_of_radii) and (distance>0):
count +=1
neighbours.append(particle2.n)
olap = (Decimal(sum_of_radii) - Decimal(distance))
overlap.append(olap)
contact_number.append(count)
并获取列表
此外,您知道是否有一个Numpy数据类型(如float)可以为我提供所需的20-21位小数吗?使用float数据类型我的代码当然更快(10倍),但我希望使用numpy类型,它允许所需的全部精度,而不是十进制精度。用空列表替换neights参数
def __init__(self, (x,y,z), n, radius, neighbours):
neighbours = [] # <- HERE
self.neighbours = neighbours
def uuu init_uuuu(self,(x,y,z),n,半径,邻域):
邻居=[]#是的,这句话也让我觉得很奇怪。可能这应该是“默认设置为None
,如果是默认设置,请使用空列表”技巧?
print my_particles[0].neighbours
def __init__(self, (x,y,z), n, radius, neighbours):
neighbours = [] # <- HERE
self.neighbours = neighbours