如何在python中显示数组的百分比

时间:2015-11-02 05:19:52

标签: python arrays numpy

你如何打印一个数组的百分比?

例如:

如果我有

<! DOCTYPE html>

<html>
    <head>
    <script type="text/javascript" src="jquery.min.js"></script>
    </head>

    <body>
                <section>
                    <h3> welcome to Iframe</h3>                      

                    <script type="text/javascript">


                        $(function(){
                            $('#submit').click(function(){
                               $url = $('#iframeContent').attr('src');
                               $('#demo').html($url);                               
                            });
                        });


                    </script>

                    <input type="submit" id="submit" value="showUrl"/> 
                    <div id="demo"></div> 

                    <iframe id="iframeContent" src="http://lumextech.com" style="min-height: 500px; min-width: 500px;"></iframe>               


                </section>
    </body>

</html> 

如何将数组的百分比设置为零?如果我想保持数组的前10%,并将数组的剩余90%更改为零?

提前谢谢你?

5 个答案:

答案 0 :(得分:7)

这将在前面给你大约90%:

x[0:int(len(x)*0.9)]

后面90%(跳过前10%):

x[int(len(x)*0.1):]

所以将最后的90%设置为零:

x[int(len(x)*0.1):] = 0

答案 1 :(得分:2)

你可以这样做:

import numpy as np

x = np.array([2,3,1,0,4,3,5,4,3,2,3,4,5,10,15,120,102,10])

cut_off = int(0.1*len(x))

print(len(x), cut_off)
for idx in range(cut_off,len(x)):
    x[idx] = 0

答案 2 :(得分:1)

x = [2,3,1,0,4,3,5,4,3,2,3,4,5,10,15,120,102,10]
index = 10 * len(x) / 100
x[index:] = [0]*(len(x) - 1 index )
print x
>>> x = [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

答案 3 :(得分:1)

以下是我要做的事情:

x = np.array([2,3,1,0,4,3,5,4,3,2,3,4,5,10,15,120,102,10])
change = round(0.9 * len(x)) # changing 90%
x[-change:] = 0 # change from last value towards beginning of array
print(x)

产生

[2 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

答案 4 :(得分:0)

做这个工作吗?

x = np.array([2,3,1,0,4,3,5,4,3,2,3,4,5,10,15,120,102,10])
j=len(x)
k=(j/100)*10
for index in range(k,j):
   x[index]=0