算法问题 [英] Problem with algorithm
问题描述
大家好。
我想创建一个大型列表:
aaaa~zzzz
有没有什么好的算法呢?
Thanx
Jia Lu
4月12日,10:16,下午,贾璐 < Roka ... @ gmail.comwrote:
大家好。
*我想创建一个大型列表,如:
aaaa~zzzz
有没有什么好的算法可以做到这一点?
当然。
test =''01''
for test in:
for n in test:
for o in test:
for p in test:
print m + n + o + p
## 0000
## 0001
## 0010
## 0011
## 0100
## 0101
## 0110
## 0111
## 1000
## 1001
## 1010
## 1011
## 1100
## 1101
## 1110
## 1111
现在只需改变test ='' 01''to test =''abcdefghijklmnopqrstuvwxyz''。
>
Thanx
>
Jia Lu
me ******** @ aol.com 写道:
4月12日,10:16 ??? pm," ;贾璐 < Roka ... @ gmail.comwrote:
>大家好。
???我想创建一个大型列表,如:
aaaa~zzzz
有没有什么好的算法可以做到这一点?
当然。
test =''01''
for test in:
for n in test:
for o in test:
for p in test:
print m + n + o + p
[snip]
原谅我犯过的任何愚蠢的错误(我一直在教
我自己的python大约1周)但是有一个适度的
众所周知的算法,可以扩展到列表的任意长度和/或
长度替代品和长度
所需的输出,并避免深层嵌套循环。
我知道对于小而恒定的输出来说并不是更好
长度,但对于更长的长度或如果输出长度
可以变化,它应该更好。有一个类似的算法
如果不允许重复(即abcd ... wxyz)。
我尝试python翻译算法:
def m_from_n(v,m):
"""
从v打印所有m个东西的组合0] ... v [n-1],
重复OK。产生一个清单。
"""
x = [0] * m
而True:
产生[v [i] for i in x]
i = m - 1
而i> = 0且x [i] == len(v)-1 :
x [i] = 0
i = i - 1
if i> = 0:
x [ i] = x [i] + 1
否则:
返回
$ m $ b for y in m_from_n(" xyz", 2):
print''''。join(y)
xx
xy
xz
yx
yy
yz
zx
zy >
zz
$ m $ b for y in m_from_n([0,1],3):
print y
[0,0,0]
[0,0,1]
[0,1,0]
[ 0,1,1]
[1,0,0]
[1,0,1]
[1,1,0] ]
[1,1,1]
$ m $ b for y in m_from_n(" abcdefghijklmnopqrstuvwxyz",4):
打印''''。join(y)
应该或多或少地做你想做的事。
Charles
Charles Sanders< C。******************* @ BoM.GOv.AUwrites:
< blockquote class =post_quotes>
原谅我所犯过的任何愚蠢的错误(我已经教了大约1周的自我蟒蛇b / b
)但是有一个适度的
众所周知的算法,扩展到备选列表和所需输出的长度
的任意长度,并避免深度嵌套循环。
s =" abcd"
def a(n):
如果n = = 0:
收益''''
返回
for c in s:
for r in a (n-1):
收益率c + r
打印列表(a(3))
Hi all.
I want to create a large list like:
aaaa ~ zzzz
Is there any good algorithm to do this?
Thanx
Jia Lu
On Apr 12, 10:16???pm, "Jia Lu" <Roka...@gmail.comwrote:Hi all.
*I want to create a large list like:
aaaa ~ zzzz
Is there any good algorithm to do this?Sure.
test = ''01''
for m in test:
for n in test:
for o in test:
for p in test:
print m+n+o+p
## 0000
## 0001
## 0010
## 0011
## 0100
## 0101
## 0110
## 0111
## 1000
## 1001
## 1010
## 1011
## 1100
## 1101
## 1110
## 1111
Now just change test=''01'' to test=''abcdefghijklmnopqrstuvwxyz''.
>
Thanx
Jia Lu
me********@aol.com wrote:On Apr 12, 10:16???pm, "Jia Lu" <Roka...@gmail.comwrote:>Hi all.
???I want to create a large list like:
aaaa ~ zzzz
Is there any good algorithm to do this?
Sure.
test = ''01''
for m in test:
for n in test:
for o in test:
for p in test:
print m+n+o+p[snip]
Forgive any silly mistakes I have made (I''ve been teaching
myself python for about 1 week) but there is a moderately
well known algorithm for this that extends to arbitrary
lengths of both the list of alternatives and the length
of the required output, and avoids deeply nested loops.
I know that it is no better for small and constant output
lengths, but for longer lengths or if the output length
can vary it should be better. There is a similar algorithm
if duplicates are not allowed (ie abcd ... wxyz).
My attempt at a python translation of the algorithm:
def m_from_n ( v, m ):
"""
Print all combinations of m things from v[0] ... v[n-1],
duplicates OK. Yields a list.
"""
x = [0] * m
while True:
yield [ v[i] for i in x ]
i = m - 1
while i>=0 and x[i]==len(v)-1:
x[i] = 0
i = i - 1
if i >= 0:
x[i] = x[i] + 1
else:
return
for y in m_from_n( "xyz", 2 ):
print ''''.join(y)
xx
xy
xz
yx
yy
yz
zx
zy
zz
for y in m_from_n( [0,1], 3 ):
print y
[0, 0, 0]
[0, 0, 1]
[0, 1, 0]
[0, 1, 1]
[1, 0, 0]
[1, 0, 1]
[1, 1, 0]
[1, 1, 1]
for y in m_from_n( "abcdefghijklmnopqrstuvwxyz", 4 ):
print ''''.join(y)
should more or less do what you want.
Charles
Charles Sanders <C.*******************@BoM.GOv.AUwrites:Forgive any silly mistakes I have made (I''ve been teaching
myself python for about 1 week) but there is a moderately
well known algorithm for this that extends to arbitrary
lengths of both the list of alternatives and the length
of the required output, and avoids deeply nested loops.s = "abcd"
def a(n):
if n==0:
yield ''''
return
for c in s:
for r in a(n-1):
yield c+r
print list(a(3))
这篇关于算法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!