English 中文(简体)
图一
原标题:sorting floats numerically - not in ascii order - in python

这可能像一个sil然的问题,但我试图找到一个没有多大成功的解决办法。 我收到一份名单:

for v in sorted(list):
    print v


[885.1, 12824]
[885.1, 19843]
[885.11, 1319]
[885.12, 1155]
[885.13, 12844]
[885.14, 33602]
[885.15, 11324]
[885.16, 44040]
[885.2, 119283]
[885.3, 8424]

我通过我的清单,利用分类的功能——然而,如上所述,将ASCII中的项目归类为人类可读顺序的浮动? 即:

[885.1, 19843]
[885.2, 119283]
[885.3, 8424]
[885.11, 1319]
[885.12, 1155]
[885.13, 12844]
[885.14, 33602]
[885.15, 11324]
[885.16, 44040]

Do I need to create an index of somekind? Convert my floats to ints? Any help much appreciated.

David。

问题回答

将浮标改为先铺,然后按“:

sorted(a, key=lambda x:map(int, str(float(x[0])).split(".")))

这称为“猎物”(自然)。 http://www.skynet.ie/~caolan/Packages/python-natsort.html”rel=“nofollow” http://www.skynet.ie/~caolan/Packages/python-natsort.html。 (后经审判)。 或许可以帮助你。

Oh and that s not necessarily ASCII sort, it s just the number order, you know, like the real axis

你们需要发挥比较功能。 类似于:

sorted(list, cmp=lambda x, y: cmp(x[0], y[0]))

Just write the function you need for what you want and plug it in.

You ll probably want something like a reverse radix sort.

This is kind of a hack, but:

a = [
    [885.1, 19843],
    [885.1, 12824],
    [885.11, 1319],
    [885.12, 1155],
    [885.13, 12844],
    [885.14, 33602],
    [885.15, 11324],
    [885.16, 44040],
    [885.2, 119283],
    [882.8, 8424],
    [882.75, 8424],
    [885.3, 8424]
]

for v in sorted(a, key=lambda t: str(t[0]).split(".")[0] + ("%05d" % int(str(t[0]).split(".")[1])) + "," + str(t[1])):
    print v

Result is

[882.8, 8424]
[882.75, 8424]
[885.1, 12824]
[885.1, 19843]
[885.2, 119283]
[885.3, 8424]
[885.11, 1319]
[885.12, 1155]
[885.13, 12844]
[885.14, 33602]
[885.15, 11324]
[885.16, 44040]

免责:这假定在ci点之后的5个地方。 相应调整。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签