【问题标题】:two functions reading a txt file, using elif使用 elif 读取 txt 文件的两个函数
【发布时间】:2014-09-28 13:11:56
【问题描述】:

所以我一直试图让这两个函数工作,当我单独执行它们时,它们工作,但是当我使用 elif 函数组合这两个函数时,它只运行第一个函数并打印出位置列表,以及错误提示“neighbour_list 未定义”

这是我的代码

 my_file=open("test_graph_1.txt","r")
 x=[]
 y=[]
 nodenumber=[]
 positionx=[]
 positiony=[]


for row in my_file:

    value=row[:-1]

    my_list=value.split(",")

    if len(my_list)==3:
        nodenumber.append(int(my_list[0]))

        positionx.append(int(my_list[1]))
        positiony.append(int(my_list[2]))

        nodenumber1 =[(nodenumber[a],positionx[a],positiony[a]) for a i range(len(nodenumber))]
        position_list=tuple(nodenumber1)




    elif len(my_list)==2:
        x.append(int(my_list[0]))
        y.append(int(my_list[1]))

        l1 = [(x[i] , y[i]) for i in range(len(x))]
        l2 = [(y[i] , x[i]) for i in range(len(x))]
        l1.extend(l2)
        neighbour_list=[[l[0] for l in l1 if l[1] == j] for j in range(len(x))]


 print("position_list",position_list)
 print("neigh",neighbour_list)

但是当我打印代码时,位置列表会很好,但 neighbour_list 会像这样出现:[[4, 1], [0, 4, 2], [1, 3], [2, 5, 4 ], [3, 0, 1], [3], []] 额外的空字符串,它不应该在那里,但在那之前一切都很好

【问题讨论】:

  • 那么你的函数在哪里?
  • 对不起,我还没有掌握该语言,我的意思是我的 2 个不同的 if 循环来获取 position_list 和 neighbour_list
  • else my_list[2] == "": 应该引发了 SyntaxError。你的意思是elif ...? (或者只是else:?)
  • 是的,我也用 elif 试过,但没用,所以我尝试了 else
  • 事情是它通过第一个 if 循环但不通过 elif 循环

标签: python python-3.x tuples nearest-neighbor neighbours


【解决方案1】:

如果对于每次循环,my_list[2] != "" 为 True,则永远不会定义 neighbour_list。那么

print("neigh",neighbour_list)

会提出NameError: the neighbour_list is not defined


相反,在输入for-loop 之前定义neighbour_list。你也可以使用

if len(my_list) == 3:
    ...
elif len(my_list) == 2:
    ...
else:
    ...

处理您希望收到的两种类型的行。


N = 5
position_list = list()
neighbour_list = [list() for j in range(N)]

with open("test_graph_1.txt","r") as my_file:
    for row in my_file:
        try:
            my_list = map(int, row.split(','))
        except ValueError:
            # Choose how to handle malformed lines
            print('invalid line: {!r}'format(row))
            continue
        if len(my_list) == 3:
            nodenumber, positionX, positionY = my_list
            position_list.append(tuple([nodenumber,positionX,positionY]))
        elif len(my_list) == 2:
            nodenumber1, nodenumber2 = my_list
            neighbour_list[nodenumber1].append(nodenumber2)
            neighbour_list[nodenumber2].append(nodenumber1)            
        else:
            # Choose how to handle lines with more than 3 or less than 2 items
            continue

print(position_list)
print("neigh", neighbour_list)

您可能还想使用像 networkxigraph 这样的图形库。

【讨论】:

  • 您必须为neighbour_list 分配一个值。你没有说你的程序应该做什么,所以我们真的无能为力。
  • 您可以在靠近顶部的for-loop 之前为neighbour_list 定义一个值。您可以将其设置为空列表或None,这取决于您希望程序在从未达到elif-suite 时执行的操作。
  • 在 txt 文件中给出了列表,关于带有节点的图。对于具有 N 个节点和 M 个链接的图,前 N 行的形式为 nodeNumber, positionX, positionY X 和 Y 位置是整数。在此之后,接下来的 M 行指定链接。它们的形式为 nodeNumber1,nodeNumber2
  • 前几行有 3 列表示节点编号、xposition、y 位置,如果使用 if my_list[2] != "":
  • 接下来的几行有2列表示节点的连接,哪个节点连接到哪个节点,用于找出相邻节点,所以我使用了elif my_list[2] == "": 我怎样才能让两个 if 套件都运行?或者我可以用别的东西吗
猜你喜欢
  • 2017-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-10
相关资源
最近更新 更多