在python中查找相对于列表col2中的值的col1中的最大值

2024-10-05 14:23:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手。我想找到col2相对于列表col1中的“man”、“women”和“people”的最大值。例如,['men', 12, '1946-Truman.txt'], ['women', 7, '1946-Truman.txt']['people', 49, '1946-Truman.txt']包含男性、女性和人群的col2的最大值。在

一种可能的解决方案是将这个元组列表转换为三个单独的数组,分别用于男性、女性和人群,然后从所有数组中找到最大值。但是,我想要一个更好的解决方案。在

数据:

[['men', 2, '1945-Truman.txt']
['women', 2, '1945-Truman.txt']
['people', 10, '1945-Truman.txt']
['men', 12, '1946-Truman.txt']
['women', 7, '1946-Truman.txt']
['people', 49, '1946-Truman.txt']
['men', 7, '1947-Truman.txt']
['women', 2, '1947-Truman.txt']
['people', 12, '1947-Truman.txt']
['men', 4, '1948-Truman.txt']
['women', 1, '1948-Truman.txt']
['people', 22, '1948-Truman.txt']
['men', 2, '1949-Truman.txt']
['women', 1, '1949-Truman.txt']
['people', 15, '1949-Truman.txt']
['men', 6, '1950-Truman.txt']
['women', 2, '1950-Truman.txt']
['people', 15, '1950-Truman.txt']
['men', 8, '1951-Truman.txt']
['women', 2, '1951-Truman.txt']
['people', 9, '1951-Truman.txt']
['men', 3, '1953-Eisenhower.txt']
['women', 0, '1953-Eisenhower.txt']
['people', 17, '1953-Eisenhower.txt']]

提前谢谢。在


Tags: txt列表数组解决方案peoplecol2col1人群
3条回答

您可以使用pandas包。 通过定义数据帧:

import pandas as pd
df = pd.DataFrame([['men', 2, '1945-Truman.txt'],
                   ['women', 2, '1945-Truman.txt'],
                   ['people', 10, '1945-Truman.txt'],
                   ['men', 12, '1946-Truman.txt'],
                    ['women', 7, '1946-Truman.txt'],
                   ['people', 49, '1946-Truman.txt'],
                   ['men', 7, '1947-Truman.txt'],
                   ['women', 2, '1947-Truman.txt'],
                   ['people', 12, '1947-Truman.txt'],
                   ['men', 4, '1948-Truman.txt'],
                   ['women', 1, '1948-Truman.txt'],
                   ['people', 22, '1948-Truman.txt'],
                   ['men', 2, '1949-Truman.txt'],
                   ['women', 1, '1949-Truman.txt'],
                   ['people', 15, '1949-Truman.txt'],
                   ['men', 6, '1950-Truman.txt'],
                   ['women', 2, '1950-Truman.txt'],
                   ['people', 15, '1950-Truman.txt'],
                   ['men', 8, '1951-Truman.txt'],
                   ['women', 2, '1951-Truman.txt'],
                   ['people', 9, '1951-Truman.txt'],
                   ['men', 3, '1953-Eisenhower.txt'],
                   ['women', 0, '1953-Eisenhower.txt'],
                   ['people', 17, '1953-Eisenhower.txt']])

那么

^{pr2}$

返回

0 
men       12
women      7
people    49
Name: 1, dtype: int64

这就是你想要的吗?在

如果您使用的列表包括:

lst=[['men', 2123, '1945-Truman.txt'],
['women', 2, '1945-Truman.txt'],
['people', 10, '1945-Truman.txt'],
['men', 12, '1946-Truman.txt'],
['women', 7, '1946-Truman.txt'],
['people', 49, '1946-Truman.txt'],
['men', 7, '1947-Truman.txt'],
['women', 2, '1947-Truman.txt']]

然后可以使用以下代码。在

^{pr2}$

这将进入名为lst的位列表中的每个列表,并找到男性、女性和人员的最大值。在

pandas是一个很好的选择,但是您可以使用max和{}:

men = max(data, key=lambda x: x[1] if x[0] == 'men' else 0)
women = max(data, key=lambda x: x[1] if x[0] == 'women' else 0)
people = max(data, key=lambda x: x[1] if x[0] == 'people' else 0)

相关问题 更多 >