如何拆分数字列表并插入另一个列表

How to get a split up a list of numbers and insert into another list

目前我有一个包含 6 行数字的文件,每行包含 9 个数字。重点是测试文件中的每一行数字是否完成了一个幻方。因此,例如,假设文件中的一行数字是 4 3 8 9 5 1 2 7 6。前三个数字需要是矩阵中的第一行。接下来的三个数字需要在第二行,第三行也一样。 因此,您需要得到一个矩阵: [['4','3','8'],['9','5','1'],['2','7','6']]

我需要测试矩阵以查看它是否是有效的幻方(行加起来为 15,列加为 15,对角线加为 15)。

我的代码目前是:

def readfile(fname):
    """Return a list of lines from the file"""
    f = open(fname, 'r')
    lines = f.read()
    lines = lines.split()
    f.close()
    return lines

def assignValues(lines):
    magicSquare = []
    rows = 3
    columns = 3
    for row in range(rows):
        magicSquare.append([0] * columns)
    for row in range(len(magicSquare)):
        for column in range(len(magicSquare[row])):
            magicSquare[row][column] = lines[column]
    return magicSquare

def main():
    lines = readfile(input_fname)
    matrix = assignValues(lines)
    print(matrix)

每当我 运行 我的代码来测试它时,我得到:

[['4', '3', '8'], ['4', '3', '8'], ['4', '3', '8']]

如您所见,我只将前 3 个数字输入矩阵。
最后,我的问题是我将如何继续使用数字行中的以下 6 个数字来继续我的矩阵?我不确定这是否是我可以在我的循环中做的事情,或者我是否错误地分割了我的台词,或者我是否完全走错了路?

谢谢。

它总是只获取前 3 列,因为

magicSquare[row][column] = lines[column]

因此

def assignValues(lines):
    magicSquare = []
    rows = 3
    columns = 3
    for row in range(rows):
        magicSquare.append([0] * columns)
    for line in range((sizeof(lines)/9)) #since the input is already split this means that the size of 'lines' divided by 9 is equal to the number of rows of numbers
        for row in range(len(magicSquare)):
            for column in range(len(magicSquare[row])):
                magicSquare[row][column] = lines[(9*line)+(3*row)+column]
    return magicSquare

注意 (3*row)+column 每次迭代都会向右移动 3 列 并且 (9*line)+(3*row)+column 每次迭代都会向右移动 9 列(整行)

一旦你得到这个,你就可以开始寻找幻方了

def testMagicSquare(matrix):
    rows = 3
    columns = 3
    for a in len(matrix)
        test1 = 0
        test2 = 0
        test3 = 0
        for b in range(3)
            if(sum(matrix[a][b])==15) test1=1 #flag true if whole row is 15 but turns false if a row is not 15
            else test1=0

            if((matrix[a][0][b]+matrix[a][1][b]+matrix[a][2][b])==15) test2=1 #flag true if column is 15 but turns false if a column is not 15
            else test2=0

            if(((matrix[a][0][0]+matrix[a][1][1]+matrix[a][2][2])==15) and 
            ((matrix[a][0][2]+matrix[a][1][1]+matrix[a][2][0])==15)) test3=1 #flag true if diagonal  is 15 but turns false if diagonal is not 15
            else test3=0

        if(test1>0 and test2>0 and test3>0) println('line ' + a + ' is a magic square')
        else println('line ' + a + ' is not a magic square')

要测试输入文件中的每一行是否包含幻方数据,您需要稍微重新组织代码。我使用了与 Francis 不同的技术来填充矩阵。理解 zip(*[iter(seq)] * size) 的工作原理可能有点困难,但这是一个 非常 有用的模式。如果您需要解释,请告诉我。

我的代码对矩阵使用元组列表,而不是列表列表,但无论如何元组在这里更合适,因为不需要修改矩阵中的数据。此外,我将输入数据从 str 转换为 int,因为您需要对数字进行算术运算以测试 matrix 是否为幻方。

#! /usr/bin/env python

def make_square(seq, size):
    return zip(*[iter(seq)] * size)


def main():
    fname = 'mydata'
    size = 3
    with open(fname, 'r') as f:
        for line in f:
            nums = [int(s) for s in line.split()]
            matrix = make_square(nums, size)
            print matrix
            #Now call the function to test if the data in matrix 
            #really is a magic square.
            #test_square(matrix)


if __name__ == '__main__':
    main()

这是 make_square() 的修改版本,returns 列表列表而不是元组列表,但请记住,元组列表实际上比列表列表更好如果你不需要列表给你的可变性。

def make_square(seq, size):
    square = zip(*[iter(seq)] * size)
    return [list(t) for t in square]

我想我应该提一下,实际上只有一个可能的 3 x 3 幻方使用从 1 到 9 的所有数字,不包括旋转和反射。但我想对这一事实进行蛮力演示并没有什么害处。 :)

此外,我有 Python 多年前(当我第一次学习 Python 时)编写的代码,它为奇数 n >= 5 生成大小为 n x n 的幻方。让我知道如果你想看吗


