如何得到一个分裂的数字列表和插入到另一个lis

2024-04-20 12:27:21 发布

您现在位置:Python中文网/ 问答频道 /正文

目前我有一个文件有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)

每当我运行代码进行测试时,我都会得到:

^{pr2}$

如你所见,我只把前3个数字输入矩阵。
最后,我的问题是,我如何继续我的矩阵与下面的6个数字行的数字?我不确定这是否是我能在我的循环中做的事情,或者我是否把我的线路划分错了,或者我完全走错了轨道?在

谢谢。在


Tags: 文件代码infordefrangecolumn矩阵
2条回答

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

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

#! /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()的一个修改版本,它返回一个列表列表而不是一个元组列表,但是请记住,如果您不需要列表提供的易变性,元组列表实际上比列表列表要好。在

^{pr2}$

我想我应该提一下,实际上只有一个可能的3x3幻方,它使用从1到9的所有数字,不包括旋转和反射。但我想用暴力来证明这一点没有坏处。:)

另外,我有几年前写的Python代码(当我第一次学习Python时),它生成大小为nxn的奇数n>;=5的幻方。如果你想看就告诉我。在


zip和iterator对象

下面的代码简要说明了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')]
>>> 

它只得到前3列,因为

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

因此

^{pr2}$

注意,(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')

相关问题 更多 >