如何通过循环访问多维Python列表的每个元素?

时间:2015-07-16 07:14:54

标签: python python-2.7 list matrix input

import re
def get_number(element):
    re_number = re.match("(\d+\.?\d*)", element)

    if re_number:
        return float(re_number.group(1))
    else:
        return 1.0

def getvalues(equation):
    elements = re.findall("([a-z0-9.]+)", equation)
    return [get_number(element) for element in elements]


eqn = []
eqn_no = int(raw_input("Enter the number of equations: "))

for i in range(eqn_no):
    eqn.append(getvalues(str(raw_input("Enter Equation %d: " % (i+1)))))
print "Main Matrix: "
for i in range((eqn_no)):
    for j in range((eqn_no+1)):
        print "\t%f" %(eqn[i][j]),
    print
print
equation=[]
equation=eqn
for k in range((eqn_no-1)):
    for i in range((k+1),eqn_no):
        for j in range((eqn_no+1)):
            if(eqn[i][j]!=0):
                eqn[i][j]=eqn[i][j]-(eqn[k][j]*(equation[i][k]/eqn[k][k]))



    print "Matrix After %d step: " %(k+1)
    for i in range(eqn_no):
            for j in range((eqn_no+1)):
                print "\t%f"%(eqn[i][j]),
                equation[i][j]=eqn[i][j];

            print
    print

输入:

25x+5y+z=106.8
64x+8y+z=177.2
144x+12y+z=279.2

output is:

Main Matrix: 
    25.000000   5.000000    1.000000    106.800000
    64.000000   8.000000    1.000000    177.200000
    144.000000  12.000000   1.000000    279.200000

Matrix After 1 step: 
    25.000000   5.000000    1.000000    106.800000
    0.000000    8.000000    1.000000    177.200000
    0.000000    12.000000   1.000000    279.200000

Matrix After 2 step: 
    25.000000   5.000000    1.000000    106.800000
    0.000000    8.000000    1.000000    177.200000
    0.000000    0.000000    1.000000    279.200000

But it should be like

Main Matrix: 
    25.000000   5.000000    1.000000    106.800000
    64.000000   8.000000    1.000000    177.200000
    144.000000  12.000000   1.000000    279.200000

Matrix After 1 step: 
    25.000000   5.000000    1.000000    106.800000
    0.000000    -4.80000    -1.56000    -96.208000
    0.000000    -16.8000    -4.76000    -335.968000

Matrix After 2 step: 
    25.000000   5.000000    1.000000    106.800000
    0.000000    -4.80000    -1.56000    -96.208000
    0.000000    0.000000    0.699999    0.759981

有没有人知道为什么地球上发生了这种情况?为什么零件正在改变而其他零件没有改变?我用c ++完成了这个代码,它完美地运行在那里,但在这里我遇到了很多问题。也许我是python的新手。

1 个答案:

答案 0 :(得分:1)

以下内容对您有用:

def get_number(element):
    re_number = re.match("(-?\d+\.?\d*)", element)

    if re_number:
        return float(re_number.group(1))
    else:
        return 1.0

def getvalues(equation):
    elements = re.findall("(-?[a-z0-9.]+)", equation)
    return [get_number(element) for element in elements]


eqn = []
eqn_no = int(raw_input("Enter the number of equations: "))

for i in range(eqn_no):
    eqn.append(getvalues(str(raw_input("Enter Equation %d: " % (i+1)))))

print

for line in eqn:
    print "|" + "  ".join(["%3.1f" % value for value in line]) + "|"

例如:

Enter the number of equations: 2
Enter Equation 1: x+2y=3
Enter Equation 2: 4x+5y=6

|1.0  2.0  3.0|
|4.0  5.0  6.0|

这也适用于4.3x+6.7y=8

之类的内容