简体   繁体   中英

Convert Set of Tuples to a List of Lists | Python

Generating unique combinations by using set() over itertools .

Outputs a set of tuples, each originating from their invocations respectively.

Instead, I now want to convert this result into a list.

import itertools

my_list = ['A', 'B', 'C']
pairs = set(itertools.combinations(my_list, 2))
print(pairs)
>>> {('A', 'C'), ('B', 'C'), ('A', 'B')}

Instead, I would like:

[['A', 'C'], ['B', 'C'], ['A', 'B']]
a =[[item for item in pair] for pair in pairs]
print(a)

returns

[['B', 'C'], ['A', 'C'], ['A', 'B']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM