gpt4 book ai didi

python - 如何限制列表中的位数?

转载 作者:行者123 更新时间:2023-12-01 06:06:51 25 4
gpt4 key购买 nike

这是我为化学从头开始程序编写的一小段代码。我在使 C1pos 列出小数点后正好 6 位数字时遇到问题。我尝试了多种方法,例如十进制函数,但到目前为止我还没有运气。

位置是一个定义,它将采用矩阵 (1,1,1)+(1,1,1) 并给出 (2,2,2) numpy 没有安装在我的服务器上。

class Position:

def __init__(self, data):
self.data = data

def __repr__(self):
return repr(self.data)

def __add__(self, other):

data = [] #start with an empty list

for j in range(len(self.data)):

data.append(self.data[j] + other.data[j])

return Position(data)


deltaa=2.000001
#Set number of steps +1
numx=2
numy=1
numz=1
Carbon=[1.070000,0.000000,0.000000]
startpos=[1.000000,1.000000,1.000000]
l=[]
top=os.getcwd()
lgeom=[]
for i in range(numx):
for j in range(numy):
for h in range(numz):
l=i, j, h
shift=list(l)
shiftdist=[ k*deltaa for k in shift]
C1pos=Position(Carbon)+Position(shiftdist)+Position(startpos)
ltempl[10]=' C1,'+str(C1pos).strip('[]').replace(' ','')

有什么建议吗?

EG:输出需要为C1,2.123456,3.123456,4.123456,数字永远不会超过10。

最佳答案

','.join("%.6f" % f for f in C1pos)

示例

C1pos = [2.123, 300.123456, 4.123456789]
print ','.join("%.6f" % f for f in C1pos)

输出

2.123000,300.123456,4.123457

关于python - 如何限制列表中的位数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7627116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com