RNCR
RNCR

Reputation: 3

Python: how to combine elements in a 2d array?

I have this list

[[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571], ...]

i want it to be like this:

[[0, 374,676,844], [1, 186, 440],[2, 202], [3, 198, 571], ...]

I tried this code:

for i in range(len(index)-1):
    if (index[i][0]==index[i+1][0]):
        index[i].append(index[i+1][1])
        del index[i+1]
        i=i-1

But it dosen't work

Upvotes: 0

Views: 513

Answers (6)

Amit Pathak
Amit Pathak

Reputation: 1377

Without importing you can do in the following way -

new_dict = {}
result = []

for item in nest:
    if item[0] in new_dict.keys():
        new_dict[item[0]].append(item[1])
    else:
        new_dict[item[0]] = [item[1]]

for key, value in new_dict.items():
    result.append([key] + value)

Upvotes: 0

Liju
Liju

Reputation: 2303

You can use dictionaries to collect the numbers from index 1 from input list.

a=[[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]
b={}

[b[i[0]].append(i[1]) if i[0] in b else b.update({i[0]:[i[0],i[1]]}) for i in a]
b=list(b.values())

print(b)

Output

[[0, 185, 374, 676, 844], [1, 186, 440], [2, 202], [3, 198, 571]]

Upvotes: 0

Ashish Gupta
Ashish Gupta

Reputation: 392

Most simple way to implement. Just make sure before using the function you have sorted list by first index of sublist.

a = [[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]
    
    n = len(a)
    x = 0
    res = []
    r = [0]
    for i in a:
        if i[0] == x:
            r.append(i[1])
        else:
            x += 1
            res.append(r)
            r = [x, i[1]]
    res.append(r)
    print(res)

Upvotes: 0

Sivaram Rasathurai
Sivaram Rasathurai

Reputation: 6343

Use collections OrderedDict

 from collections import OrderedDict

l = [[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]
d = OrderedDict()
for inner in l:
    if inner[0] in d:
        d[inner[0]].append(inner[1])
    else:
        d[inner[0]] = [inner[1]]

print(d.values())

Upvotes: 0

Sushil
Sushil

Reputation: 5531

This is a simpler solution:

lst = [[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]
newlst = []
curr_lst = []
max_index = 3

for x in range(max_index+1):
    
    curr_lst = [x]
    
    for y in lst:
        if y[0] == x:
            curr_lst.append(y[1])
    newlst.append(curr_lst)
    
print(newlst)

Output:

[[0, 185, 374, 676, 844], [1, 186, 440], [2, 202], [3, 198, 571]]

Upvotes: 0

Mark
Mark

Reputation: 92440

Generally speaking, it is not advisable to delete items from the list you are iterating over. In Python it's more common (and easier) to generate a new list with comprehension.

You can make groups with itertools.groupby and group by the the first item in the list. Then from those groups you can create the desired sub-groups by taking the key plus all the other second elements. Something like:

from itertools import groupby

l = [[0, 185], [0, 374], [0, 676], [0, 844], [1, 186], [1, 440], [2, 202], [3, 198], [3, 571]]

[[k] + [i[1] for i in g] for k, g in groupby(l, key=lambda x: x[0])]
# [[0, 185, 374, 676, 844], [1, 186, 440], [2, 202], [3, 198, 571]]

Upvotes: 2

Related Questions