我的Python代码出了什么问题?

我的Python代码出了什么问题?,python,string,Python,String,我对编程和Python特别陌生。在这里,我尝试编写一段代码,从用户那里接收矩阵(作为列表列表编写),并将此字符串转换为“真实”列表: def字符串到矩阵(arg): 结果=[] 行=参数计数(“[”)-1 对于范围内的i(行): 结果。追加([])) 计数=0 i=2 虽然我你的问题不清楚,所以帮不上什么忙,但请仔细阅读下面的代码,看看它是否打印出正确的输出 def string_to_matrix(arg): result = [] lines = arg.count("[")

我对编程和Python特别陌生。在这里,我尝试编写一段代码,从用户那里接收矩阵(作为列表列表编写),并将此字符串转换为“真实”列表:

def字符串到矩阵(arg):
结果=[]
行=参数计数(“[”)-1
对于范围内的i(行):
结果。追加([]))
计数=0
i=2

虽然我你的问题不清楚,所以帮不上什么忙,但请仔细阅读下面的代码,看看它是否打印出正确的输出

def string_to_matrix(arg):
    result = []
    lines = arg.count("[") - 1
    for i in range(lines):
        result.append([])
    count = 0
    i = 2
    while i < len(arg):
        if arg[i] == "[":
            count += 1
            i=i+1
        elif arg[i].isdigit():
            new_number = 0
            while arg[i].isdigit():
                new_number = 10*new_number + int(arg[i])
                i=i+1
            result[count].append(new_number)
        else:
            i=i+1
    return result 
def字符串到矩阵(arg):
结果=[]
行=参数计数(“[”)-1
对于范围内的i(行):
结果。追加([]))
计数=0
i=2
而i
您的问题不清楚,所以请仔细阅读下面的代码,看看它是否打印出正确的输出

def string_to_matrix(arg):
    result = []
    lines = arg.count("[") - 1
    for i in range(lines):
        result.append([])
    count = 0
    i = 2
    while i < len(arg):
        if arg[i] == "[":
            count += 1
            i=i+1
        elif arg[i].isdigit():
            new_number = 0
            while arg[i].isdigit():
                new_number = 10*new_number + int(arg[i])
                i=i+1
            result[count].append(new_number)
        else:
            i=i+1
    return result 
def字符串到矩阵(arg):
结果=[]
行=参数计数(“[”)-1
对于范围内的i(行):
结果。追加([]))
计数=0
i=2
而i
问题已暂停,但我将尝试回答,而您应该改进它。阅读。提出您的问题“哪里出了问题”不是询问任何问题的正确方式

def string_to_matrix(arg):
    result = []
    lines = arg.count("[") - 1
    for i in range(lines):
        result.append([])
    count = 0
    i = 2
    while i < len(arg):
        if arg[i] == "[":
            count += 1
            i += 1
        elif arg[i].isdigit():
            new_number = 0
            while arg[i].isdigit():
                new_number = 10*new_number + int(arg[i])
                i += 1
            result[count].append(new_number)
        else:
            i += 1
    return result


m_1 = string_to_matrix(input("Type your first matrix in format [[x, y, ...],[z, ...],[...], ...]: "))
print(m_1)
  • 考虑对字符串使用
    split
    方法。请看以下内容:

    arg='[[1,2,3],[4,5,6],[7,8,9]'

    打印(参数[2:-2]。拆分('],['))

    [1,2,3,4,5,6,7,8,9']

  • 看到了吗?在一行中,你得到了一个字符串列表,你可以很容易地将其解析为矩阵中的不同行。现在看看发生了什么:

    >>> l = '1, 2, 3'.split(',')
    >>> l
    ['1', ' 2', ' 3']
    >>> l1 = [int(value) for value in l]
    >>> l1
    [1, 2, 3]
    

    当然,您可以添加一些更有意义的名称,检查用户数据的正确性等等,但我认为您可以利用我在这里编写的示例。

    问题已被搁置,但我将尝试回答它,同时您应该改进它。阅读。问你的问题“出了什么问题”并不是一种适当的提问方式

    def string_to_matrix(arg):
        result = []
        lines = arg.count("[") - 1
        for i in range(lines):
            result.append([])
        count = 0
        i = 2
        while i < len(arg):
            if arg[i] == "[":
                count += 1
                i += 1
            elif arg[i].isdigit():
                new_number = 0
                while arg[i].isdigit():
                    new_number = 10*new_number + int(arg[i])
                    i += 1
                result[count].append(new_number)
            else:
                i += 1
        return result
    
    
    m_1 = string_to_matrix(input("Type your first matrix in format [[x, y, ...],[z, ...],[...], ...]: "))
    print(m_1)
    
  • 考虑对字符串使用
    split
    方法。请看以下内容:

    arg='[[1,2,3],[4,5,6],[7,8,9]'

    打印(参数[2:-2]。拆分('],['))

    [1,2,3,4,5,6,7,8,9']

  • 看到了吗?在一行中,你得到了一个字符串列表,你可以很容易地将其解析为矩阵中的不同行。现在看看发生了什么:

    >>> l = '1, 2, 3'.split(',')
    >>> l
    ['1', ' 2', ' 3']
    >>> l1 = [int(value) for value in l]
    >>> l1
    [1, 2, 3]
    

    当然,您可以添加一些更有意义的名称,检查用户数据的正确性等等,但我认为您可以利用我在这里编写的示例。

    如果您愿意,这里是另一种简单的方法:

    def string_to_matrix(arg):
        result = []
        i=1
        while i<len(arg)-1:
            if(arg[i]=='['):
                i+=1
                temp = ""
                while(arg[i]!=']'):
                    temp+=arg[i]
                    i+=1
                temp = temp.split(',')
                temp2 = []
                for j in temp:
                    temp2.append(int(j.strip(' ')))
                result.append(temp2)
            else:
                i+=1
        return result
    
    
    m_1 = string_to_matrix(raw_input("Type your first matrix in format [[x, y, ...],[z, ...],[...], ...]: "))
    print m_1
    
    def字符串到矩阵(arg):
    结果=[]
    i=1
    
    而我如果你愿意,这里有另一个简单的方法:

    def string_to_matrix(arg):
        result = []
        i=1
        while i<len(arg)-1:
            if(arg[i]=='['):
                i+=1
                temp = ""
                while(arg[i]!=']'):
                    temp+=arg[i]
                    i+=1
                temp = temp.split(',')
                temp2 = []
                for j in temp:
                    temp2.append(int(j.strip(' ')))
                result.append(temp2)
            else:
                i+=1
        return result
    
    
    m_1 = string_to_matrix(raw_input("Type your first matrix in format [[x, y, ...],[z, ...],[...], ...]: "))
    print m_1
    
    def字符串到矩阵(arg):
    结果=[]
    i=1
    
    而iYou不处理逗号、空格或任何其他将出现在矩阵中的字符。
    i
    在您的程序遇到这些字符时不会递增。这就是它被卡住的原因。谢谢,现在很清楚:-)您不处理逗号、空格或将出现在矩阵中的任何其他字符。
    i
    在y时不会递增我们的程序遇到了这些字符。这就是它被卡住的原因。谢谢,现在很清楚:-)啊,问题是当arg[i]不是“[”且不是数字时计数器“i”没有增加?谢谢:-)啊,所以问题是当arg[i]不是“[”且不是数字时计数器“i”没有增加?谢谢:-)