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

Python 将字符串列表与浮点列表关联,python,string,list,Python,String,List,所以我意识到我需要把东西添加到一个空列表中。我怎么做我评论过的,我已经用伪代码写出来了,但我不知道如何完整地编写它 def number_letter(a, b): lst = [] ##a is the letters ["A", "B", "C"] ##b is the lists of numbers [[0.5, 0.5, 0], [0.05, 0.3, 0.65]] ##together if a user was calling it, it would look like this

所以我意识到我需要把东西添加到一个空列表中。我怎么做我评论过的,我已经用伪代码写出来了,但我不知道如何完整地编写它

def number_letter(a, b):
lst = []
##a is the letters ["A", "B", "C"]
##b is the lists of numbers [[0.5, 0.5, 0], [0.05, 0.3, 0.65]]
##together if a user was calling it, it would look like this
##number_letter(["A", "B", "C"], [[0.5, 0.5, 0], [0.05, 0.3, 0.65]])
##Outputs relating numbers from 'b' with letters from 'a' in descending order (the values are summed)
##[[0.8, "B"], [0.65, "C"], [0.55, "A"]]
一班轮

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

>>> 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']]

这一切都取决于
argsort
实现,对于Python,您将希望使用它


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

在这里,它在“number\u 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)

你是在问什么还是在想什么?好的。你的问题是什么?你的问题是什么?
zip()
就是你要找的for@Chris_Rands只是查看了函数,这很有趣,但是在将其添加到字母之前,我如何对这些数字求和?欢迎提供任何建议。:)亲爱的玛丽妈妈,这很管用,但请解释一下怎么做!我对python非常陌生,不知道什么是键,什么是映射,什么是lambda!如果您不怕阅读,python文档是一个很好的起点。x前面的“*”在for循环中做什么@Frederick99请注意,您不需要
zip(b[1],b[0])
,因为您正在将
sum
映射到它。这两个
b
列表的元素在
zip
中的排列顺序无关紧要,事实上它可以是
map(sum,zip(*b))
。非常感谢您的建议和回答,我对python非常陌生,你介意用argsort函数来解释吗?此外,还有一种更长但可能更简单的方法吗?它在Python中实现起来很简单,只是语法可能有点棘手。这是将这个答案与另一个“一行”答案联系起来的原因。Argosrt通常是一种算法,它为您提供排序后产生的新索引位置。在你的例子中,按总和排序意味着索引0到索引2(0.55到最后),索引1到索引0(0.8到第一),索引2到索引1(0.65到中间)。因此,权重的argsort将是
[1,2,0]
。由于
numpy.argsort
假定数据按升序排序,因此我必须在其末尾添加
[::-1]
语法,这是用于反转iterable序列元素的Python切片语法,有效地将其视为降序排序。
#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)