将字符串列表与浮点列表相关联

时间:2017-04-27 13:19:21

标签: python string list

所以我意识到我需要将内容添加到空列表中。我如何做我评论的内容,我已经在Pseudocode中写出来但我实际上并不知道如何完全编写代码。

var countries = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 5,
    remote: {
        url: 'http://localhost:5000/api/compositesearch/%QUERY',
        wildcard: '%QUERY',
        transform: function(d) {
            return d.countries;
        }
    }
});

var cities = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 5,
    remote: {
        url: 'http://localhost:5000/api/compositesearch/%QUERY',
        wildcard: '%QUERY',
        transform: function(d) {
            return d.cities;
        }
    }
});

3 个答案:

答案 0 :(得分:1)

此处它位于“number_letter”函数中的一个衬里中

sorted(dict(zip(a,map(sum, zip(b[0], b[1])))).items(), key=lambda x: x[1], reverse=True)

详情

#sum list column wise
list_sum = map(sum, zip(b[0], b[1]))
# create a dictionary with key and value list
dictionary = dict(zip(a,list_sum))
#sort it by value in descending order
sorted(dictionary, key=lambda x: x[1], reverse=True)

答案 1 :(得分:0)

一个班轮

def number_letter(a, b):
    return [[*x]for x in sorted(zip(map(sum,zip(*b)),a),key=lambda x:x[0],reverse=True)]

编辑: IDLE会话

>>> b
[[0.5, 0.5, 0], [0.05, 0.3, 0.65]]

>>> list(zip(b[0],b[1]))
[(0.5, 0.05), (0.5, 0.3), (0, 0.65)]  #zips elements from argument iterables together

>>> sum( (1,2) )  #the name says it all
3

>>> foo=lambda a,b: a+b  #lambdas are temporary functions generally passed in as argument to other functions
>>> foo(1, 2)
3

>>> list(map( int, ['1', '2'] ))  #maps each of the value(s) from the argument iterable(s) to the function (int in this case)
[1, 2]
>>> list(map(foo, [1,2], [4,5]))
[5, 7]

>>> print(b)
[[0.5, 0.5, 0], [0.05, 0.3, 0.65]]
>>> print(*b)
[0.5, 0.5, 0] [0.05, 0.3, 0.65]  # * unpacks the iterable's elements

>>> sorted([2, 4, 7, 1, 3],reverse=True)  #returns a new sorted list, passing keyword arg reverse as True sorts in descending order
[7, 4, 3, 2, 1]

>>> #Now for the best part: list comprehensions (or comprehensions in general)
>>> lst = [1,2,3,4]
>>> [ 3*x for x in lst]  #i'd suggest you read about it as i don't want to ruin learning for you, i'm sorry if i did already.
[3, 6, 9, 12]

编辑2:将所有内容放在一起。

>>> a
['A', 'B', 'C']
>>> b
[[0.5, 0.5, 0], [0.05, 0.3, 0.65]]

>>> x = list(map(sum,zip(*b))) #zip b[0] and b[1] and find the sum
>>> print(x)
[0.55, 0.8, 0.65]
>>> x2 = list(zip(x,a)) #[(0.55, 'A'), (0.8, 'B'), (0.65, 'C')]

>>> x3 = sorted(x2,key=lambda x:x[0],reverse=True) #sort x2 in desc order of first elements
>>> print(x3)
[(0.8, 'B'), (0.65, 'C'), (0.55, 'A')]

>>> #this is only for OP's requirement of the elements to be lists
>>> y = [ [*k] for k in x3]
>>> print(y)
[[0.8, 'B'], [0.65, 'C'], [0.55, 'A']]

答案 2 :(得分:0)

这一切都取决于argsort实现,对于Python,您需要使用numpy.argsort

In [16]: def number_letter(a, b):
    ...:     sums = list(map(sum, zip(*b)))
    ...:     return [[sums[i], a[i]] for i in numpy.argsort(sums)[::-1]]
    ...: 

In [17]: number_letter(["A", "B", "C"], [[0.5, 0.5, 0], [0.05, 0.3, 0.65]])
Out[17]: [[0.8, 'B'], [0.65, 'C'], [0.55, 'A']]

对于纯Python,您需要实现自己的argsort函数。

相关问题