社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何在不删除python中元素索引的情况下删除元素

Ali Kahya • 4 年前 • 581 次点击  

我试图找到“3个最大的第一个数字”,它们的索引在列表中。

例如:

我有一个数字为0到10的列表。

numbers=[3,5,2,3,3,5,6,2,1,2,3,4,5,6,7]

有了这个号码:

2号出现:5次

3号出现4次

4号出现3次

我有一个计算数字百分比的清单

numbersInPercentage=[(100/len(numbers))*(numbers).count(0),
(100/len(numbers))*(numbers).count(1),
...
(100/len(numbers))*(numbers).count(10)]

我的算法(代码块)寻找3个最大的数字

           #first biggest
        largest_integer = max(numbersInPercentage) 
        index = numbersInPercentage.index(max(numbersInPercentage))
        print('Number is :', index," by ",largest_integer,"%")
        numbersInPercentage.remove(largest_integer) # this part killing me

           #second biggest
        second_largest_integer = max(numbersInPercentage)  
        index = numbersInPercentage.index(max(numbersInPercentage))
        print('Number is :', index," by ",second_largest_integer,"%")
        numbersInPercentage.remove(second_largest_integer) # this part killing me

           #thirdbiggest
        third_largest_integer = max(numbersInPercentage)  
        index = numbersInPercentage.index(max(numbersInPercentage))
        print('Number is :', index," by ",third_largest_integer,"%")
        numbersInPercentage.remove(third_largest_integer) # this part killing me

输出如下:

数字是:2乘以25.0%

数字是:2乘20.0%

数字是:2乘以15.0%

这部分代码删除了那个索引,所以我当前的索引一个一个地减少。 numbersInPercentage.remove(最大整数)

应该是这样的:

数字是:2乘以25.0%

数字是:3乘20.0%

数字是:4乘15.0%

我的代码发现最大的数字和他们的当前索引非常好,除了删除索引方法杀了我。

我需要分配其他删除了索引的内容,或者我必须删除没有索引值的内容。

附笔: 对不起,我的语言不得体,希望你能理解我的问题。 请有人把我的问题恰当地回答好。谢谢。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38638
 
581 次点击  
文章 [ 2 ]  |  最新文章 4 年前