python-3.x - Python - 更新集合

标签 python-3.x set

我正在尝试使用 Python 更新一组节点及其位置(坐标)。

n = 5  #number of current nodes
vertices = []
vertices=list(range(n)) # list of vertices=[0,1,2,3,4]

我正在为顶点中的每个元素创建一组随机点,如下所示:

pos = {i:(random.randint(0,50),random.randint(0,50)) for i in vertices}

此外,我还有另一个列表(称为“s”),其中包含一些坐标,我正在枚举“s”中的这些元素以添加索引(以继续索引为“vertices”)到“s”中的每个坐标。

for index, i in enumerate(s , vertices[-1]+1):
    print(index, i)

然后,我的输出为

5 (29.5, 34.0)
6 (20.0, 25.75)
7 (23.75, 36.0)

现在我需要用这些点及其相应的索引更新我的集合“pos”。我怎样才能做到这一点?

最佳答案

您可以在迭代自身时像这样分配,而不仅仅是打印

for index, i in enumerate(s, vertices[-1] + 1):
    pos[index] = i

最后,pos 将类似于

{0: (0, 10), 1: (16, 33), 2: (17, 36), 3: (40, 13), 4: (26, 39), 5: (29.5, 34.0), 6: (20.0, 25.75), 7: (23.75, 36.0)}

关于python-3.x - Python - 更新集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46188957/

相关文章:

c++ - 使用 std::set 查找结构数据

java - 比较作为映射的键和值的集合的大小

c++ - 集合和 vector 。 C++ 中的集合速度快吗?

python - 在 komodo 编辑中使用 time.sleep()?

python - 在 Python 中的 sock.sendall() 之后调用 sock.recv()

Python - 拆分一行并从单词中提取字符

python - 如何使用模型/ View 将 2 个表格小部件(每个表格小部件 1 列)组合成一个列表小部件

python - fatal error : Python. h : No such file or directory, python-Levenshtein 安装

c++ - 如何在 C++ 集合中插入值

algorithm - 查找所有最大子集的高效算法