Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取给定百分比范围内的列表元素?_Python_Loops_For Loop_Math - Fatal编程技术网

Python 如何获取给定百分比范围内的列表元素?

Python 如何获取给定百分比范围内的列表元素?,python,loops,for-loop,math,Python,Loops,For Loop,Math,我试图迭代满足我的需求的元素列表 不使用numPy的百分位数范围。我用一个简单的for循环来解决这个问题。我得到的答案是我想要的所有元素,但它正在被重复。我在哪里 缺乏?谢谢你的意见 yos = [('student3', 12), ('student4', 14), ('student9', 35), ('student6', 43), ('student1', 45), ('student7', 47)

我试图迭代满足我的需求的元素列表 不使用numPy的百分位数范围。我用一个简单的for循环来解决这个问题。我得到的答案是我想要的所有元素,但它正在被重复。我在哪里 缺乏?谢谢你的意见

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])
        
 
想要的输出:

    ('student9', 35)
    ('student6', 43)
    ('student1', 45)
    ('student7', 47)
    ('student5', 48)
('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)
收到的输出:

    ('student9', 35)
    ('student6', 43)
    ('student1', 45)
    ('student7', 47)
    ('student5', 48)
('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)

您的计算会产生重复的值

y=len(yos)*(si/100)
y1=整数(y+0.5)
对于多次迭代,y1将是相同的值。y=2.6,y=2.7,y=2.8,y=2.9当你加上.5并将其转换为整数时,所有这些都转换为3

一种选择是创建一组索引,然后打印出这些索引的值

比如:

导入数学
yos=[('student3',12),
('student4',14),
('student9',35),
('student6',43),
('student1',45),
('student7',47),
('student5',48),
('student2',78),
('student10',80),
('student8',98)]
指数集合=范围(25,76)内的指数集合([int(数学下限(len(yos)*(si/100)))
对于指数集合中的y:
打印(y[y])
请在此处尝试代码

实际上是int()函数造成了这个问题。因此,解决方案是添加
10
作为范围中的第三个参数:
range(25,75,10)
要了解更多信息,请参阅
.range()
函数文档


您还必须使范围介于
25
75
之间,而不是
76
。。。因此,您对.int()没有问题。

|请仔细检查您的代码,并将问题缩小为一个问题。然后问一个具体的问题。“为什么我的代码会这样做”是请大家也采取,阅读和。欢迎来到堆栈溢出!你能想出一个数学规则,告诉你列表中应该使用的最小和最大的索引吗?如果你有这些,你能想出一种方法来重复列表的这一部分吗?谢谢你,普拉纳夫。我会记下来的。我对这个网站不熟悉,但我会记得浏览过你的链接谢谢你的意见,Abdel。这符合我的准则欢迎!所以请给我的答案打分:)。。。。还有一点:我想你还得核实一下边界。。例如,您必须检查函数输出中是否包含25%和75%的学生;要做到这一点,你只需玩弄这些价值观,并检查一下。。总而言之,您必须检查您的代码是否考虑了所有用例。工作非常完美。非常感谢。