为什么python不会产生所有可能的字母组合?

发布于 2025-01-21 09:38:44 字数 1541 浏览 1 评论 0原文

使用代码代码生成文件

将以可能的组合返回两个文件!

最终文件就是使用!

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的笑 2025-01-28 09:38:44

假设您的问题是“为什么'lida'在文件数据中找不到'adil'是?”答案是:因为您在任务中使用了错误的itertools函数(和/或误解了它的工作)。

组合根据输入迭代中的位置按顺序产生所有 unique 子序列。由于您的输入峰值按顺序排序,因此输出的元素也始终以排序顺序; 'abcd'将存在,但是'abdc'不会,因为d是在输入中c之后(abcd b no订购“ ABCD” 将存在)。如果要包含所有各种排列(So 'adil''lida'都出现在输出中),您需要itertools.permutationss.permutations < /code>,而不是itertools.combinations。同样,如果您需要一个字母才能重复(So 'aaaa'是可能的输出),则需要comminations_with_replacement,如果仅按照<<<代码>组合,或product(使用重复通过关键字传递的参数)如果您希望按照permututations

不过请警告,对于排列,输出的数量 ;我强烈建议不要尝试将它们全部存储在记忆中。只需在排列上循环对象和一个一个一个,例如:

with open('file.txt', 'w') as e:
   perms = map(''.join, permutations(iterable, size))   # map(''.join, ...) is an efficient way to change an iterable of tuples of str to single strs
   file.write(f'[{next(perms)!r}')  # Write open bracket and pull initial value
   # Iterate over remaining values
   for perm in perms:
       file.write(f',{perm!r}')  # Write comma and new value
   file.write(']')  # Write close bracket

它仍然会在文件中产生法律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 wrong itertools 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, because d comes after c in the input (no ordering of a, b, c and d except 'abcd' will exist). If you want to include all the various permutations (so 'adil' and 'lida' both appear in the output), you'd want itertools.permutations, not itertools.combinations. Similarly, if you need a letter to be able to repeat (so 'aaaa' is a possible output), you'd want combinations_with_replacement if only unique outputs desired as per combinations, or product (with a repeat argument passed by keyword) if you want all orderings as per permutations.

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 the permutations object and write them one by one, e.g.:

with open('file.txt', 'w') as e:
   perms = map(''.join, permutations(iterable, size))   # map(''.join, ...) is an efficient way to change an iterable of tuples of str to single strs
   file.write(f'[{next(perms)!r}')  # Write open bracket and pull initial value
   # Iterate over remaining values
   for perm in perms:
       file.write(f',{perm!r}')  # Write comma and new value
   file.write(']')  # Write close bracket

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文