python 2.5.2 中的排列
我有一个用于输入的数字列表,例如
671.00
1,636.00
436.00
9,224.00
,我想用一种方法来生成所有可能的总和以用于输出,例如:
671.00 + 1,636.00 = 2,307.00
671.00 + 436.00 = 1,107.00
671.00 + 9,224.00 = 9,224.00
671.00 + 1,636.00 + 436.00 = 2,743.00
...
并且我想在 Python 中执行此操作 我目前的限制是: a)我现在刚刚学习Python(这是想法的一部分) b)我将不得不使用Python 2.5.2(没有intertools)
我想我已经找到了一段可能有帮助的代码:(
def all_perms(str):
if len(str) <=1:
yield str
else:
for perm in all_perms(str[1:]):
for i in range(len(perm)+1):
#nb str[0:1] works in both string and list contexts
yield perm[:i] + str[0:1] + perm[i:]
来自这些人)
但我不确定如何在我的提案中使用它。 有人可以提供一些提示和帮助代码吗?
谢尔斯、
F.
I have a list of numbers for input, e.g.
671.00
1,636.00
436.00
9,224.00
and I want to generate all possible sums with a way to id it for output, e.g.:
671.00 + 1,636.00 = 2,307.00
671.00 + 436.00 = 1,107.00
671.00 + 9,224.00 = 9,224.00
671.00 + 1,636.00 + 436.00 = 2,743.00
...
and I would like to do it in Python
My current constrains are:
a) I'm just learning python now (that's part of the idea)
b) I will have to use Python 2.5.2 (no intertools)
I think I have found a piece of code that may help:
def all_perms(str):
if len(str) <=1:
yield str
else:
for perm in all_perms(str[1:]):
for i in range(len(perm)+1):
#nb str[0:1] works in both string and list contexts
yield perm[:i] + str[0:1] + perm[i:]
( from these guys )
But I'm not sure how to use it in my propose.
Could someone trow some tips and pieces of code of help?
cheers,
f.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
排列是指获取一组有序的事物并移动这些事物(即改变顺序)。您的问题是关于列表中的内容的组合。
现在,枚举组合的一种简单方法是将列表中的条目映射到数字中的位。例如,假设如果设置了位#0(即1),则数字
lst[0]
参与组合,如果设置了位#1,则lst[1]参与组合
参与组合等。这样,范围0 <= n << 2**(len(lst))
识别lst
成员的所有可能组合,包括空成员 (n = 0
) 和整个lst
(n = 2**(len(lst)) - 1
)。您只需要 2 个或更多项的组合,即仅需要在二进制表示中至少有两个非零位的组合 ID。以下是如何识别这些:
下一步是提取由组合 id 标识的列表成员的组合。这很容易,这要归功于列表推导的强大功能:
现在让我们创建一个生成器来生成所有总和及其字符串表示形式:
最后,让我们使用它:
Permutations are about taking an ordered set of things and moving these things around (i.e. changing order). Your question is about combinations of things from your list.
Now, an easy way of enumerating combinations is by mapping entries from your list to bits in a number. For example, lets assume that if bit #0 is set (i.e. 1), then number
lst[0]
participates in the combination, if bit #1 is set, thenlst[1]
participates in the combination, etc. This way, numbers in range0 <= n < 2**(len(lst))
identify all possible combinations oflst
members, including an empty one (n = 0
) and the wholelst
(n = 2**(len(lst)) - 1
).You need only combinations of 2 items or more, i.e. only those combination IDs that have at least two nonzero bits in their binary representation. Here is how to identify these:
Next step is to extract a combination of list members identified by a combination id. This is easy, thanks to the power of list comprehensions:
Now let's make a generator that produces all sums, together with their string representations:
And, finally, let's use it:
下面的代码生成给定列表的所有“子集”(空集除外),即它返回列表的列表。
现在您也可以编写一个简短的函数
doit
来进行输出:The code below generates all "subsets" of a given list (except the empty set), i.e. it returns a list of lists.
Now you could just write a short function
doit
for output also:使用 itertools (Python >=2.6) 将是:
With itertools (Python >=2.6) would be: