简体   繁体   English

如何匹配带有名称的列表和带有数字的列表

[英]How to match a lists with name and a lists with numbers

Beginner here.初学者在这里。 I want to assign each name with a number, so I can find the name of the person with the biggest number.我想给每个名字分配一个数字,这样我就可以找到数字最大的人的名字。 I can find the biggest number using max(), but how do I find the name that has the biggest number?我可以使用 max() 找到最大的数字,但是如何找到具有最大数字的名称?

for example:例如:

name = ['John', 'Joe','Jae']
number = ['10','20','30']

biggest_number = max(number)
name = ????

How do I match these 2 lists to find the name?如何匹配这两个列表以找到名称?

You can use the .index() method on a list: it will give you the position of '30' in number :您可以在列表中使用.index()方法:它将为您提供编号为“30”的number

position = number.index("30")
name[position]
# 'Jae'

You can get the index of that number and use the index to access the first list.您可以获取该数字的索引并使用该索引访问第一个列表。 Like so:像这样:

indx = number.index(biggest_number)
biggest_name = name[indx]

Or in a single line或者在一行中

biggest_name = name[number.index(biggest_number)]

Find the index of the biggest number then based on the index find the name.找到最大数字的索引,然后根据索引找到名称。

name = ['John', 'Joe','Jae']
number = ['10','20','30']

biggest_number = max(number)
max_index = number.index(biggest_number)
print(name[max_index])

What you want is to have an "associative array" between the number and the name.您想要的是在数字和名称之间有一个“关联数组”。 In Python that is done with a dictionary, where the key is the number and the value is the name.在 Python 中使用字典完成,其中键是数字,值是名称。

You could do it with this:你可以这样做:

name = ['John', 'Joe','Jae']
number = ['10','20','30']
num_to_name = { int(number): name for number, name in zip(number, name) }
print(num_to_name[max(num_to_name)])

The notation { x: y for x, y in collection } is called a dict comprehension and is an efficient way of defining a dictionary when all values are known.符号{ x: y for x, y in collection }称为字典推导式,是在所有值都已知时定义字典的有效方法。

Also notice that I took the liberty to convert the numbers to integers when using them as keys, because if you keep them as strings, you might have surprises, like:另请注意,在将数字用作键时,我冒昧地将数字转换为整数,因为如果将它们保留为字符串,您可能会感到惊讶,例如:

numbers = ["90", "100"]
max(numbers)
'90'

Here, as they are strings, a lexicographical order search would be performed, and as the character '9' happens after character '1', then the program would assume that '90' is bigger than '100'.在这里,由于它们是字符串,因此将执行字典顺序搜索,并且由于字符 '9' 出现在字符 '1' 之后,因此程序将假定 '90' 大于 '100'。

Using integers you'd get the behaviour you'd expect.使用整数你会得到你期望的行为。

You don't want the max(number) , you want the position where the max number occurs.你不想要max(number) ,你想要最大数量出现的 position There exist various ways to do it, but the most straighforward is to use numpy's index = np.argmax(numbers) to get the position, and the name = names[index]有多种方法可以做到,但最直接的方法是使用 numpy 的index = np.argmax(numbers)来获取 position,并且name = names[index]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM