如何选择列表中最低10%的数字?

时间:2019-06-04 09:38:17

标签: python min

我想获得列表中最低的10%数字。

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(cors());

app.post('/api/restaurants', (req, res, next) => {
    var errors = [];
    if(!req.query.name){
        errors.push("No name specified");
    }
    if(!req.query.description){
        errors.push("No description specified");
    }
    if(!req.query.places_free){
        errors.push("No description specified");
    }
    if(!req.query.latitude){
        errors.push("No latitude specified");
    }
    if(!req.query.longitude){
        errors.push("No longitude specified");
    }
    if(!req.query.phone){
        errors.push("No phone specified");
    }
    if(!req.query.website){
        errors.push("No website specified");
    }

    if(errors.length){
        res.status(400).json({"error": errors.join(',')});
        return;
    }

    var data = {
        name: req.body.name,
        description: req.body.description,
        places_free: req.body.places_free,
        latitude: req.body.latitude,
        longitude: req.body.longitude,
        phone: req.body.phone,
        website: req.body.website
    }

    var sql = "INSERT INTO restaurants (name, description, places_free, latitude, longitude, phone, website) VALUES (?, ?, ?, ?, ?, ?, ?)";
    var params = [data.name, data.description, data.places_free, data.latitude, data.longitude, data.phone, data.website];
    db.run(sql, params, (err, result) => {
        if(err){
            res.status(400).json({"error": err.message});
            return;
        } else {
            res.json({
                "status" : "succes",
                "data": data
            });
        }
    });
});

从上面的列表中,我希望得到结果。     结果= [1,2] 这是列表中最低的10%。

6 个答案:

答案 0 :(得分:2)

result = List[:int(len(List)*0.1)]

答案 1 :(得分:2)

如果每个元素都是唯一的,则可以简单地对数据进行排序和切片

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 < np.percentile(l, 10)]) # 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

答案 2 :(得分:2)

尝试一下-

sorted(lis)[:int((0.1 * len(lis)))]

其中lis是您的列表。

答案 3 :(得分:2)

如果要使用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)])

答案 4 :(得分:2)

你在这里 = ^ .. ^ =

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]

答案 5 :(得分:1)

A  B                C          D    E
8  a5747dfae47cccc8 7382625596 Anil 2017-08-07 16:51:58.753
9  a5747dfae47cccc8 7382625596 Anil 2017-08-07 17:21:11.416
10 a5747dfae47cccc8 7382625596 Anil 2017-08-07 17:41:09.771
11 a5747dfae47cccc8 7382625596 Anil 2017-08-07 18:01:22.448