python列表中怎么存小数_排序python中的小数列表(sorting list of decimals in python)

排序python中的小数列表(sorting list of decimals in python)

我有以下列表,我想排序:

['104.900209904', '238.501860857', '9.59893298149', '362.470027924', '419.737339973']

我用“排序”函数来做到这一点:

Block1_Video1 = sorted(Block1_Video1)

但是,这就是我得到的:

['104.900209904', '238.501860857', '362.470027924', '419.737339973', '9.59893298149']

虽然这是我想要的(一个列表按数字排序,考虑小数点):

[''9.59893298149', 104.900209904', '238.501860857', '362.470027924', '419.737339973']

我怎么能做到这一点?

I have the following list that I'd like to sort:

['104.900209904', '238.501860857', '9.59893298149', '362.470027924', '419.737339973']

I used the "sorted" function to do it:

Block1_Video1 = sorted(Block1_Video1)

However, this is what I get:

['104.900209904', '238.501860857', '362.470027924', '419.737339973', '9.59893298149']

Whereas this is what I want (a list sorted numerically, taking decimal points into account):

[''9.59893298149', 104.900209904', '238.501860857', '362.470027924', '419.737339973']

How can I accomplish this?

原文:https://stackoverflow.com/questions/39005831

2019-11-09 16:15

满意答案

排序需要基于列表中相应字符串的浮点值。

Block1_Video1 = ['104.900209904', '238.501860857', '9.59893298149', '362.470027924', '419.737339973']

sorted(Block1_Video1,key=lambda x:float(x))

或者只是使用

sorted(Block1_Video1, key=float)

The sorting needs to be based on the float values of the corresponding strings in the list.

Block1_Video1 = ['104.900209904', '238.501860857', '9.59893298149', '362.470027924', '419.737339973']

sorted(Block1_Video1,key=lambda x:float(x))

Or simply use

sorted(Block1_Video1, key=float)

2016-08-18

相关问答

您要求输入精确度为零的有效数字。 如果您不允许存储任何数字,则所有数字都将为零。 请注意,精度是重要的小数位数,也就是说,不包括前导零。 0.5有一个有效数字, 1.5有两个。 另请注意, Decimal使用银行家舍入 。 You asked for zero significant digits of precision. If you don't allow any digits at all to be stored, all numbers will be zero. Note that ...

排序需要基于列表中相应字符串的浮点值。 Block1_Video1 = ['104.900209904', '238.501860857', '9.59893298149', '362.470027924', '419.737339973']

sorted(Block1_Video1,key=lambda x:float(x))

或者只是使用 sorted(Block1_Video1, key=float)

The sorting needs to be based on the float ...

如果您有Python 2.6或更新版本,请使用format : '{0:.3g}'.format(num)

对于Python 2.5或更旧版本: '%.3g'%(num)

说明: {0}告诉format打印第一个参数 - 在这种情况下, num 。 冒号后的所有内容(:)指定format_spec 。 .3将精度设置为3。 g删除无关紧要的零。 见http://en.wikipedia.org/wiki/Printf#fprintf 例如: tests=[(1.00, '1'),

...

您正在两次插值: print("The volume of the cylinder is" + str(volume) + "{}cm\u00b3".format(volume))

只需一次就可以: print("The volume of the cylinder is {}cm\u00b3".format(volume))

关于.format()函数的.format()是你可以告诉它将你的数字格式化为一定数量的小数: print("The volume of the cylinder i...

浮点数的工作方式就像科学记数法。 1.0000000000000001e-21适合64位浮点数允许的有效位数/尾数的53位 。 加上1 ,比微小部分大许多数量级,导致细节被丢弃,并且精确地存储1 Floating point numbers work like scientific notation. 1.0000000000000001e-21 fits within the 53 bits of significand/mantissa a 64-bit float allows. Addin...

不知道为什么你会被投票。 decimal.Decimal表示使用基数10中的浮点数。由于它不是直接在硬件中实现,因此可以控制精度级别(默认为28位): >>> from decimal import *

>>> getcontext().prec = 6

>>> Decimal(1) / Decimal(7)

0.142857

但是,您可能更喜欢使用mpmath模块 ,它支持任意精度的实数和复数浮点计算: >>> from mpmath import mp

>>> mp.dps = 50

>>>...

对切片进行排序并将其写回 : >>> PAE[1:4] = sorted(PAE[1:4], key=itemgetter(2))

>>> PAE

[['a', 0, 8], ['b', 2, 1], ['d', 7, 2], ['c', 4, 3], ['e', 8, 4]]

Sort the slice and write it back: >>> PAE[1:4] = sorted(PAE[1:4], key=itemgetter(2))

>>> PAE

[['a', 0, 8], ['b...

在密钥中使用list.index : >>> data = [[1, .45, 0], [2, .49, 2], [3, .98, 0], [4, .82, 1],

[5, .77, 1], [6, .98, 2]]

>>> List_1 = [2, 0, 1]

>>> sorted(data, key=lambda e: (List_1.index(e[2]), -e[1]))

[[6, 0.98, 2], [2, 0.49, 2], [3, 0.98, 0], [1, ...

>>> import collections

>>> d = collections.OrderedDict()

>>> d[1] = 2

>>> d

OrderedDict([(1, 2)])

>>> d[1.234] = 4

>>> d

OrderedDict([(1, 2), (1.234, 4)])

>>> d[1.234]

4

>>> d[round(1.2343, 3)]

4

是的, OrderedDict可以将浮点数作为键。 您所看到的问题是由于其他原因 - 您是否希望在此循环之前...

使用[:] (切片整个列表)来更改旧列表的值,而不是创建新的引用: items[:] = [x for (y, x) in sorted(zip(test_rank, items), key=lambda s: s[0])]

Use [:] (slicing the whole list) to change the value of the old list instead of creating a new reference: items[:] = [x for (y, x) in sor...

相关文章

列表就像java里的collection,所具有的特性也要比元组更多,更灵活,其character总结

...

python2和python3的区别,1.性能 Py3.0运行 pystone benchmark的速

...

该程序是在python2.3上完成的,python版本间有差异。 Mapper: import sys

...

python里的字典就像java里的HashMap,以键值对的方式存在并操作,其特点如下:通过键来存取

...

Python的运算符和其他语言类似(我们暂时只了解这些运算符的基本用法,方便我们展开后面的内容,高级应

...

数据类型:sequence (序列),sequence(序列)是一组有顺序的元素的集合,序列可以包含一

...

java List对象排序有多种方法,下面是其中两种 第一种方法,list中的对象实现Comparab

...

mod_python: the long story - Grisha Trubetskoy

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值