从命令行给出的输入文件中读取

时间:2017-10-12 22:13:51

标签: python-3.x

您好我对python相对较新但是我的代码试图完成以检查图表是否为二分图我的代码是我的代码以及我当前的问题是我有错误是我如何阅读文本文件由用户输入给出,然后通过功能,我知道我正在访问该文件,但后来我有一个错误,它没有正确读取行。已修复,但现在它没有通过该功能发送。给我错误

Traceback (most recent call last):

  File "color.py", line 4, in <module>

    class color():

  File "color.py", line 83, in color

    f = iscolor(f)

TypeError: iscolor() missing 1 required positional argument: 'src'

下面是我的代码

如果你有任何问题可以自由陈述他们我已经尝试了其他方法,如果希望看到我做了什么,他们被评论出来与实际评论虽然它有点混乱,因为有些线路我拆开或重用以修复以前的错误。

import sys 
import re

class color():



    def _init_(self , vertex):
        self.vertex = vvertex
        self.graph =  [0 for column in range(vertex)]
        self.graph =  [0 for row in range(vertex)]

    #if true if the Graph is bipartite
    # otherwise colorable

    def iscolor(self, src):


        #set color array to set -1as no color 
        clear[-1] * self.vertex


        #start coloring the first point
        clear[src] = 1

        #make a line of in and out
        queue = []

        queue.append[src]

        # run
        while queue:

            u = queue.pop()


        #return false if there is a self-loop

            if self.graph[u][u] == 1:
                return False;

            for vertex in range(self.vertex):

                # found an edge thats not colored
                if self.graph[u][vertex] == 1 and clear[vertex] == -1:

                    #put it in
                    clear[vertex] = 1 - clear[u]

                    queue.append(vertex)

                #found an edge its colored with the same color as befor
                elif self.graph[u][vertex] == 1 and clear[vertex] == clear[u]:

                    return False

        return True

        #open the file from input

        #with open(sys.argv[0]) as f:

    f = open(sys.argv[1])

    string = f.read()

    string = string.split(" ")

    #Grab the string individually then convert
    #int_list = [int(i) for i in f]

     #string = list(map(int, string))

    #f = iscolor(int_list, f)



#read the integers from the file

    for line in f:

        vertexstart = [int(x) for x in line.split()]    
    f = iscolor(f)



   #print("is two colorable") if f.iscolor(0) else print("is not two colorable")

  #print`enter code here`
  #print( " connected component(s)")

0 个答案:

没有答案