gpt4 book ai didi

Python - 排列和百分比

转载 作者:太空宇宙 更新时间:2023-11-04 09:44:19 25 4
gpt4 key购买 nike

我正在尝试为每个 bench 计算值从 1-c1 到 4 的百分比变化。每个 bench 的值都在 pythonic 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] 更改为 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]

如有任何其他更改,我们将不胜感激。谢谢。

最佳答案

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

石斑鱼函数是a recipe in the itertools documentation .

关于Python - 排列和百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18062982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com