[英]How do I get the elements of a list within a given percentile range?
I am trying to iterate over a list of elements that satisfy my percentile range without using numPy.我试图在不使用 numPy 的情况下迭代满足我的百分位数范围的元素列表。 I am approaching the problem with a simple for-loop.我正在用一个简单的 for 循环来解决这个问题。 The answer I am receiving is of all the elements that I want but it is being repeated.我收到的答案是我想要的所有元素,但它正在重复。 Where am I lacking?我缺什么地方?
yos = [('student3', 12),
('student4', 14),
('student9', 35),
('student6', 43),
('student1', 45),
('student7', 47),
('student5', 48),
('student2', 78),
('student10', 80),
('student8', 98)]
for si in range(25, 76):
y = len(yos)*(si/100)
y1 = int(y+.5)
if yos[y1-1] in yos:
print(yos[y1-1])
Wanted output:想要 output:
('student9', 35)
('student6', 43)
('student1', 45)
('student7', 47)
('student5', 48)
Received output:收到 output:
('student9', 35)
('student9', 35)
('student9', 35)
('student9', 35)
('student9', 35)
('student9', 35)
('student9', 35)
('student9', 35)
('student9', 35)
('student9', 35)
('student6', 43)
('student6', 43)
('student6', 43)
('student6', 43)
('student6', 43)
('student6', 43)
('student6', 43)
('student6', 43)
('student6', 43)
('student6', 43)
('student1', 45)
('student1', 45)
('student1', 45)
('student1', 45)
('student1', 45)
('student1', 45)
('student1', 45)
('student1', 45)
('student1', 45)
('student1', 45)
('student7', 47)
('student7', 47)
('student7', 47)
('student7', 47)
('student7', 47)
('student7', 47)
('student7', 47)
('student7', 47)
('student7', 47)
('student7', 47)
('student5', 48)
('student5', 48)
('student5', 48)
('student5', 48)
('student5', 48)
('student5', 48)
('student5', 48)
('student5', 48)
('student5', 48)
('student5', 48)
('student2', 78)
Your calculation results in duplicate values.您的计算导致重复值。
y = len(yos)*(si/100)
y1 = int(y+.5)
y1 will be the same value for multiple iterations. y1 将是多次迭代的相同值。 y=2.6, y=2.7, y=2.8, y=2.9 all convert to 3 when you add.5 and convert it to an int. y=2.6, y=2.7, y=2.8, y=2.9 在 add.5 时都转换为 3 并将其转换为 int。
One option would be to create a set of indices, then just print out the values at those indices.一种选择是创建一组索引,然后打印出这些索引处的值。
Something like:就像是:
import math
yos = [('student3', 12),
('student4', 14),
('student9', 35),
('student6', 43),
('student1', 45),
('student7', 47),
('student5', 48),
('student2', 78),
('student10', 80),
('student8', 98)]
set_of_indices = set([int(math.floor(len(yos) * (si / 100))) for si in range(25, 76)])
for y in set_of_indices:
print(yos[y])
z = [('student3', 12),
('student4', 14),
('student9', 35),
('student6', 43),
('student1', 45),
('student7', 47),
('student5', 48),
('student2', 78),
('student10', 80),
('student8', 98)]
for si in range(25, 75,10):
y = (len(z)*si)/100
y1 = int(y+.5)
if z[y1-1] in z: print(z[y1-1])
Try the code here试试这里的代码
It's in fact the int() function that created the problem.实际上是 int() function 造成了问题。 so the solution was to add 10
as a third parameter in range: range(25, 75,10)
To understand more, please see the .range()
function documentation所以解决方案是在范围内添加10
作为第三个参数: range(25, 75,10)
要了解更多信息,请参阅.range()
function 文档
You've gotta also make the range be between 25
and 75
, not 76
... So you don't have a problem with.int()您还必须使范围介于25
和75
之间,而不是76
...所以您对 .int() 没有问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.