[英]How do I find find the index of the greatest integer in a list that contains integers and strings in Python?
Thats how the list looks like这就是列表的样子
incomes = ['Kozlowski', 52000, 'Kasprzak', 51000, 'Kowalski', 26000]
I want to print the biggest income and the surname of the person with that income (index of the income - 1)我想打印最大的收入和那个收入的人的姓氏(收入指数 - 1)
You can try this:你可以试试这个:
index_of_highest_salary = incomes.index(np.max(incomes[1::2]))
You can create a dict
from your data, then use max
to find the key with the largest corresponding value.您可以根据数据创建dict
,然后使用max
来查找对应值最大的键。
>>> incomes = ['Kozlowski', 52000, 'Kasprzak', 51000, 'Kowalski', 26000]
>>> data = dict(zip(incomes[::2], incomes[1::2]))
>>> data
{'Kozlowski': 52000, 'Kasprzak': 51000, 'Kowalski': 26000}
>>> max(data.items(), key=lambda i: i[1])
('Kozlowski', 52000)
Then you don't need indexing and the data is more structured.然后你不需要索引,数据更有条理。
If your pattern is ["SURNAME_1", INCOME_1, "SURNAME_2", INCOME_2, ... ]
, then you can do this:如果您的模式是["SURNAME_1", INCOME_1, "SURNAME_2", INCOME_2, ... ]
,那么您可以这样做:
prices = incomes[1::2] # This will return all integers
names = incomes[::2] # This will return all surnames
max_price = max(prices)
max_price_index = prices.index(max_price)
person = names[max_price_index]
but you should really change it to dictionary, as it is easier to work with, and it's more efficient但你真的应该把它改成字典,因为它更容易使用,而且效率更高
Your title question is different to what you're asking, so here's an answer for your title: If you don't want to use other libraries like numpy, you can do this:您的标题问题与您要问的不同,所以这里是您标题的答案:如果您不想使用其他库,如 numpy,您可以这样做:
index = 0
max_n = None
for i in range(len(incomes)):
element = incomes[i]
if type(element) == int or type(element) == float:
if max_n is None or element > max_n:
max_n = element
index = i
index
will hold the index of the largest number of the list. index
将保存列表中最大数量的索引。
This answer assumes you want the index of the entry as stated in the title of your question.此答案假设您需要问题标题中所述的条目索引。
Use enumerate
to create data where index and value are combined.使用enumerate
创建结合了索引和值的数据。
incomes = ['Kozlowski', 52000, 'Kasprzak', 51000, 'Kowalski', 26000]
print(list(enumerate(incomes[1::2])))
This will give you [(0, 52000), (1, 51000), (2, 126000)]
.这会给你[(0, 52000), (1, 51000), (2, 126000)]
。
We can now feed this data to max
and use a key function that gives us the second entry of each tuple.我们现在可以将此数据提供给max
并使用键 function 为我们提供每个元组的第二个条目。 When we get this tuple we can get the index from the first entry in the tuple.当我们得到这个元组时,我们可以从元组的第一个条目中得到索引。 Since we left out every second element (the name) this index must be multiplied by 2
.由于我们省略了每个第二个元素(名称),因此该索引必须乘以2
。
incomes = ['Kozlowski', 52000, 'Kasprzak', 51000, 'Kowalski', 26000]
max_income = max(enumerate(incomes[1::2]), key=lambda x: x[1])
print(max_income[0] * 2)
If you want the index and the entries the code can be adjusted.如果您想要索引和条目,可以调整代码。
max_income = max(enumerate(zip(incomes[::2], incomes[1::2])), key=lambda x: x[1][1])
print(max_income)
This will give you a tuple with an index as the first entry and a tuple with name and income as the second entry.这将为您提供一个以索引作为第一个条目的元组,以及一个以名称和收入作为第二个条目的元组。 To map this index to your incomes
list it will have to be multiplied by 2
.要将 map 这个索引添加到您的incomes
列表中,它必须乘以2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.