python - grouping list elements -
this question has answer here:
i want group every element in list other elements in list
for ex -
l1 = [1,2,3] l2 = [(1,2),(1,3),(2,3)]
i try using zip:
l2 = list(zip(l1,l1[1:]))
but gives me:
l2 = [(1, 2), (2, 3)]
desired output:
[(1,2),(1,3),(2,3)]
for
[1,2,3]
its itertools.combinations :
>>> l1 = [1,2,3] >>> itertools import combinations >>> list(combinations(l1,2)) [(1, 2), (1, 3), (2, 3)]
Comments
Post a Comment