v = input("enter the vertices: ")
v = [x.strip(' ') for x in v.split(',')]
e = input("enter the edges: ")
e = [x.strip(' ') for x in e.split(',')]
edges = set(e)
print(edges)
如标题所示,我想将输出 {'a-b', 'c-a', 'b-c'}
更改为 {( 'a','b'),('b','c'),('c','a')}
因此更容易将数据引用为边缘。我该怎么做?
最佳答案
您可以使用集合理解简单地解析边缘并按'-'
进行分割:
e = input("enter the edges: ")
e = {tuple(x.strip().split('-')) for x in e.split(',')}
关于python - 将集合从 {a-b,b-c,c-a} 更改为 {(a,b),(b,c),(c,a)}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42194392/