简体   繁体   English

Python代码打印不需要的行

[英]Python code printing an unwanted line

I can't figure out what is causing my code to print a "None" at the end of the execution. 我不知道是什么导致我的代码在执行结束时打印“无”。 This code is supposed to read a txt file with a matrix of zeroes and ones in it and print the coordenates of the orthogonal "islands" formed by the number one. 该代码应该读取其中包含零和一的矩阵的txt文件,并打印由数字1形成的正交“岛”的坐标。

from itertools import groupby
from operator import itemgetter

def matrixReader(inputFile): # Reads the file and creates the matrix map.
    w, h = map(int, inputFile.readline().strip().split())
    matrix = [list(map(int, line.strip())) for line in inputFile]
    return matrix, h, w

def matrixAdvancedSweeper(matrix, h, w): # Swipes the matrix map after NEW land chunks and makes a tuple for each piece of land.
    islandCount = 1
    landCoordinates = []
    for y in range(h):
        for x in range(w):
            if matrix[y][x] == 1:
                islandCount += 1
                islandSorter(matrix, y, x, islandCount)
            if matrix[y][x] > 1:
                landCoordinates.append((matrix[y][x]-1, y, x)) # Creates a list of tuples with the number of the island, height position and width position.
    return sorted(landCoordinates, key=itemgetter(0, 1)) # Sorts the list of tuples (first) by number of the island and (second) by height position.

def islandSorter(m, h, w, c): # Recursive function to enumerate and hide any land attached to a chunk already swiped.
    if m[h][w] == 1:
        m[h][w] = c
        if w < len(m[0]) - 1: islandSorter(m, h, w + 1, c)
        if h < len(m) - 1: islandSorter(m, h + 1, w, c)
        if w > 0: islandSorter(m, h, w - 1, c)
        if h > 0: islandSorter(m, h - 1, w, c)

def coordinatePrinter(sortedCoordinates): # Prints the groups of coordinates that forms each island.
    for key, group in groupby(sortedCoordinates, lambda x: x[0]): # Creates groups using the first item of the tuples as key and traverses those returning each key and group.
        print('____________\n  Island %s\nCoordinates:' % (key))
        for tile in group:
            print(' x %s   y %s' % (tile[1], tile[2]))

with open('TestFile.txt') as inputFile: # Opens the defined file to execute the nested code and than closes it.
    print(coordinatePrinter(matrixAdvancedSweeper(*matrixReader(inputFile))))

Here is an exemple of the file it should read (TestFile.txt) for testing purposes: 这是出于测试目的应阅读的文件示例(TestFile.txt):

4 4
1110
0000
0000
0110

The problem is the print statement you are using when you are calling the method: 问题是调用方法时使用的打印语句:

with open('TestFile.txt') as inputFile: # Opens the defined file to execute the nested code and than closes it.
    print(coordinatePrinter(matrixAdvancedSweeper(*matrixReader(inputFile))))

The return type for the print statement is None . 打印语句的返回类型为None So once your function's execution is completed your print statement exits with None 因此,一旦函数执行完毕,您的打印语句将以None退出

Simply solution is to remove print like: 简单的解决方案是删除print例如:

with open('TestFile.txt') as inputFile: # Opens the defined file to execute the nested code and than closes it.
    coordinatePrinter(matrixAdvancedSweeper(*matrixReader(inputFile)))

since you are already printing values in your coordinatePrinter function 因为您已经在coordinatePrinter函数中打印值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM