开发者

arranging a set of names such that two names are printed as one solution

How can arrange a set of names so that each solution has two names.for example ["bob", "sally", "jane"]

开发者_运维问答

the outcome should be like; bob & sally, sally & jane etc, using python

Thanks in advance.


>>> import itertools
>>> li = ["bob", "sally", "jane"]
>>> for i in itertools.combinations(li, 2):
    print i

And you get:

('bob', 'sally')
('bob', 'jane')
('sally', 'jane')

Check out the docs for itertools, especially on combinations and permutations. There are good code examples there showing how it really works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