如何选择列表中最低的 10% 的数字?
How to choose the numbers which are lowest 10% in the list?
我想获得列表中最低的 10% 的数字。
List = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
从上面的列表中,我希望得到结果。
结果 = [1,2]
这是列表中最低的 10%。
result = List[:int(len(List)*0.1)]
如果每个元素都是唯一的,您可以简单地对数据进行排序和切片
l = list(range(1, 21))
number_value_to_get = len(l)//10
print(sorted(l)[:number_value_to_get]
但是,在大多数情况下,这是错误的,你可以使用numpy版本
import numpy as np
l = np.array(range(1, 21))
threshold = np.percentile(l, 10) # calculate the 10th percentile
print(l[l < threshold]) # Filter the list.
注意需要定义这是否包含 10%
import numpy as np
l = np.array([1]*20)
threshold = np.percentile(l, 10)
print(l[l < np.percentile(l, 10)]) # Gives you empty list
print(l[l <= np.percentile(l, 10)]) # Gives you full list
试试这个 -
sorted(lis)[:int((0.1 * len(lis)))]
其中 lis
是您的列表。
# list of values
lstValues = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
# get maximum value out of list values
max = max(lstValues)
# calculate 10 percent out of max value
max /= 10
# print all the elements which are under the 10% mark
print([i for i in lstValues if i <= max])
如果你想使用 numpy,有一个内置的百分位数函数:
import numpy
l = numpy.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
print(l[l < numpy.percentile(l,10)])
给你
=^..^=
import numpy as np
List = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
percent = 10
values = list(sorted(np.asarray(List, dtype=np.int))[:int(len(List)/(100/percent))])
输出:
[1, 2]
mylist=[int(x) for x in range(1,21)]
mylist.sort()
newlist=[]
for i in range(len(mylist)//10): #just index through the 10% elements with this
newlist.append(mylist[i])
print(newlist)
我想获得列表中最低的 10% 的数字。
List = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
从上面的列表中,我希望得到结果。 结果 = [1,2] 这是列表中最低的 10%。
result = List[:int(len(List)*0.1)]
如果每个元素都是唯一的,您可以简单地对数据进行排序和切片
l = list(range(1, 21))
number_value_to_get = len(l)//10
print(sorted(l)[:number_value_to_get]
但是,在大多数情况下,这是错误的,你可以使用numpy版本
import numpy as np
l = np.array(range(1, 21))
threshold = np.percentile(l, 10) # calculate the 10th percentile
print(l[l < threshold]) # Filter the list.
注意需要定义这是否包含 10%
import numpy as np
l = np.array([1]*20)
threshold = np.percentile(l, 10)
print(l[l < np.percentile(l, 10)]) # Gives you empty list
print(l[l <= np.percentile(l, 10)]) # Gives you full list
试试这个 -
sorted(lis)[:int((0.1 * len(lis)))]
其中 lis
是您的列表。
# list of values
lstValues = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
# get maximum value out of list values
max = max(lstValues)
# calculate 10 percent out of max value
max /= 10
# print all the elements which are under the 10% mark
print([i for i in lstValues if i <= max])
如果你想使用 numpy,有一个内置的百分位数函数:
import numpy
l = numpy.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
print(l[l < numpy.percentile(l,10)])
给你 =^..^=
import numpy as np
List = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
percent = 10
values = list(sorted(np.asarray(List, dtype=np.int))[:int(len(List)/(100/percent))])
输出:
[1, 2]
mylist=[int(x) for x in range(1,21)]
mylist.sort()
newlist=[]
for i in range(len(mylist)//10): #just index through the 10% elements with this
newlist.append(mylist[i])
print(newlist)