0

我正在尝试计算每个值从 1-c1 到 4 的百分比变化bench。每个的值benchpythonic list类似于lists = [[],[],[],[],[],[]]

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]
]

这就是列表的样子

Paulo,作为对您的评论的回应,我希望 % 从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]

任何其他更改将不胜感激。谢谢你。

4

2 回答 2

2

正如评论中所说,有示例输出会很有帮助,但是由于您似乎只想要一个工具,然后自己实现它,我认为您正在寻找的是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

grouper 函数是itertools 文档中的一个秘诀

于 2013-08-05T18:25:47.840 回答
0

我有点不清楚输出应该是什么,但是如果你想比较and的1-c1值,那么你可以为列表中的每一列比较 and 的值,等等:ab1-c1bc

# 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)
于 2013-08-05T18:07:15.093 回答