具有唯一值的圆形 Python 列表
Round Python list with unique values
我想要 "round" 一个有序的数值列表,可以是(正/负)浮点数或整数形式。我不希望输出中有相同的值,除非输入值本身相同。理想情况下,我希望四舍五入到最接近的 5 或 10,以尽可能高的幅度执行,然后向下舍入,直到相邻值之间不匹配。
以下是我正在寻找的一些示例:
[-0.1, 0.21, 0.29, 4435.0, 9157, 9858.0, 10758.0, 11490.0, 12111.9]
结果:
[-0.1, 0.0, 0.25, 5000.0, 9000.0, 10000.0, 11000.0, 11500.0, 12000.0]
这是我目前的情况:
def rounder(n, base=1):
base = base * (10 ** (len(str(abs(n))) - len(str(abs(n)))))
return base * round(float(n)/base)
for i in range(len(inp_values)-1):
while True:
a = rounder(inp_values[i], 10**((len(str(abs(int(inp_values[i])))))-(i+1)) / 2)
b = rounder(inp_values[i+1], 10**((len(str(abs(int(inp_values[i+1])))))-(i+1)) / 2)
print a, b
if a < b:
break
如有任何帮助,我们将不胜感激。
如果您保留一个数字字典(在 round = key 之前,round = value 之后),并编写一个 for 循环,如果舍入值在字典中发生冲突,则使用较低的精度进行舍入?例如:
from math import log10, floor
def roundSD(x, sd):
"Returns x rounded to sd significant places."
return round(x, -int(floor(log10(abs(x)))) + sd - 1)
def round5(x, sd):
"Returns x rounded to sd significant places, ending in 5 and 0."
return round(x * 2, -int(floor(log10(abs(x)))) + sd - 1) / 2
inputData = [-0.1, 0.21, 0.29, 4435.0, 9157, 9858.0, 10758.0, 11490.0, 12111.9]
roundedDict = {}
roundedData = []
for input in inputData:
if input in roundedDict:
# The input is already calculated.
roundedData.append(roundedDict[input])
continue
# Now we attempt to round it
success = False
places = 1
while not success:
rounded = roundSD(input, places)
if rounded in roundedDict.values():
# The value already appeared! We use better precision
places += 1
else:
# We rounded to the correct precision!
roundedDict[input] = rounded
roundedData.append(rounded)
success = True
这将保证如果两个数字相同,它们将给出相同的四舍五入输出。如果两个数字不同,它们将永远不会给出相同的输出。
A 运行 从上面给出:
[-0.1, 0.2, 0.3, 4000.0, 9000.0, 10000.0, 11000.0, 11500.0, 12000.0]
随意将 round 函数更改为您自己的,以便仅将 round 合并到 5 和 10。
我想要 "round" 一个有序的数值列表,可以是(正/负)浮点数或整数形式。我不希望输出中有相同的值,除非输入值本身相同。理想情况下,我希望四舍五入到最接近的 5 或 10,以尽可能高的幅度执行,然后向下舍入,直到相邻值之间不匹配。
以下是我正在寻找的一些示例:
[-0.1, 0.21, 0.29, 4435.0, 9157, 9858.0, 10758.0, 11490.0, 12111.9]
结果:
[-0.1, 0.0, 0.25, 5000.0, 9000.0, 10000.0, 11000.0, 11500.0, 12000.0]
这是我目前的情况:
def rounder(n, base=1):
base = base * (10 ** (len(str(abs(n))) - len(str(abs(n)))))
return base * round(float(n)/base)
for i in range(len(inp_values)-1):
while True:
a = rounder(inp_values[i], 10**((len(str(abs(int(inp_values[i])))))-(i+1)) / 2)
b = rounder(inp_values[i+1], 10**((len(str(abs(int(inp_values[i+1])))))-(i+1)) / 2)
print a, b
if a < b:
break
如有任何帮助,我们将不胜感激。
如果您保留一个数字字典(在 round = key 之前,round = value 之后),并编写一个 for 循环,如果舍入值在字典中发生冲突,则使用较低的精度进行舍入?例如:
from math import log10, floor
def roundSD(x, sd):
"Returns x rounded to sd significant places."
return round(x, -int(floor(log10(abs(x)))) + sd - 1)
def round5(x, sd):
"Returns x rounded to sd significant places, ending in 5 and 0."
return round(x * 2, -int(floor(log10(abs(x)))) + sd - 1) / 2
inputData = [-0.1, 0.21, 0.29, 4435.0, 9157, 9858.0, 10758.0, 11490.0, 12111.9]
roundedDict = {}
roundedData = []
for input in inputData:
if input in roundedDict:
# The input is already calculated.
roundedData.append(roundedDict[input])
continue
# Now we attempt to round it
success = False
places = 1
while not success:
rounded = roundSD(input, places)
if rounded in roundedDict.values():
# The value already appeared! We use better precision
places += 1
else:
# We rounded to the correct precision!
roundedDict[input] = rounded
roundedData.append(rounded)
success = True
这将保证如果两个数字相同,它们将给出相同的四舍五入输出。如果两个数字不同,它们将永远不会给出相同的输出。
A 运行 从上面给出:
[-0.1, 0.2, 0.3, 4000.0, 9000.0, 10000.0, 11000.0, 11500.0, 12000.0]
随意将 round 函数更改为您自己的,以便仅将 round 合并到 5 和 10。