我有一些传输功率的节点,我将它们的位置放在一个列表中,然后我用它来计算从每个节点到另一个节点的功率接收器,以便我做的是:
Node_positions = [[0,100],[100,0],[0,0],[100,100]]
distances_Ascending = [[]] * len(list(itertools.combinations(Node_positions , 2)))
Counter = 0
for p0, p1 in itertools.combinations(Node_positions , 2):
distances_Ascending[Counter] = (math.sqrt((p0[0] - p1[0]) ** 2 + (p0[1] - p1[1]) ** 2))
Counter += 1
Powers_Ascending = [[]] * len(list(itertools.combinations(Node_positions , 2)))
counter = 0
row = 0
next_no = sheet.nrows - 1
for elements in range(len(list(itertools.combinations(Node_positions , 2)))):
Powers_Ascending[elements] = 10 * (math.log10((sheet.cell_value(row, 0) * 1e-3))) - 20 * (
math.log10((4 * math.pi * distances_Ascending[elements] * 2.4e9) / 3e8))
counter += 1
if counter == next_no:
row += 1
counter = 0
next_no -= 1
Pycharm for python强调了这一部分:
(4 * math.pi * distances_Ascending[elements] * 2.4e9)
并说:
Expected type 'Number' got 'list[list]' instead
这是什么意思?Isn&t; t distance =已经是一个代表节点之间距离的数字,而且日志里面的所有内容都已经是一个数字?
问题出在哪里?
答案 0 :(得分:1)
在我看来,distance_Ascending是一个列表列表
>>> foo = [[]] * 3
[[], [], []]
所以它抱怨,因为你正在混淆一个列表和一个浮动。
从您的代码看来,您只是在循环结束后递增计数器Count
,因此您只分配给列表的第一个元素。
所有其他元素仍然是变量初始化的列表,并且您在访问时遇到错误。
您需要做的最小更改是在循环中移动 Counter 变量:
Node_positions = [[0,100],[100,0],[0,0],[100,100]]
distances_Ascending = [[]] * len(list(itertools.combinations(Node_positions , 2)))
# if you want to intialise the list it is better to do
# [0] * len(...your_calculations...)
# otherwise use the append() method of the list in the loop
Counter = 0
for p0, p1 in itertools.combinations(Node_positions , 2):
distances_Ascending[Counter] = math.sqrt((p0[0] - p1[0]) ** 2 + (p0[1] - p1[1]) ** 2)
Counter += 1 # <--- this was not properly indented, hence the error
Powers_Ascending = [[]] * len(list(itertools.combinations(Node_positions , 2)))
# same init comment as for distances_Ascending
counter = 0
row = 0
next_no = sheet.nrows - 1
for elements in range(len(list(itertools.combinations(Node_positions , 2)))):
Powers_Ascending[elements] = 10 * (math.log10((sheet.cell_value(row, 0) * 1e-3))) - 20 * (math.log10((4 * math.pi * distances_Ascending[elements] * 2.4e9) / 3e8))
# this part was also badly indented
counter += 1
if counter == next_no:
row += 1
counter = 0
next_no -= 1
您编码的几点建议: