将一个浮点数组复制到字符串数组

时间:2014-02-05 02:45:14

标签: python arrays list matrix copy

#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 convertm。这应该做的是采用数字(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,但这不是我们正在讨论的内容!) 我完全不知道为什么会这样做,任何一点点的帮助都会非常感激!

谢谢你

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

1 个答案:

答案 0 :(得分:1)

该行

temps = [[]]*lm

列出一个列表,其中每个子列表指向内存中的相同列表。因此,如果您修改一个,则将其全部修改。这就是你看到你所看到的行为的原因。

将其更改为

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

获取不同的子列表。