我试图在名为sizeList的列表中循环并检查条件。代码没有返回正确的值。谁知道问题可能是什么?
overAvg = 0
overTre = 0
avgSize = np.mean(sizeList)
for a in sizeList:
if a >= avgSize:
overAvg += 1
treshold = raw_input("Enter Size Treshold: ")
for t in sizeList:
if t >= treshold:
overTre += 1
print overAvg
print overTre
for key, value in pktDict.iteritems():
sizeValue = [value]
sizeList.append(sizeValue)
工作代码:
for key, value in pktDict.iteritems():
sizeValue = value
sizeList.append(sizeValue)
avgSize = np.mean(sizeList)
for a in sizeList:
if a >= avgSize:
overAvg += 1
treshold = int(raw_input("Enter Size Treshold: "))
for t in sizeList:
if t >= treshold:
overTre += 1
print overAvg
print overTre
答案 0 :(得分:1)
您需要将输入大小转换为...
$("#loading-indicator").show();
$.post(
"/echo/json/",
{ delay: 2 },
function(data) {
...
$("#loading-indicator").hide();
}
);
...
,否则它会比较词汇值而不是数字。此外,从评论中我看到您的列表在每个字段中都有一个大小为1的列表,您需要将其展平(或者首先将列表初始化为1D):
int
结果:
import numpy as np
overAvg = 0
overTre = 0
sizeList = [[4],[7],[43],[5],[54],[7],[4],[5]] # only formatted like this because OP's list is
sizeList = [item[0] for item in sizeList]
avgSize = np.mean(sizeList)
for a in sizeList:
if a >= avgSize:
overAvg += 1
treshold = int(raw_input("Enter Size Treshold: "))
for t in sizeList:
if t >= treshold:
overTre += 1
print overAvg
print overTre
在您的代码中,您创建的列表每次迭代只包含一个项目,而您应该这样做:
Enter Size Treshold: 5
2
6