为什么python不会产生所有可能的字母组合?
使用代码代码生成文件
将以可能的组合返回两个文件!
最终文件就是使用!
使用Itertools生成的字母可能组合
将元组连接到字符串
映射输出到字符串将
输出写入文件,
读取生成的文件并删除未没有的空间,
最后测试
文件:test..pypypy
#using itertools generating possible combinations of letters given below and writing it to a file
from itertools import combinations
a = "lida"
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',]
def Combinations(iterable, size):
result = combinations(iterable, size)
#joining tuple to string
def join_tuple_string(strings_tuple) -> str:
return ' '.join(strings_tuple)
#maping output to string
output = map(join_tuple_string, result)
end = list(output)
#writing the output to a file
with open('file.txt', 'w') as e:
e.write(str(end))
Combinations(letters, 4)
#Reading the generated file and removing uncessary spaces
with open('file.txt', 'r') as e:
a = e.read()
for i in range(len(a)):
list = a[i]
b = list.replace(' ', '')
with open('final.txt', 'a') as f:
f.write(b)
# finally Testing
with open('final.txt', 'r') as r:
file = r.read()
if 'adil' in file:
print('present')
if 'lida' in file:
print('present')
else:
print('not present')
Generate the file's using code
code will return two file with possible combinations!
final file is that to use!
using itertools generating possible combinations of letters
joining tuple to string
maping output to string
writing the output to a file
Reading the generated file and removing uncessary spaces
finally Testing
File : test.py
#using itertools generating possible combinations of letters given below and writing it to a file
from itertools import combinations
a = "lida"
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',]
def Combinations(iterable, size):
result = combinations(iterable, size)
#joining tuple to string
def join_tuple_string(strings_tuple) -> str:
return ' '.join(strings_tuple)
#maping output to string
output = map(join_tuple_string, result)
end = list(output)
#writing the output to a file
with open('file.txt', 'w') as e:
e.write(str(end))
Combinations(letters, 4)
#Reading the generated file and removing uncessary spaces
with open('file.txt', 'r') as e:
a = e.read()
for i in range(len(a)):
list = a[i]
b = list.replace(' ', '')
with open('final.txt', 'a') as f:
f.write(b)
# finally Testing
with open('final.txt', 'r') as r:
file = r.read()
if 'adil' in file:
print('present')
if 'lida' in file:
print('present')
else:
print('not present')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的问题是“为什么
'lida'
在文件数据中找不到'adil'
是?”答案是:因为您在任务中使用了错误的itertools
函数(和/或误解了它的工作)。组合
根据输入迭代中的位置按顺序产生所有 unique 子序列。由于您的输入峰值按顺序排序,因此输出的元素也始终以排序顺序;'abcd'
将存在,但是'abdc'
不会,因为d
是在输入中c
之后(a
,b
,c
和d
b no订购“ ABCD” 将存在)。如果要包含所有各种排列(So
'adil'
和'lida'
都出现在输出中),您需要itertools.permutationss.permutations < /code>,而不是
代码>组合,或itertools.combinations
。同样,如果您需要一个字母才能重复(So'aaaa'
是可能的输出),则需要comminations_with_replacement
,如果仅按照<<<product
(使用重复
通过关键字传递的参数)如果您希望按照permututations
。不过请警告,对于
排列
,输出的数量 ;我强烈建议不要尝试将它们全部存储在记忆中。只需在排列上循环
对象和写
一个一个一个,例如:它仍然会在文件中产生法律
list
littleal,并避免添加添加您首先要避免的任何空间中的任何一个,都不会吹木,试图一次固定所有排列。Assuming your question is "Why is
'lida'
not found in the file data while'adil'
is?" the answer is: Because you used the wrongitertools
function for your task (and/or misunderstood what it does).combinations
produces all unique subsequences, in order by position in the input iterable. Since your input iterable is in sorted order, your output's elements will always be in sorted order too;'abcd'
will exist, but'abdc'
won't, becaused
comes afterc
in the input (no ordering ofa
,b
,c
andd
except'abcd'
will exist). If you want to include all the various permutations (so'adil'
and'lida'
both appear in the output), you'd wantitertools.permutations
, notitertools.combinations
. Similarly, if you need a letter to be able to repeat (so'aaaa'
is a possible output), you'd wantcombinations_with_replacement
if only unique outputs desired as percombinations
, orproduct
(with arepeat
argument passed by keyword) if you want all orderings as perpermutations
.Be warned though, the number of outputs gets much larger for
permutations
; I'd strongly recommend not trying to store them all in memory. Just loop over thepermutations
object andwrite
them one by one, e.g.:which still produces a legal
list
literal in the file, and avoids adding any of those spaces you're trying to avoid in the first place, all without blowing your RAM trying to hold all the permutations at once.