我试图计算每个bench的值从1-c1到4的百分比变化。每个bench的值都在类似于lists = [[],[],[],[],[],[]]的pythonic list中。
在python中是否有一个库或任何统计包可以帮助我轻松地完成这一任务?
"bench" "1-c1", "1-c3", "1-c6", "1-c7", "1-poll", "1", "2-c1", "2-c3", "2-c6", "2-c7", "2-poll", "2", "3-c1", "3-c3", "3-c6", "3-c7", "3-poll", "3", "4-c1", "4-c3", "4-c6", "4-c7", "4-poll", "4"
a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
b 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
d 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
e 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
f 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24请给我。如果有什么不清楚的话请告诉我
lists = [
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
]这就是名单上的样子
保罗,作为对您的评论的回应,我希望从lists[0][0] to lists[n][n]中更改%
多亏了Paulo。我可以改进我的解决方案
listi = []
iter = []
percentage = [[],[],[],[],[]]
def increase_decrease(lists):
for data in range(0,len(lists)):
listi.append(lists[data][0])
for row in listi:
iterations = list(itertools.product(row,row))
iter.append(iterations)
for b in range(0, len(iter)):
for a in range(0, len(iter[0])):
if (iter[b][a][0] != iter[b][a][1]) and (iter[b][a][0] > iter[b][a][1]):
percentage[b].append(float(float(float(iter[b][a][0] - iter[b][a][1])/iter[b][a][0]))*100)
if (iter[b][a][0] != iter[b][a][1]) and (iter[b][a][0] < iter[b][a][1]):
percentage[b].append(float(float(float(iter[b][a][1] - iter[b][a][0])/iter[b][a][1]))*100)
print percentage[b]如有任何其他更改,将不胜感激。谢谢。
发布于 2013-08-05 18:25:47
正如注释中所说的,有示例输出会很有帮助,但是由于您似乎只需要一个工具,然后自己实现它,我认为您要寻找的是itertools.product,它将为您提供所需的所有排列:
>>> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> for row in a:
... print list(itertools.product(row, row))
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
[(4, 4), (4, 5), (4, 6), (5, 4), (5, 5), (5, 6), (6, 4), (6, 5), (6, 6)]
[(7, 7), (7, 8), (7, 9), (8, 7), (8, 8), (8, 9), (9, 7), (9, 8), (9, 9)]然后,您可以遍历itertools.product返回的每个生成器并计算百分比,可能跳过第二个值的索引等于或大于第一个生成器的百分比(例如,对于product返回的顶部行,您可能只需要(1,2),(1,3)和(2,3))。这样您就可以跳过多余的职位:
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
row = [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
grouped = grouper(3, row)
for i, row in enumerate(grouped):
for j, column in enumerate(row):
if i < j:
print column石斑鱼的函数是迭代工具文档中的菜谱。
发布于 2013-08-05 18:07:15
我有点不清楚输出到底应该是什么,但是如果您想比较a和b的1-c1值,那么列表中每个列的b和c的1-c1值等等,您可以这样做:
# NOTE: I'm using foo as input because it should have more interesting
# results than for your given list
foo = [[x for x in range(1, 11)],
[x for x in range(10, 0, -1)],
[x for x in range(1, 11)]]
ans = []
for comp, to in zip(foo[:-1], foo[1:]):
ans.append([])
for n in range(len(comp)):
ans[-1].append((100 * to[n]/float(comp[n])) - 100)存储在ans中的值是:
[[900.0, 350.0, 166.66666666666669, 75.0, 20.0, -16.66666666666667, -42.857142857142854, -62.5, -77.77777777777777, -90.0],
[-90.0, -77.77777777777777, -62.5, -42.857142857142854, -16.66666666666667, 20.0, 75.0, 166.66666666666669, 350.0, 900.0]]因此,ans中的第一个元素是一个列表,其中包含了foo中第一个和第二个列表中每个元素之间的百分比变化,而第二个列表对于foo中的第二个和第三个列表是相同的。
我希望这就是你要找的!
更新:--在做了一些其他的事情之后,我有了这样的想法:您想要将每个元素与整个矩阵中的其他元素进行比较。
import itertools
from string import ascii_lowercase
# I converted the list into a dictionary so that the output would
# have better names than: Comparing 101 to 131 etc.
# The principle is the same for lists though
foo = {'BENCH-1': dict((l, x) for l, x in zip(ascii_lowercase, range(100, 103))),
'BENCH-2': dict((l, x) for l, x in zip(ascii_lowercase, range(120, 117, -1))),
'BENCH-3': dict((l, x) for l, x in zip(ascii_lowercase, range(130, 133)))}
# Find all combinations of the benches
for (k1, v1), (k2, v2) in itertools.combinations(foo.iteritems(), 2):
# Find all pairs in each of the combinations of the benches.
# NOTE: the sorted is to help the readability of the output
# NOTE: ik1 = inner key 1, iv2 = inner value 2
for (ik1, iv1), (ik2, iv2) in sorted(itertools.product(v1.iteritems(), v2.iteritems())):
print 'Comparing %s to %s: %f' % (k1 + ik1, k2 + ik2, (100 * iv1 / float(iv2)) - 100)产出如下:
Comparing BENCH-2a to BENCH-3a: -7.69230769231
Comparing BENCH-2a to BENCH-3b: -8.39694656489
Comparing BENCH-2a to BENCH-3c: -9.09090909091
Comparing BENCH-2b to BENCH-3a: -8.46153846154
Comparing BENCH-2b to BENCH-3b: -9.16030534351
Comparing BENCH-2b to BENCH-3c: -9.84848484848
Comparing BENCH-2c to BENCH-3a: -9.23076923077
Comparing BENCH-2c to BENCH-3b: -9.92366412214
Comparing BENCH-2c to BENCH-3c: -10.6060606061
Comparing BENCH-2a to BENCH-1a: 20.0
Comparing BENCH-2a to BENCH-1b: 18.8118811881
Comparing BENCH-2a to BENCH-1c: 17.6470588235
Comparing BENCH-2b to BENCH-1a: 19.0
Comparing BENCH-2b to BENCH-1b: 17.8217821782
Comparing BENCH-2b to BENCH-1c: 16.6666666667
Comparing BENCH-2c to BENCH-1a: 18.0
Comparing BENCH-2c to BENCH-1b: 16.8316831683
Comparing BENCH-2c to BENCH-1c: 15.6862745098
Comparing BENCH-3a to BENCH-1a: 30.0
Comparing BENCH-3a to BENCH-1b: 28.7128712871
Comparing BENCH-3a to BENCH-1c: 27.4509803922
Comparing BENCH-3b to BENCH-1a: 31.0
Comparing BENCH-3b to BENCH-1b: 29.702970297
Comparing BENCH-3b to BENCH-1c: 28.431372549
Comparing BENCH-3c to BENCH-1a: 32.0
Comparing BENCH-3c to BENCH-1b: 30.6930693069
Comparing BENCH-3c to BENCH-1c: 29.4117647059若要使用列表作为输入执行此操作,请执行以下操作:
import itertools
for b1, b2 in itertools.combinations(foo, 2):
for v1, v2 in sorted(itertools.product(b1, b2)):
print (100 * v1 / float(v2)) - 100)https://stackoverflow.com/questions/18062982
复制相似问题