从 python 中的文件读取时删除空格和逗号

remove whitespaces and commas when reading from a file in python

我必须从具有以下结构的文件构建图表:

1   80,982  163,8164    170,2620    145,648 200,8021    173,2069

第一位是顶点(1),80是权重为982的相邻顶点。我想去掉空格和那个逗号。

我尝试使用 strip()split(','),但我找不到合适的格式来构建我的图表。

您可以使用正则表达式轻松处理您的任务,如下所示:

>>> import re
>>> s = "1 80,982 163,8164 170,2620 145,648 200,8021 173,2069"
>>> re.findall(r'(\d+) (\d+),(\d+)', s) # pass your file content s as string
[('1', '80', '982'), ('8164', '170', '2620'), ('648', '200', '8021')]

说明:

findall - returns 所有匹配模式的列表

\d+ - 匹配一位或多位数字

如果我正确理解您的数据结构,即每行包含一个顶点及其成对的相邻顶点和权重,您只需使用 split 即可,如下所示:

lines = "1 80,982 163,8164 170,2620 145,648 200,8021 173,2069"
graph = {}
for line in lines.split('\n'):
    vertex, *neighbors = line.split()
    graph[vertex] = [tuple(neighbor.split(','))
                     for neighbor in neighbors]

结果是一个字典,其中包含作为键的顶点和作为值的相邻顶点列表-权重元组。