zip 和迭代器对象

下面是一些代码,简要说明了 zip() 和 iter() 函数的作用。

''' Fun with zip '''

numbers = [1, 2, 3, 4, 5, 6]
letters = ['a', 'b', 'c', 'd', 'e', 'f']

#Using zip to create a list of tuples containing pairs of elements of numbers & letters 
print zip(numbers, letters)

#zip works on other iterable objects, including strings
print zip(range(1, 7), 'abcdef')

#zip can handle more than 2 iterables
print zip('abc', 'def', 'ghi', 'jkl')

#zip can be used in a for loop to process two (or more) iterables simultaneously
for n, l in zip(numbers, letters):
    print n, l

#Using zip in a list comprehension to make a list of lists
print [[l, n] for n, l in zip(numbers, letters)]

#zip stops if one of the iterables runs out of elements
print [[n, l] for n, l in zip((1, 2), letters)]
print [(n, l) for n, l in zip((3, 4), letters)]

#Turning an iterable into an iterator object using the iter function
iletters = iter(letters)

#When we take some elements from an iterator object it remembers where it's up to
#so when we take more elements from it, it continues from where it left off.
print [[n, l] for n, l in zip((1, 2, 3), iletters)]
print [(n, l) for n, l in zip((4, 5), iletters)]

#This list will just contain a single tuple because there's only 1 element left in iletters
print [(n, l) for n, l in zip((6, 7), iletters)]

#Rebuild the iletters iterator object 
iletters = iter('abcdefghijkl')

#See what happens when we zip multiple copies of the same iterator object. 
print zip(iletters, iletters, iletters)

#It can be convenient to put multiple copies of an iterator object into a list
iletters = iter('abcdefghijkl')
gang = [iletters] * 3

#The gang consists of 3 references to the same iterator object 
print gang

#We can pass each iterator in the gang to zip as a separate argument 
#by using the "splat" syntax
print zip(*gang)

#A more compact way of doing the same thing: 
print zip(* [iter('abcdefghijkl')]*3)

下面是交互式解释器中的相同代码 运行,因此您可以轻松查看每个语句的输出。

>>> numbers = [1, 2, 3, 4, 5, 6]
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f']
>>> 
>>> #Using zip to create a list of tuples containing pairs of elements of numbers & letters 
... print zip(numbers, letters)
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')]
>>> 
>>> #zip works on other iterable objects, including strings
... print zip(range(1, 7), 'abcdef')
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')]
>>> 
>>> #zip can handle more than 2 iterables
... print zip('abc', 'def', 'ghi', 'jkl')
[('a', 'd', 'g', 'j'), ('b', 'e', 'h', 'k'), ('c', 'f', 'i', 'l')]
>>> 
>>> #zip can be used in a for loop to process two (or more) iterables simultaneously
... for n, l in zip(numbers, letters):
...     print n, l
... 
1 a
2 b
3 c
4 d
5 e
6 f
>>> #Using zip in a list comprehension to make a list of lists
... print [[l, n] for n, l in zip(numbers, letters)]
[['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5], ['f', 6]]
>>> 
>>> #zip stops if one of the iterables runs out of elements
... print [[n, l] for n, l in zip((1, 2), letters)]
[[1, 'a'], [2, 'b']]
>>> print [(n, l) for n, l in zip((3, 4), letters)]
[(3, 'a'), (4, 'b')]
>>> 
>>> #Turning an iterable into an iterator object using using the iter function
... iletters = iter(letters)
>>> 
>>> #When we take some elements from an iterator object it remembers where it's up to
... #so when we take more elements from it, it continues from where it left off.
... print [[n, l] for n, l in zip((1, 2, 3), iletters)]
[[1, 'a'], [2, 'b'], [3, 'c']]
>>> print [(n, l) for n, l in zip((4, 5), iletters)]
[(4, 'd'), (5, 'e')]
>>> 
>>> #This list will just contain a single tuple because there's only 1 element left in iletters
... print [(n, l) for n, l in zip((6, 7), iletters)]
[(6, 'f')]
>>> 
>>> #Rebuild the iletters iterator object 
... iletters = iter('abcdefghijkl')
>>> 
>>> #See what happens when we zip multiple copies of the same iterator object. 
... print zip(iletters, iletters, iletters)
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'), ('j', 'k', 'l')]
>>> 
>>> #It can be convenient to put multiple copies of an iterator object into a list
... iletters = iter('abcdefghijkl')
>>> gang = [iletters] * 3
>>> 
>>> #The gang consists of 3 references to the same iterator object 
... print gang
[<iterator object at 0xb737eb8c>, <iterator object at 0xb737eb8c>, <iterator object at 0xb737eb8c>]
>>> 
>>> #We can pass each iterator in the gang to zip as a separate argument 
... #by using the "splat" syntax
... print zip(*gang)
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'), ('j', 'k', 'l')]
>>> 
>>> #A more compact way of doing the same thing: 
... print zip(* [iter('abcdefghijkl')]*3)
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'), ('j', 'k', 'l')]
>>>