python – 对于给定的浮点数列表,有没有办法可以找到给定整个部分的最大小数部分?

我遇到了这个问题,在那里我被绊倒了.

问题是这个.

我有一个这样的列表:

L = [2.1, 2.2, 2.3, 2.4, 2.5, 3.1, 3.2, 3.3, 3.4, 4.1, 4.2, 4.3, 4.4, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 6.1, 6.2, 6.3, 6.4, 6.5, 7.1, 7.2, 7.3, 7.4, 7.5, 8.1, 8.2, 8.3, 8.4, 8.5, 9.1, 9.2, 9.3, 9.4, 9.5, 10.1, 10.2, 10.3, 10.4, 10.5, 11.1, 11.2, 11.3, 11.4, 11.5, 12.1, 12.2, 12.3, 12.4, 12.5]

需要:
ans = [[2,5],[3,4],[4,4],……]

即整数和最大小数部分.

我们应该怎么做?

到目前为止,我试过这个:

# Imports
import numpy as np
import math

L = [2.1, 2.2, 2.3, 2.4, 2.5, 3.1, 3.2, 3.3, 3.4, 4.1, 4.2, 4.3, 4.4, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 6.1, 6.2, 6.3, 6.4, 6.5, 7.1, 7.2, 7.3, 7.4, 7.5, 8.1, 8.2, 8.3, 8.4, 8.5, 9.1, 9.2, 9.3, 9.4, 9.5, 10.1, 10.2, 10.3, 10.4, 10.5, 11.1, 11.2, 11.3, 11.4, 11.5, 12.1, 12.2, 12.3, 12.4, 12.5]

whole = [ int(str(x).split(".")[0]) for x in L]
frac = [ int(str(x).split(".")[-1]) for x in L]
wf = [[w,f] for w,f in zip(whole,frac)]

wset = list(set(whole))

print(whole)
print(frac)
print(wf)
print(wset)

最佳答案 这是一个numpy解决方案:

L.sort() # skip this if L is already sorted
Li = L.astype(int)
uniq = np.r_[np.where(np.diff(Li))[0], Li.size-1]
int_part = Li[uniq]
max_frac = np.round((L[uniq]-int_part) * 10).astype(int)
np.c_[int_part, max_frac].tolist()
# [[2, 5], [3, 4], [4, 4], [5, 6], [6, 5], [7, 5], [8, 5], [9, 5], [10, 5], [11, 5], [12, 5]]

一些时间:

import numpy as np
import itertools as it
from timeit import timeit

def pp(L):
    Li = L.astype(int)
    uniq = np.r_[np.where(np.diff(Li))[0], Li.size-1]
    int_part = Li[uniq]
    max_frac = np.round((L[uniq]-int_part) * 10).astype(int)
    return np.c_[int_part, max_frac].tolist()

def johnchase(L):
    vals = []
    foo = [str(e).split('.') for e in L]
    for key, group in it.groupby(foo, lambda x: (x[0])):
        vals.append([int(e) for e in max(group)])
    return vals

def bernie(L):
    return [[k, int(round(max(i % 1 for i in g)*10,1))]
            for k, g in it.groupby(L,lambda x:int(x))]

def kprabhakaran(L):
    return [map(int,str(max(list(cgen))).split('.'))
     for c,cgen in it.groupby(L,lambda x:int(x))]

def prep(N, k):
    global L
    L = np.sort(np.random.choice(np.arange(N) / 10, k, False))

N, k, n = 2 * 10**6, 10**6, 10

for func in pp, johnchase, bernie, kprabhakaran:
    print('{:20s}'.format(func.__name__), '{:6.4f} secs'.format(timeit(
        lambda: func(L), lambda: prep(N, k), number=n) / n))

打印:

pp                   0.0379 secs
johnchase            1.7252 secs
bernie               1.4773 secs
kprabhakaran         0.7592 secs
点赞