Transform "List of Tuples" into a Flat List or a Matrix

How to convert a list of tuples to a matrix?

This will maintain the order of column/row names and produce the requested solution:

from collections import OrderedDict

input = [('House', 'Dog', 7), ('House', 'Cat', 5), ('Garden', 'Dog', 4), ('Garden', 'Cat', 3), ('Park', 'Mouse', 2)]

column_names = list(OrderedDict.fromkeys(item[1] for item in input))
row_names = list(OrderedDict.fromkeys(item[0] for item in input))

result = [[''] * (len(column_names) + 1) for i in range(len(row_names) + 1)]

for i in range(len(column_names)):
result[0][i+1] = column_names[i]

for i in range(len(row_names)):
result[i+1][0] = row_names[i]

for item in input:
result[row_names.index(item[0]) + 1][column_names.index(item[1]) + 1] = item[2]

print(result)

How to convert a list of tuples to a matrix for column access?

Using operator:

In [250]: import operator

In [252]: f = lambda l, i: list(map(operator.itemgetter(i), l))

In [253]: f(tups, 0)
Out[253]: ['a', 'b']

In [254]: f(tups, 1)
Out[254]: [5, 8]

In [255]: f(tups, 2)
Out[255]: ['test', 'test1']

If you're using python2, drop the list(...).

How do I make a flat list out of a list of lists?

Given a list of lists l,

flat_list = [item for sublist in l for item in sublist]

which means:

flat_list = []
for sublist in l:
for item in sublist:
flat_list.append(item)

is faster than the shortcuts posted so far. (l is the list to flatten.)

Here is the corresponding function:

def flatten(l):
return [item for sublist in l for item in sublist]

As evidence, you can use the timeit module in the standard library:

$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' '[item for sublist in l for item in sublist]'
10000 loops, best of 3: 143 usec per loop
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'sum(l, [])'
1000 loops, best of 3: 969 usec per loop
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'reduce(lambda x,y: x+y,l)'
1000 loops, best of 3: 1.1 msec per loop

Explanation: the shortcuts based on + (including the implied use in sum) are, of necessity, O(L**2) when there are L sublists -- as the intermediate result list keeps getting longer, at each step a new intermediate result list object gets allocated, and all the items in the previous intermediate result must be copied over (as well as a few new ones added at the end). So, for simplicity and without actual loss of generality, say you have L sublists of I items each: the first I items are copied back and forth L-1 times, the second I items L-2 times, and so on; total number of copies is I times the sum of x for x from 1 to L excluded, i.e., I * (L**2)/2.

The list comprehension just generates one list, once, and copies each item over (from its original place of residence to the result list) also exactly once.

How to convert List of Lists of Tuples- pairs (index,value) into 2D numpy array

You can figure out the array's dimensions the following way. The Y dimension is the number of sublists

>>> data = [[(0, 0.5), (1, 0.6)], [(4, 0.01), (5, 0.005), (6, 0.002)], [(1,0.7)]]
>>> dim_y = len(data)
>>> dim_y
3

The X dimension is the largest [0] index of all of the tuples, plus 1.

>>> dim_x = max(max(i for i,j in sub) for sub in data) + 1
>>> dim_x
7

So then initialize an array of all zeros with this size

>>> import numpy as np
>>> arr = np.zeros((dim_x, dim_y))
>>> arr
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])

Now to fill it, enumerate over your sublists to keep track of the y index. Then for each sublist use the [0] for the x index and the [1] for the value itself

for y, sub in enumerate(data):
for x, value in sub:
arr[x,y] = value

Then the resulting array should be populated (might want to transpose to look like your desired dimensions).

>>> arr.T
array([[0.5 , 0.6 , 0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0.01 , 0.005, 0.002],
[0. , 0.7 , 0. , 0. , 0. , 0. , 0. ]])

How to get first element in a list of tuples?

>>> a = [(1, u'abc'), (2, u'def')]
>>> [i[0] for i in a]
[1, 2]


Related Topics



Leave a reply



Submit