如何在Python中使用itertools生成关键字列表的所有可能变体,包括leet code?

huangapple go评论67阅读模式
英文:

How do I use itertools in Python to generate all possible variants of a list of keywords with leet code?

问题

import itertools

def leet(word):
    leet_matches = [['a', '@', '4', '∆', 'Д', 'а', 'а', 'a', 'à'],
    ['b', '8', 'b', 'ḃ', 'ḅ', 'ḇ'],
    ['c', '<', '{', '[', '(', '©'],
    ['d', 'd', 'ď', 'ḋ', 'ḍ', 'ḏ', 'ḑ', 'ḓ'],
    ['e', '3', '£', '₤', '€', 'е'],
    ['f', '7', 'ƒ', 'ḟ'],
    ['g', '9', '[', '-', '6', 'ĝ', 'ğ', 'ġ', 'ģ', 'ǧ', 'ǵ', 'ḡ'],
    ['h', '4', '#', 'ĥ', 'ȟ', 'ḣ', 'ḥ', 'ḧ', 'ḩ', 'ḫ', 'ẖ'],
    ['i', '1', '|', '!', 'ì', 'í'],
    ['j', '√', 'ĵ', 'ǰ'],
    ['k', 'ķ', 'ǩ', 'ḱ', 'ḳ', 'ḵ', 'ķ', 'ǩ', 'ḱ', 'ḳ', 'ḵ'],
    ['l', '1', '|', 'ĺ', 'ļ', 'ľ', 'ḷ', 'ḹ', 'ḻ', 'ḽ'],
    ['m', 'м', 'ḿ', 'ṁ', 'ṃ'],
    ['n', 'И', 'и', 'п', 'ñ', 'ń', 'ņ', 'ň', 'ǹ', 'ṅ', 'ṇ', 'ṉ', 'ṋ'],
    ['o', '0', 'Ø', 'Θ', 'о', 'ө', 'ò', 'ó', 'ô', 'õ', 'ö', 'ō', 'ŏ', 'ő', 'ơ', 'ǒ', 'ǫ', 'ǭ'],
    ['p', 'р', 'ṕ', 'ṗ'],
    ['q', '9', '(', ')', '0'],
    ['r', 'Я', '®', 'ŕ', 'ŗ', 'ř', 'ȑ', 'ȓ', 'ṙ', 'ṛ', 'ṝ', 'ṟ'],
    ['s', '5', '$', '§', 'ś', 'ŝ', 'ş', 'š', 'ș', 'ṡ', 'ṣ', 'ṥ', 'ṧ', 'ṩ'],
    ['t', '7', '+', 'т', 'ţ', 'ť', 'ț', 'ṫ', 'ṭ', 'ṯ', 'ṱ', 'ẗ'],
    ['u', 'ù', 'ú', 'û', 'ü', 'ũ', 'ū', 'ŭ', 'ů', 'ű', 'ų', 'ư', 'ǔ', 'ǖ', 'ǘ'],
    ['v'],
    ['w', 'Ш', 'ŵ', 'ẁ', 'ẃ', 'ẅ', 'ẇ', 'ẉ', 'ẘ'],
    ['x', '×', '%', '*', 'Ж', 'ẋ', 'ẍ'],
    ['y', '¥', 'Ч', 'ү', 'у', 'ṽ'],
    ['z', '5', 'ź', 'ż', 'ž', 'ẑ']]
    l = []
    for letter in word:
        for match in leet_matches:
            if match[0] == letter:
                l.append(match)
    return list(itertools.product(*l))

word = "hola"
test_list = leet(word)

def remove(string):
    return string.replace(" ", "")

res = [''.join(tups) for tups in test_list]
print(str(res) + remove(str(res)))


import csv
with open('leet_latinalphabet.csv', mode='w') as csvfile:
    fieldnames = ['leet variants']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerow({"leet variants": str(res)[1:-1].replace("'", "")})
英文:

Python - How to make current script iterate through list of words instead of one string/word only?

I am very new to python, and have put together a script parsing different scripts i've looked at.

The goal is to return all possible variants of a list of keywords, replacing the characters by leet code (e.g.: 'L33T' or 'l337' instead of 'Leet')

I have been able to achieve this for one string/word only, but I wish to be able to input a list of keywords and obtain the same results.

