简体   繁体   English

获取元组列表中浮点数的最大值?

[英]Get the max value of float numbers on a list on tuples?

i have the following list:我有以下清单:

erra_eus_repo = [(u'RHSA-2017:2796', u'6.7'), (u'RHSA-2017:2796', u'6.8'), (u'RHSA-2017:2794', u'7.2'), (u'RHSA-2017:2793', u'7.3')]

what I am trying to take the floating point numbers from each tuple :我试图从每个tuple获取floating

6.7, 6.8 ,7.2, 7.3

and get the max number for each version that before the dot .ie :并获取点 .ie 之前的每个版本的最大数量:

new_list = [ 6.8, 7.3 ]

Note that max() will not work here, since if I have 5.9 and 5.11, I will get the max as 5.9, I want the result to be 5.11 since 11 > 9.请注意, max()在这里不起作用,因为如果我有 5.9 和 5.11,我将得到最大值为 5.9,我希望结果为 5.11,因为 11 > 9。

What I have tried:我尝试过的:

eus_major = []
eus_minor = []
for major in erra_eus_repo:
    minor = (major[1][2])
    major = (major[1][0])
    if major not in eus_major:
            eus_major.append(major)

    if minor not in eus_minor:
            eus_minor.append(minor)
 print(eus_major, eus_minor)

currently i am getting:目前我得到:

[u'6', u'7'] [u'7', u'2', u'3']

You can achieve this for instance with a combination of groupby and sorting:例如,您可以通过组合groupby和排序来实现这一点:

from itertools import groupby

srt_list = sorted(erra_eus_repo, key=lambda x: x[1]);

max_list = []
for key, group in groupby(srt_list, lambda x: x[1].split('.')[0]):
        max_el = max(list(group), key = lambda y: int(y[1].split('.')[1]))
        max_list.append(float(max_el[1]))

First the array is sorted based on second element of each tuple to get sequences of elements with matching non-decimal number for grouping with groupby .首先,根据每个元组的第二个元素对数组进行排序,以获取具有匹配非十进制数的元素序列,以便与groupby进行分组。 groupby groups the elements into just that - each group will represent a sequence XZ with common X. In each of these sequences - groups the program finds the one with maximum decimal part treated as a stand-along number. groupby将元素分组为 - 每组将代表一个具有公共 X 的序列 XZ。在这些序列中的每一个 - 组中,程序会找到具有最大小数部分的组作为独立数字。 The whole number is then appended to the list with max values as a float.然后将整数附加到列表中,最大值作为浮点数。

Do not treat the version numbers as floating point, treat as '.'不要将版本号视为浮点数,而是将其视为“.”。 separated strings.分隔的字符串。 Then split each version string (split on '.') and compare.然后拆分每个版本字符串(拆分为“.”)并进行比较。 Like this:像这样:

def normalize_version(v):
    return tuple(map(int, v.split('.')))

Then you can see:然后你可以看到:

>>> u'5.11' > u'5.9'
False

>>> normalize_version(u'5.11') > normalize_version(u'5.9')
True

Here is another take on the problem which provides the highest value for each RHSA value (which is want I think you're after):这是对每个 RHSA 值提供最高值的问题的另一种看法(我认为这是您想要的):

erra_eus_repo = [(u'RHSA-2017:2796', u'6.7'), (u'RHSA-2017:2796', u'6.8'), (u'RHSA-2017:2794', u'7.2'), (u'RHSA-2017:2793', u'7.3')]

eus_major = {}

for r in erra_eus_repo:

    if r[0] not in eus_major.keys():
        eus_major[r[0]] = 0

    if float(r[1]) > float(eus_major[r[0]]):
        eus_major[r[0]] = r[1]

print(eus_major)

output:输出:

{'RHSA-2017:2796': '6.8', 'RHSA-2017:2794': '7.2', 'RHSA-2017:2793': '7.3'}

I left the value as a string, but it could easily be cast as a float.我将该值保留为字符串,但它可以轻松转换为浮点数。

The following simply uses the built-in min and max functions:下面简单地使用内置的minmax函数:

erra_eus_repo = [(u'RHSA-2017:2796', u'6.7'),
                 (u'RHSA-2017:2796', u'6.8'),
                 (u'RHSA-2017:2794', u'7.2'),
                 (u'RHSA-2017:2793', u'7.3')]

eus_major = max(float(major[1])for major in erra_eus_repo)
eus_minor = min(float(major[1])for major in erra_eus_repo)
newlist = [eus_minor, eus_major]
print(newlist)  # -> [6.7, 7.3]

This may look like you're trying to compare decimal values, but you really aren't.这可能看起来像您正在尝试比较十进制值,但实际上并非如此。 It goes without saying (but I will) that while 9<11, .9>.11.不言而喻(但我会),而 9<11, .9>.11。 So the idea of splitting the number into two separate values is really the only way to get a valid comparison.因此,将数字分成两个单独的值的想法实际上是获得有效比较的唯一方法。

The list is a list of lists - you have the master and each has a sub-list of RHSA and a value.该列表是一个列表列表 - 您拥有主列表,每个列表都有一个 RHSA 子列表和一个值。 Apparently you want discard the first item in the list and only get the (I assume) version of that item.显然,您想丢弃列表中的第一个项目,只获得该项目的(我假设)版本。 Here's some code that, while crude, will give you an idea of what to do.这里有一些代码,虽然很粗糙,但会让你知道该怎么做。 (I'd welcome comments on how to clean that up...) So I've taken the lists, split them, then split the versions into major and minor, then compared them and if nothing exists in the list, add the value. (我欢迎对如何清理它的评论......)所以我已经获取了列表,将它们拆分,然后将版本分为主要版本和次要版本,然后比较它们,如果列表中不存在任何内容,请添加值. I also added for sake of testing a 6.11 version number.我还添加了为了测试 6.11 版本号。

lstVersion = []
lstMaxVersion=[]
erra_eus_repo = [(u'RHSA-2017:2796', u'6.7'), (u'RHSA-2017:2796', u'6.8'), (u'RHSA-2017:2796', u'6.11'), (u'RHSA-2017:2794', u'7.2'), (u'RHSA-2017:2793', u'7.3')]
for strItem in erra_eus_repo:
    lstVersion.append(strItem[1])

for strVersion in lstVersion:
    blnAdded = False
    intMajor = int(strVersion.split('.')[0])
    intMinor = int(strVersion.split('.')[1])
    print 'intMajor: ', intMajor
    print 'intMinor:' , intMinor
    for strMaxItem in lstMaxVersion:
        intMaxMajor = int(strMaxItem.split('.')[0])
        intMaxMinor = int(strMaxItem.split('.')[1])
        print 'strMaxitem: ', strMaxItem
        print 'intMaxMajor: ', intMaxMajor
        print 'intMaxMinor: ', intMaxMinor
        if intMajor == intMaxMajor:
            blnAdded = True
            if intMinor > intMaxMinor:
                lstMaxVersion.remove(strMaxItem)
                lstMaxVersion.append(str(intMajor)+'.'+str(intMinor))
    if not blnAdded:
        lstMaxVersion.append(str(intMajor)+'.'+str(intMinor))

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

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