我有一个锥体间连接的嵌套列表。你知道吗
a = [0,1]
b = [2,4]
c = [2,0]
d = [4,3]
e=[a,b,c,d]
我想写一个程序,列出锥0的每个邻居,锥1的每个邻居。。。 我希望输出为[[1,2],[0],[0,4],[4],[2,3]]
我做了以下工作:
neighbour_list =[]
cone = 0
for element in e:
while cone in element:
if cone == element[0]:
neighbour = element[1]
neighbour_list.append(neighbour)
else:
neighbour = element[0]
neighbour_list.append(neighbour)
cone = cone + 1
print(neighbour_list)
我的想法是识别包含cone 0、cone 1、cone 2等的列表,然后为每个列表提取邻居(即元素[1]或元素[0]),并将其附加到邻居列表中。你知道吗
我得到错误消息:“while cone in element”-类型为(int)not iterable的参数
怎么了?你知道吗
我建议改用字典。你知道吗
运行它可以:
使用描述性名称将有助于提高代码的可读性。你知道吗
如果我们事先不知道锥体是什么,我们就需要找到连接处的所有锥体
如果你只想考虑有连接点的锥体
如果要考虑交叉点中表示的范围内的所有圆锥体,请使用
如果cone循环是外循环,则构建列表会更容易一些,因为我们正试图找到每个cone的邻居:
似乎邻居的名单遗漏了一些信息-你不能轻易地分辨出邻居是为哪个圆锥。您可以使用
zip
添加信息:或者在程序中稍加修改就可以添加信息列表
相关问题 更多 >
编程相关推荐