0

Maybe it is supersimple, but I can't think of something simple and efficient.

I have, say, the list :

[[11, 2, 4],
 [11, 37, 1],
 [13, 2, 5],
 [13, 17, 1],
 [14, 11, 1],
 [14, 47, 1],
 [15, 2, 3],
 [15, 61, 1],
 [17, 2, 3],
 [17, 53, 1],
 [19, 2, 5],
 [19, 11, 1]]

and I'm looking for a function where outputs

[[11,[2,4],[37,1]],[13,[2,5],[17,1]], etc]
111
  • 133
  • 6

5 Answers5

2

Here are few solution's you can give it a try, using itertools.groupby

from itertools import groupby

print(
    [[key] + [i[1:] for i in value]
     for key, value in groupby(input_, key=lambda x: x[0])]
)

OR

from collections import defaultdict

group_by_ = defaultdict(list)

for i in input_:
    group_by_[i[0]].append(i[1:])

print(
    [[k] + v for k, v in group_by_.items()]
)

[[11, [2, 4], [37, 1]],
 [13, [2, 5], [17, 1]],
 [14, [11, 1], [47, 1]],
 [15, [2, 3], [61, 1]],
 [17, [2, 3], [53, 1]],
 [19, [2, 5], [11, 1]]]
sushanth
  • 8,275
  • 3
  • 17
  • 28
2

Example, say:

vector = [[1, 2, 3], [1, 5, 6], [7, 8, 9], [7,11,12]]

Don't think it gets any easier than that:

[[i[0], i[1:3], k[1:3]] for i, k in zip(vector[0::2], vector[1::2])]   

yielding [[1, [2, 3], [5, 6]], [7, [8, 9], [11, 12]]].

luizbarcelos
  • 686
  • 5
  • 17
2

You can use a dictionary and do like this.

l = [[11, 2, 4],
 [11, 37, 1],
 [13, 2, 5],
 [13, 17, 1],
 [14, 11, 1],
 [14, 47, 1],
 [15, 2, 3],
 [15, 61, 1],
 [17, 2, 3],
 [17, 53, 1],
 [19, 2, 5],
 [19, 11, 1]]
 
d = {}
for i in l:
    val = d.setdefault(i[0], [])
    val.append(i[1:])

ans = []
for i,v in d.items():
    ans.append([i,*v])
    
print(ans)
[[11, [2, 4], [37, 1]], [13, [2, 5], [17, 1]], [14, [11, 1], [47, 1]], [15, [2, 3], [61, 1]], [17, [2, 3], [53, 1]], [19, [2, 5], [11, 1]]]
Ram
  • 4,724
  • 2
  • 14
  • 22
  • 1
    You could smush that if-statement like this: `v = d.setdefault(i[0], []); v.append(i[1:])`. Or if that's too confusing, you could at least remove the `elif`: `if i[0] not in d: d[i[0]] = v = []; v.append(i[1:])` – wjandrea Aug 07 '21 at 03:23
2

Try using itertool's groupby method:

import itertools, operator

L = [[11, 2, 4],
 [11, 37, 1],
 [13, 2, 5],
 [13, 17, 1],
 [14, 11, 1],
 [14, 47, 1],
 [15, 2, 3],
 [15, 61, 1],
 [17, 2, 3],
 [17, 53, 1],
 [19, 2, 5],
 [19, 11, 1]]

key_func = operator.itemgetter(0)
for key, group in itertools.groupby(L, key_func):
    print([key] + [i[1:] for i in list(group)])
  • 1
    Beside the point, but [named lambdas are bad practice](/q/38381556/4518341). Put it inline instead: `itertools.groupby(L, lambda x: x[0])`. I would normally say to use a `def` instead, but this is only a one-off use, so no point. Although if you did want something reusable, I'd recommend [`itemgetter(0)`](https://docs.python.org/3/library/operator.html#operator.itemgetter). – wjandrea Aug 07 '21 at 03:17
  • 1
    @wjandrea Done! Thanks for the suggestion –  Aug 07 '21 at 03:23
1

one simple way is using dictionary

use first element of as key and another two be a value using list, like this

dic = {11: [[2,4],[37,1]], 13: [[2, 5],[17, 1]], ect}
  • when you make dictionary, if key is not in dictionary, make new list as value(ex, [[2,4]]), else append list

then you may use method keys() or items() to make this dictionary to list

mintropy
  • 11
  • 2
  • [Ram's answer](https://stackoverflow.com/a/68689105/4518341) has an implementation of this. BTW, welcome to Stack Overflow! Check out the [tour]. – wjandrea Aug 07 '21 at 03:29