This is my first time using Stack overflow, and I would really appreciate any help you can provide me 如何在Python中使用itertools生成关键字列表的所有可能变体,包括leet code?

Here is my code:

import itertools
def leet(word):
leet_matches = [[&#39;a&#39;,&#39;@&#39;,&#39;4&#39;,&#39;∆&#39;,&#39;Д&#39;,&#39;а&#39;,&#39;а&#39;,&#39;a&#39;,&#39;&#224;&#39;],
[&#39;b&#39;,&#39;8&#39;,&#39;b&#39;,&#39;ḃ&#39;,&#39;ḅ&#39;,&#39;ḇ&#39;],
[&#39;c&#39;,&#39;&lt;&#39;,&#39;{&#39;,&#39;[&#39;,&#39;(&#39;,&#39;&#169;&#39;],
[&#39;d&#39;,&#39;d&#39;,&#39;ď&#39;,&#39;ḋ&#39;,&#39;ḍ&#39;,&#39;ḏ&#39;,&#39;ḑ&#39;,&#39;ḓ&#39;],
[&#39;e&#39;,&#39;3&#39;,&#39;&#163;&#39;,&#39;₤&#39;,&#39;€&#39;,&#39;е&#39;],
[&#39;f&#39;,&#39;7&#39;,&#39;ƒ&#39;,&#39;ḟ&#39;],
[&#39;g&#39;,&#39;9&#39;,&#39;[&#39;,&#39;-&#39;,&#39;6&#39;,&#39;ĝ&#39;,&#39;ğ&#39;,&#39;ġ&#39;,&#39;ģ&#39;,&#39;ǧ&#39;,&#39;ǵ&#39;,&#39;ḡ&#39;],
[&#39;h&#39;,&#39;4&#39;,&#39;#&#39;,&#39;ĥ&#39;,&#39;ȟ&#39;,&#39;ḣ&#39;,&#39;ḥ&#39;,&#39;ḧ&#39;,&#39;ḩ&#39;,&#39;ḫ&#39;,&#39;ẖ&#39;],
[&#39;i&#39;,&#39;1&#39;,&#39;|&#39;,&#39;!&#39;,&#39;&#236;&#39;,&#39;&#237;&#39;],
[&#39;j&#39;,&#39;√&#39;,&#39;ĵ&#39;,&#39;ǰ&#39;],
[&#39;k&#39;,&#39;ķ&#39;,&#39;ǩ&#39;,&#39;ḱ&#39;,&#39;ḳ&#39;,&#39;ḵ&#39;,&#39;ķ&#39;,&#39;ǩ&#39;,&#39;ḱ&#39;,&#39;ḳ&#39;,&#39;ḵ&#39;],
[&#39;l&#39;,&#39;1&#39;,&#39;|&#39;,&#39;ĺ&#39;,&#39;ļ&#39;,&#39;ľ&#39;,&#39;ḷ&#39;,&#39;ḹ&#39;,&#39;ḻ&#39;,&#39;ḽ&#39;],
[&#39;m&#39;,&#39;м&#39;,&#39;ḿ&#39;,&#39;ṁ&#39;,&#39;ṃ&#39;],
[&#39;n&#39;,&#39;И&#39;,&#39;и&#39;,&#39;п&#39;,&#39;&#241;&#39;,&#39;ń&#39;,&#39;ņ&#39;,&#39;ň&#39;,&#39;ǹ&#39;,&#39;ṅ&#39;,&#39;ṇ&#39;,&#39;ṉ&#39;,&#39;ṋ&#39;],
[&#39;o&#39;,&#39;0&#39;,&#39;&#216;&#39;,&#39;Θ&#39;,&#39;о&#39;,&#39;ө&#39;,&#39;&#242;&#39;,&#39;&#243;&#39;,&#39;&#244;&#39;,&#39;&#245;&#39;,&#39;&#246;&#39;,&#39;ō&#39;,&#39;ŏ&#39;,&#39;ő&#39;,&#39;ơ&#39;,&#39;ǒ&#39;,&#39;ǫ&#39;,&#39;ǭ&#39;],
[&#39;p&#39;,&#39;р&#39;,&#39;ṕ&#39;,&#39;ṗ&#39;],
[&#39;q&#39;,&#39;9&#39;,&#39;(&#39;,&#39;)&#39;,&#39;0&#39;],
[&#39;r&#39;,&#39;Я&#39;,&#39;&#174;&#39;,&#39;ŕ&#39;,&#39;ŗ&#39;,&#39;ř&#39;,&#39;ȑ&#39;,&#39;ȓ&#39;,&#39;ṙ&#39;,&#39;ṛ&#39;,&#39;ṝ&#39;,&#39;ṟ&#39;],
[&#39;s&#39;,&#39;5&#39;,&#39;$&#39;,&#39;&#167;&#39;,&#39;ś&#39;,&#39;ŝ&#39;,&#39;ş&#39;,&#39;š&#39;,&#39;ș&#39;,&#39;ṡ&#39;,&#39;ṣ&#39;,&#39;ṥ&#39;,&#39;ṧ&#39;,&#39;ṩ&#39;],
[&#39;t&#39;,&#39;7&#39;,&#39;+&#39;,&#39;т&#39;,&#39;ţ&#39;,&#39;ť&#39;,&#39;ț&#39;,&#39;ṫ&#39;,&#39;ṭ&#39;,&#39;ṯ&#39;,&#39;ṱ&#39;,&#39;ẗ&#39;],
[&#39;u&#39;,&#39;&#249;&#39;,&#39;&#250;&#39;,&#39;&#251;&#39;,&#39;&#252;&#39;,&#39;ũ&#39;,&#39;ū&#39;,&#39;ŭ&#39;,&#39;ů&#39;,&#39;ű&#39;,&#39;ų&#39;,&#39;ư&#39;,&#39;ǔ&#39;,&#39;ǖ&#39;,&#39;ǘ&#39;],
[&#39;v&#39;],
[&#39;w&#39;,&#39;Ш&#39;,&#39;ŵ&#39;,&#39;ẁ&#39;,&#39;ẃ&#39;,&#39;ẅ&#39;,&#39;ẇ&#39;,&#39;ẉ&#39;,&#39;ẘ&#39;],
[&#39;x&#39;,&#39;&#215;&#39;,&#39;%&#39;,&#39;*&#39;,&#39;Ж&#39;,&#39;ẋ&#39;,&#39;ẍ&#39;],
[&#39;y&#39;,&#39;&#165;&#39;,&#39;Ч&#39;,&#39;ү&#39;,&#39;у&#39;,&#39;ṽ&#39;],
[&#39;z&#39;,&#39;5&#39;,&#39;ź&#39;,&#39;ż&#39;,&#39;ž&#39;,&#39;ẑ&#39;]]
l = []
for letter in word:
for match in leet_matches:
if match[0] == letter:
l.append(match)
return list(itertools.product(*l))
word = &quot;hola&quot;
test_list = leet(word)
def remove(string):
return string.replace(&quot; &quot;, &quot;&quot;)
res = [&#39;&#39;.join(tups) for tups in test_list]
print (str(res)+remove(str(res)))
import csv
with open (&#39;leet_latinalphabet.csv&#39;, mode =&#39;w&#39;) as csvfile:
fieldnames = [&#39;leet variants&#39;]
writer = csv.DictWriter(csvfile,fieldnames=fieldnames)
writer.writeheader()
writer.writerow({&quot;leet variants&quot;:str(res)[1:-1].replace(&quot;&#39;&quot;,&quot;&quot;)})

答案1

得分: 1

循环遍历单词列表,在每个单词上调用leet()

words = ['hola', 'some', 'other', 'word']

with open('leet_latinalphabet.csv', mode='w') as csvfile:
    fieldnames = ['word', 'leet variants']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    for word in words:
        row = {"word": word, "leet variants": ",".join(leet(word))}
        writer.writerow(row)
英文:

Loop over the list of words, calling leet() on each word.

words = [&#39;hola&#39;, &#39;some&#39;, &#39;other&#39;, &#39;word&#39;]
with open (&#39;leet_latinalphabet.csv&#39;, mode =&#39;w&#39;) as csvfile:
fieldnames = [&#39;word&#39;, &#39;leet variants&#39;]
writer = csv.DictWriter(csvfile,fieldnames=fieldnames)
writer.writeheader()
for word in words:
row = {&quot;word&quot;: word, &quot;leet variants&quot;: &quot;,&quot;.join(leet(word))}
writer.writerow(row)

huangapple
  • 本文由 发表于 2023年6月2日 03:18:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定