gpt4 book ai didi

python - 将 float 组复制到字符串数组

转载 作者:太空宇宙 更新时间:2023-11-03 18:36:05 25 4
gpt4 key购买 nike

#ADD STRING MATRIX AND NUM MATRIX Fraction(3).limit_denominator(10)from fractions import Fraction
#ONLY WORKS FOR SQUARE ONES RIGHT NOW!
from fractions import Fraction

def make1(nm,x):
if nm[x][x]!=1:
print("Divide R1 by ",Fraction(nm[x][x]).limit_denominator(10))
tempr = multiply(nm[x],1/nm[x][x])
nm[x] = tempr
return nm

def convert(n):
try:
return float(n)
except ValueError:
num, denom = n.split('/')
return float(num) / float(denom)

def convertm(m):
lm = len(m)
lx = len(m[0])
tempn = [0]*lx
temps = [[]]*lm
print(temps)
cnt = 0
for x in m:
tempn = x
for n in x:
temps[cnt].append(str(Fraction(n).limit_denominator(10)))
print(n)
cnt+=1
print(temps)

def mprint(matrix):
s = [[str(e) for e in row] for row in matrix]
lens = [max(map(len, col)) for col in zip(*s)]
fmt = '\t'.join('{{:{}}}'.format(x) for x in lens)
table = [fmt.format(*row) for row in s]
print('\n'.join(table))

def subtract(r1,r2): #r1-r2
tempr = [0]*len(r1)
for x in range (0,len(r1)):
tempr[x] = r1[x]-r2[x]
return tempr

def multiply(r1,n):
tempr = [0]*len(r1)
for x in range (0,len(r1)):
tempr[x] = r1[x]*n
return tempr

def ans(nm):
end = len(nm[0])
cnt = 0
for x in nm:
cnt+=1
print("X",cnt,"=",x[end-1])

equ = int(input("How many equasions are in the linear system? "))
#unk = int(input("How many unkowns are in the linear system? "))
nm = [0] * equ
sm = [0] * equ
for x in range (0,equ):
tempinput = input("Please enter line "+str(x+1)+" of the matrix: ")
templist = [convert(n) for n in tempinput.split()]
nm[x] = templist
sm[x] = tempinput.split()

mprint(nm)

nm = make1(nm,0)

mprint(nm)

for p in range (0,equ-1):

for x in range (p,equ-1):
print("Subtract ",Fraction(nm[x+1][p]).limit_denominator(10),"*",p+1,"by",p+2)
tempr = multiply(nm[p],nm[x+1][p])
nm[x+1] = subtract(tempr,nm[x+1])
print("FIRST X: ",x,"P",x)
mprint(nm)
nm = make1(nm,p+1)
mprint(nm)

#GOIN BACK
for p in range (0,equ-1):
for x in range (0,equ-(p+1)):
print("Subtract ",x,"by",Fraction(nm[x][2-p]).limit_denominator(10),"*",3)
tempr = multiply(nm[2-p],nm[x][2-p])
nm[x]= subtract(nm[x],tempr)
print("SECOND X: ",x,"P",x)

mprint(nm)

ans(nm)

##or x in range (0,equ):
# print()

#g = nm[1][0]-1
#print("")
#tempr = multiply(nm[0],g/nm[0][0])
#nm[0]=tempr
#tempr = subtract(nm[1],nm[0])
#nm[0] = tempr

Pastebin of my code

好吧,我的实际问题是在未实现的(因为我无法让它工作)def conversionm 中。这样做的目的是获取带有数字 (nm) 的矩阵,并获取每个值,如果需要,将其转换为分数 (x/x) 形式的字符串,并将其存储在字符串矩阵 (sm) 中。这是我引用的代码段...

def convertm(m):
lm = len(m)
lx = len(m[0])
tempn = [0]*lx
temps = [[]]*lm
print(temps)
cnt = 0
for x in m:
tempn = x
for n in x:
temps[cnt].append(str(Fraction(n).limit_denominator(10)))
print(n)
cnt+=1
print(temps)

我添加了一些打印,以便尝试测试期间到底发生了什么。我得到的输出是所有行中重复的最后一行。我想我目前没有返回声明只是因为我一直在努力让它发挥作用。好的,举个例子,如果导入一个数组...

[[1,2,3],

[4,5,6],

[7,8,9]]

它将输出(将温度设置为)

[ ['7','8','9'],

['7','8','9'],

['7','8','9']]

我希望它输出(将温度设置为)

[['1','2','3'],

['4','5','6'],

['7','8','9']]

我也使用Python 3.3.1

(可能应该升级到 3.3.3,但这不是我们讨论的内容!)我完全不知道为什么要这样做,任何一点帮助都将非常感激!

谢谢

如果这种格式很糟糕,我也深表歉意,我是新手,我从另一个论坛复制/粘贴了这个,我非常想知道这里发生了什么。

最佳答案

线路

temps = [[]]*lm

创建一个列表列表,其中每个子列表都指向内存中的同一个列表。因此,如果您修改其中一项,那么您就修改了所有这些。这就是为什么您会看到您所看到的行为。

更改为

temps = [[] for _ in range(lm)] # xrange on python2

获取不同的子列表。

关于python - 将 float 组复制到字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21567664/

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