python - 将集合从 {a-b,b-c,c-a} 更改为 {(a,b),(b,c),(c,a)}?

标签 python

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/

相关文章:

python - PostgreSQL 日期时间类型的最佳数据类型

Python 结构体长度不正确

python - 为什么 multiprocessing.sharedctypes 赋值这么慢?

Python:连接 URL 和整数

python - 从列表理解和一般情况下有效地创建 numpy 数组

python - 凯拉斯 + tensorflow : Prediction on multiple gpus

python - 如何找到图像中物体的中心和角度?

python - 如何在 django 中使用 celery 运行任务并将结果保存在 django 数据库中?

python - 从Python使用REST API : do I need an async library?

python - 更改 Python easy_install 的默认包含路径