开发者

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

开发者 https://www.devze.com 2023-03-07 19:18 出处:网络
How can arrange a set of names so that each solution has two names.for example [\"bob\", \"sally\", \"jane\"]

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

精彩评论

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

关注公众号