我在python代码中遇到了一个相当棘手的问题。我环顾四周,没有发现有类似问题的人
我想生成字符串,将一些字符转换成几个不同的字符
我想用几个不同的字符来代替原来的字符。 我想做的是这样的:
text = "hi there"
translations = {"i":["b", "c"], "r":["e","f"]}
result = magicfunctionHere(text,translations)
print(result)
> [
"hb there",
"hc there",
"hi theee",
"hi thefe",
"hb theee",
"hb thefe",
"hc theee",
"hc thefe"
]
结果包含原始文本的任意组合,其中“i”和“r”分别替换为“b”和“c”,以及“e”和“f”
我不知道如何做到这一点,使用itertools和函数,如排列、乘积等
希望我说得够清楚,这是一个相当具体的问题! 谢谢你的帮助
一个简单的方法是分两步使用
Nested Loop Construct
(函数),如下所示:另见:https://eval.in/960803
另一种同样复杂的方法是使用单个函数和嵌套循环,如下所示:
请参阅:https://eval.in/961602
相关问题 更多 >
编程相关推荐