得到一组"STRINGS"的可能组合.

编程入门 行业动态 更新时间:2024-10-11 15:21:20
本文介绍了得到一组"STRINGS"的可能组合. - PYTHON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为获得一组"字符"的可能组合,语法为:

For getting possible combinations of set of "characters", syntax would be:

>>>q=[''.join(p) for p in itertoolsbinations('ABC',2)] >>>q ['AB', 'AC', 'BC']

如何获得一组" STRINGS "的可能组合 例如:

What about getting possible combinations of set of "STRINGS" for example :

'A1X2','B1','C19'

'A1X2','B1','C19'

输出应为: ['A1X2B1', 'A1X2C19', 'B1C19']

output should be: ['A1X2B1', 'A1X2C19', 'B1C19']

推荐答案

只需传递strings即可. combinations而不是对"ABC"的每个字符进行迭代,而是对strings列表中包含的字符串进行迭代.并且join的行为相同(Python中的字符和字符串之间没有区别,字符只是大小为1的字符串)

just pass strings instead. Instead of iterating on each char of "ABC", combinations iterates on the strings contained in the strings list. And join acts the same (there is no difference between a character and a string in python, characters are just strings of size 1)

strings = ['A1X2','B1','C19'] q=[''.join(p) for p in itertoolsbinations(strings,2)]

结果:

['A1X2B1', 'A1X2C19', 'B1C19']

更多推荐

得到一组"STRINGS"的可能组合.

本文发布于:2023-11-30 08:17:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   quot   STRINGS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!