Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:按数量级对列表进行分类_Python - Fatal编程技术网

Python:按数量级对列表进行分类

Python:按数量级对列表进行分类,python,Python,我有一个带值的嵌套列表: list = [ ... ['Country1', 142.8576737907048, 207.69725105029553, 21.613192419863577, 15.129178465784218], ['Country2', 109.33326343550823, 155.6847323746669, 15.450489646386226, 14.131554442715336], ['Country3', 99.23033109735835, 115.37

我有一个带值的嵌套列表:

list = [
...
['Country1', 142.8576737907048, 207.69725105029553, 21.613192419863577, 15.129178465784218],
['Country2', 109.33326343550823, 155.6847323746669, 15.450489646386226, 14.131554442715336],
['Country3', 99.23033109735835, 115.37122637190915, 5.380298424850267, 5.422030104456135],
...]
我想按数量级计算第二个索引/列中的值,从最低数量级开始,到最大数量级结束…例如

99.23033109735835 = 10 <= x < 100
142.8576737907048 = 100 <= x < 1000
             9432 = 1000 <= x < 10000

99.23033109735835=10此函数将双精度转换为整数量级:

>>> def magnitude(x):
...     return int(math.log10(x))
... 
>>> magnitude(99.23)
1
>>> magnitude(9432)
3

(因此,
10**magnity(x)如果
x
是你的数字之一,那么
len(str(int(x))是什么?

或者,如果数字小于0,那么什么是
int(math.log10(x))

(另请参阅的文档。还请注意,此处的int()舍入可能不是您想要的-请参阅和,并注意您可能需要
int(ceil(…)
int(floor(…)
来获得整数答案)

导入对分
从集合导入defaultdict
lis1=[[Country1',142.8576737907048,207.69725105029553,21.613192419863577,15.129178465784218],
[Country2',109.33326343550823,155.6847323746669,15.450489646386226,14.131554442715336],
[Country3',99.23033109735835,115.37122637190915,5.380298424850267,5.422030104456135],
]
lis2=[011001000]
dic=默认DICT(整数)
对于lis1中的x:
x=x[1]
ind=对分。对分(lis2,x)

如果不是(x>=lis2[-1]或x以数量级进行分类,请执行以下操作:

from math import floor, log10
from collections import Counter
counter =  Counter(int(floor(log10(x[1]))) for x in list)
1是从10到小于100,2是从100到小于1000

print counter
Counter({2: 2, 1: 1})
然后它只是简单地打印出来

for x in sorted(counter.keys()):
    print "%d <= x < %d: %d" % (10**x, 10**(x+1), counter[x])
排序(counter.keys())中x的

打印“%d,以防需要重叠范围或具有任意边界的范围(不坚持数量级/2次幂/任何其他可预测序列):

从集合导入defaultdict
lst=[
[Country1',142.8576737907048,207.69725105029553,21.613192419863577,15.129178465784218],
[Country2',109.33326343550823,155.6847323746669,15.450489646386226,14.131554442715336],
[Country3',99.23033109735835,115.37122637190915,5.380298424850267,5.422030104456135],
]
桶={

'10另一个选项,使用
bisect

import bisect
from collections import Counter
list0 = [
['Country1', 142.8576737907048, 207.69725105029553, 21.613192419863577, 15.129178465784218],
['Country2', 109.33326343550823, 155.6847323746669, 15.450489646386226, 14.131554442715336],
['Country3', 99.23033109735835, 115.37122637190915, 5.380298424850267, 5.422030104456135]
]

magnitudes = [10**x for x in xrange(5)]
c = Counter(bisect.bisect(magnitudes, x[1]) for x in list0)
for x in c:
  print x, '#'*c[x]
将“答案”扩展到所有实数,您可以使用:

import math

def magnitude (value):
    if (value == 0): return 0
    return int(math.floor(math.log10(abs(value))))
测试用例:

In [123]: magnitude(0)
Out[123]: 0

In [124]: magnitude(0.1)
Out[124]: -1

In [125]: magnitude(0.02)
Out[125]: -2

In [126]: magnitude(150)
Out[126]: 2

In [127]: magnitude(-5280)
Out[127]: 3

<代码>大小(6)==(3)此定义。这是不正确的。真的,我没有考虑负量<代码> int(Load(Log10(x)))< /Case>做这项工作。我将把它留给你的答案,但是既然你已经删除了它,我将在这里编辑它的完整性……我实际上会接受数学。CELL().你希望看到的价值范围是什么?在数学框之外思考得很好,但不要把你的答案写成问题;相反,也许可以给出实际的结果
for x in sorted(counter.keys()):
    print "%d <= x < %d: %d" % (10**x, 10**(x+1), counter[x])
from collections import defaultdict
lst = [
    ['Country1', 142.8576737907048, 207.69725105029553, 21.613192419863577, 15.129178465784218],
    ['Country2', 109.33326343550823, 155.6847323746669, 15.450489646386226, 14.131554442715336],
    ['Country3', 99.23033109735835, 115.37122637190915, 5.380298424850267, 5.422030104456135],
]

buckets = {
    '10<=x<100': lambda x: 10<=x<100,
    '100<=x<1000': lambda x: 100<=x<1000,
}

result = defaultdict(int)
for item in lst:
    second_column = item[1]
    for label, range_check in buckets.items():
        if range_check(second_column):
            result[label] +=1

print (result)
import bisect
from collections import Counter
list0 = [
['Country1', 142.8576737907048, 207.69725105029553, 21.613192419863577, 15.129178465784218],
['Country2', 109.33326343550823, 155.6847323746669, 15.450489646386226, 14.131554442715336],
['Country3', 99.23033109735835, 115.37122637190915, 5.380298424850267, 5.422030104456135]
]

magnitudes = [10**x for x in xrange(5)]
c = Counter(bisect.bisect(magnitudes, x[1]) for x in list0)
for x in c:
  print x, '#'*c[x]
import math

def magnitude (value):
    if (value == 0): return 0
    return int(math.floor(math.log10(abs(value))))
In [123]: magnitude(0)
Out[123]: 0

In [124]: magnitude(0.1)
Out[124]: -1

In [125]: magnitude(0.02)
Out[125]: -2

In [126]: magnitude(150)
Out[126]: 2

In [127]: magnitude(-5280)
Out[127]: 3