Merge lists of different size with order - python

Is there a good way to merge lists like that:
L1 = [1.1, 1.2, 1.3]
L2 = [2.1, 2.2, 2.3, 2.4]
L3 = [3.1, 3.2]
Result:
[1.1, 2.1, 3.1, 1.2, 2.2, 3.2, 1.3, 2.3, 2.4]
There should be no "None" elements in result.
Edit
Since it was marked as duplicate:
I do not need a result like this:
[(1.1, 2.1, 3.1), (1.2, 2.2, 3.2), (1.3, 2.3, None), (None, 2.4, None)]
I do not need any "None" elements. And the result should be one list.

With izip_longest from itertools:
>>> from itertools import izip_longest
>>> L1 = [1.1, 1.2, 1.3]
>>> L2 = [2.1, 2.2, 2.3, 2.4]
>>> L3 = [3.1, 3.2]
>>> [x for sub in izip_longest(L1,L2,L3) for x in sub if x is not None]
[1.1, 2.1, 3.1, 1.2, 2.2, 3.2, 1.3, 2.3, 2.4]
Answer to the comment:
What if the lists have None in them?
None is the default fillvalue:
>>> list(izip_longest(L1,L2,L3))
[(1.1, 2.1, 3.1), (1.2, 2.2, 3.2), (1.3, 2.3, None), (None, 2.4, None)]
If the lists can have None in them, use a fillvalue that cannot appear in the lists. For example:
>>> list(izip_longest(L1,L2,L3,fillvalue='my_awesome_fillval'))
[(1.1, 2.1, 3.1), (1.2, 2.2, 3.2), (1.3, 2.3, 'my_awesome_fillval'), ('my_awesome_fillval', 2.4, 'my_awesome_fillval')]

To merge the lists
L1 = [1.1, 1.2, 1.3]
L2 = [2.1, 2.2, 2.3, 2.4]
L3 = [3.1, 3.2]
you can use the following one-liner
>>> [x for y in map(None,L1,L2,L3) for x in y if x is not None]
[1.1, 2.1, 3.1, 1.2, 2.2, 3.2, 1.3, 2.3, 2.4]

Related

Storing multiple arrays in a np.zeros or np.ones

I'm trying to initialize a dummy array of length n using np.zeros(n) with dtype=object. I want to use this dummy array to store n copies of another array of length m.
I'm trying to avoid for loop to set values at each index.
I tried using the below code but keep getting error -
temp = np.zeros(10, dtype=object)
arr = np.array([1.1,1.2,1.3,1.4,1.5])
res = temp * arr
The desired result should be -
np.array([[1.1,1.2,1.3,1.4,1.5], [1.1,1.2,1.3,1.4,1.5], ... 10 copies])
I keep getting the error -
operands could not be broadcast together with shapes (10,) (5,)
I understand that this error arises since the compiler thinks I'm trying to multiply those arrays.
So how do I achieve the task?
np.tile() is a built-in function that repeats a given array reps times. It looks like this is exactly what you need, i.e.:
res = np.tile(arr, 2)
>>> arr = np.array([1.1,1.2,1.3,1.4,1.5])
>>> arr
array([1.1, 1.2, 1.3, 1.4, 1.5])
>>> np.array([arr]*10)
array([[1.1, 1.2, 1.3, 1.4, 1.5],
[1.1, 1.2, 1.3, 1.4, 1.5],
[1.1, 1.2, 1.3, 1.4, 1.5],
[1.1, 1.2, 1.3, 1.4, 1.5],
[1.1, 1.2, 1.3, 1.4, 1.5],
[1.1, 1.2, 1.3, 1.4, 1.5],
[1.1, 1.2, 1.3, 1.4, 1.5],
[1.1, 1.2, 1.3, 1.4, 1.5],
[1.1, 1.2, 1.3, 1.4, 1.5],
[1.1, 1.2, 1.3, 1.4, 1.5]])

How to pass argument of type char ** from Python to C API [duplicate]

As seen here How do I convert a Python list into a C array by using ctypes? this code will take a python array and transform it to a C array.
import ctypes
arr = (ctypes.c_int * len(pyarr))(*pyarr)
Which would the way of doing the same with a list of lists or a lists of lists of lists?
For example, for the following variable
list3d = [[[40.0, 1.2, 6.0, 0.3], [50.0, 4.2, 0, 0]], [[40.0, 1.2, 6.0, 0.3], [50.0, 4.2, 0, 0]], [[40.0, 1.2, 6.0, 0.3], [50.0, 4.2, 0, 0]]]
I have tried the following with no luck:
([[ctypes.c_double * 4] *2]*3)(*list3d)
# *** TypeError: 'list' object is not callable
(ctypes.c_double * 4 *2 *3)(*list3d)
# *** TypeError: expected c_double_Array_4_Array_2 instance, got list
Thank you!
EDIT: Just to clarify, I am trying to get one object that contains the whole multidimensional array, not a list of objects. This object's reference will be an input to a C DLL that expects a 3D array.
It works with tuples if you don't mind doing a bit of conversion first:
from ctypes import *
list3d = [
[[0.0, 1.0, 2.0, 3.0], [4.0, 5.0, 6.0, 7.0]],
[[0.2, 1.2, 2.2, 3.2], [4.2, 5.2, 6.2, 7.2]],
[[0.4, 1.4, 2.4, 3.4], [4.4, 5.4, 6.4, 7.4]],
]
arr = (c_double * 4 * 2 * 3)(*(tuple(tuple(j) for j in i) for i in list3d))
Check that it's initialized correctly in row-major order:
>>> (c_double * 24).from_buffer(arr)[:]
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,
0.2, 1.2, 2.2, 3.2, 4.2, 5.2, 6.2, 7.2,
0.4, 1.4, 2.4, 3.4, 4.4, 5.4, 6.4, 7.4]
Or you can create an empty array and initialize it using a loop. enumerate over the rows and columns of the list and assign the data to a slice:
arr = (c_double * 4 * 2 * 3)()
for i, row in enumerate(list3d):
for j, col in enumerate(row):
arr[i][j][:] = col
I made the change accordingly
a = [[[40.0, 1.2, 6.0, 0.3], [50.0, 4.2, 0, 0]], [[40.0, 1.2, 6.0, 0.3], [50.0, 4.2, 0, 0]], [[40.0, 1.2, 6.0, 0.3], [50.0, 4.2, 0, 0]]]
arr = (((ctypes.c_float * len(a[0][0])) * len(a[0])) * len(a))
arr_instance=arr()
for i in range(0,len(a)):
for j in range(0,len(a[0])):
for k in range(0,len(a[0][0])):
arr_instance[i][j][k]=a[i][j][k]
The arr_instance is what you want.

how to merge the values of a list of lists and a list into 1 resulting list of lists

I have a list of lists (a) and a list (b) which have the same "length" (in this case "4"):
a = [
[1.0, 2.0],
[1.1, 2.1],
[1.2, 2.2],
[1.3, 2.3]
]
b = [3.0, 3.1, 3.2, 3.3]
I would like to merge the values to obtain the following (c):
c = [
[1.0, 2.0, 3.0],
[1.1, 2.1, 3.1],
[1.2, 2.2, 3.2],
[1.3, 2.3, 3.3]
]
currently I'm doing the following to achieve it:
c = []
for index, elem in enumerate(a):
x = [a[index], [b[index]]] # x assigned here for better readability
c.append(sum(x, []))
my feeling is that there is an elegant way to do this...
note: the lists are a lot larger, for simplicity I shortened them. they are always(!) of the same length.
In python3.5+ use zip() within a list comprehension and in-place unpacking:
In [7]: [[*j, i] for i, j in zip(b, a)]
Out[7]: [[1.0, 2.0, 3.0], [1.1, 2.1, 3.1], [1.2, 2.2, 3.2], [1.3, 2.3, 3.3]]
In python 2 :
In [8]: [j+[i] for i, j in zip(b, a)]
Out[8]: [[1.0, 2.0, 3.0], [1.1, 2.1, 3.1], [1.2, 2.2, 3.2], [1.3, 2.3, 3.3]]
Or use numpy.column_stack in numpy:
In [16]: import numpy as np
In [17]: np.column_stack((a, b))
Out[17]:
array([[ 1. , 2. , 3. ],
[ 1.1, 2.1, 3.1],
[ 1.2, 2.2, 3.2],
[ 1.3, 2.3, 3.3]])

Compare adjacent values in numpy array

I have a Numpy one-dimensional array of data, something like this
a = [1.9, 2.3, 2.1, 2.5, 2.7, 3.0, 3.3, 3.2, 3.1]
I want to create a new array, where the values are composed of the greater of the adjacent values. For the above example, the output would be:
b = [2.3, 2.3, 2.5, 2.7, 3.0, 3.3, 3.3, 3.2]
I can do this by looping through the input array, comparing the neighbouring values, eg:
import numpy as np
a = np.array([1.9, 2.3, 2.1, 2.5, 2.7, 3.0, 3.3, 3.2, 3.1])
b = np.zeros(len(a)-1)
for i in range(len(a)-1):
if (a[i] > a[i+1]):
b[i] = a[i]
else:
b[i] = a[i+1]
but I'd like to do this in a more elegant "pythonic" vectorised fashion. I've searched and read about np.zip, np.where, np.diff etc but haven't yet found a way to do this (or more likely, I haven't understood what is possible). Any suggestions ?
You want element-wise maximum of a[1:] and a[:-1]:
>>> a
array([ 1.9, 2.3, 2.1, 2.5, 2.7, 3. , 3.3, 3.2, 3.1])
>>> a[1:]
array([ 2.3, 2.1, 2.5, 2.7, 3. , 3.3, 3.2, 3.1])
>>> a[:-1]
array([ 1.9, 2.3, 2.1, 2.5, 2.7, 3. , 3.3, 3.2])
>>> np.maximum(a[1:], a[:-1])
array([ 2.3, 2.3, 2.5, 2.7, 3. , 3.3, 3.3, 3.2])

How can a Python list be sliced such that a column is moved to being a separate element column?

I have a list of the following form:
[[0, 5.1, 3.5, 1.4, 0.2],
[0, 4.9, 3.0, 1.4, 0.2],
[0, 4.7, 3.2, 1.3, 0.2],
[1, 4.6, 3.1, 1.5, 0.2],
[1, 5.0, 3.6, 1.4, 0.2],
[1, 5.4, 3.9, 1.7, 0.4],
[1, 4.6, 3.4, 1.4, 0.3]]
I want to slice out the first column and add it as a new element to each row of data (so at each odd position in the list), changing it to the following form:
[[5.1, 3.5, 1.4, 0.2], [0],
[4.9, 3.0, 1.4, 0.2], [0],
[4.7, 3.2, 1.3, 0.2], [0],
[4.6, 3.1, 1.5, 0.2], [1],
[5.0, 3.6, 1.4, 0.2], [1],
[5.4, 3.9, 1.7, 0.4], [1],
[4.6, 3.4, 1.4, 0.3], [1],]
How could I do this?
So far, I have extracted the necessary information in the following ways:
targets = [element[0] for element in dataset]
features = dataset[1:]
Try indexing and then get flattened list- i used list comprehension for flattening.
>>>l=[[0, 5.1, 3.5, 1.4, 0.2],
[0, 4.9, 3.0, 1.4, 0.2],
[0, 4.7, 3.2, 1.3, 0.2],
[1, 4.6, 3.1, 1.5, 0.2],
[1, 5.0, 3.6, 1.4, 0.2],
[1, 5.4, 3.9, 1.7, 0.4],
[1, 4.6, 3.4, 1.4, 0.3]]
>>>[[i[1:],[i[0]]] for i in l]#get sliced list of lists
>>>[[[5.1, 3.5, 1.4, 0.2], [0]], [[4.9, 3.0, 1.4, 0.2], [0]], [[4.7, 3.2, 1.3, 0.2], [0]], [[4.6, 3.1, 1.5, 0.2], [1]], [[5.0, 3.6, 1.4, 0.2], [1]], [[5.4, 3.9, 1.7, 0.4], [1]], [[4.6, 3.4, 1.4, 0.3], [1]]]
>>>d=[[i[1:],[i[0]]] for i in l]
>>>[item for sublist in d for item in sublist]#flatten list d
>>>[[5.1, 3.5, 1.4, 0.2], [0], [4.9, 3.0, 1.4, 0.2], [0], [4.7, 3.2, 1.3, 0.2], [0], [4.6, 3.1, 1.5, 0.2], [1], [5.0, 3.6, 1.4, 0.2], [1], [5.4, 3.9, 1.7, 0.4], [1], [4.6, 3.4, 1.4, 0.3], [1]]
Just oneliner alternative-
[item for sublist in [[i[1:],[i[0]]] for i in l] for item in sublist] #Here l is that list
List comprehensions are nice but can be a bit hard to scan. Loops are still useful, especially when combined with extend:
res = []
for entry in dataset:
res.extend([entry[1:], entry[:1]])
now:
import pprint
pprint.pprint(res)
prints:
[[5.1, 3.5, 1.4, 0.2],
[0],
[4.9, 3.0, 1.4, 0.2],
[0],
[4.7, 3.2, 1.3, 0.2],
[0],
[4.6, 3.1, 1.5, 0.2],
[1],
[5.0, 3.6, 1.4, 0.2],
[1],
[5.4, 3.9, 1.7, 0.4],
[1],
[4.6, 3.4, 1.4, 0.3],
[1]]
Try this:
from itertools import chain
print list(chain(*[list((element[1:],[element[0]])) for element in a]))
Output:
[[5.1, 3.5, 1.4, 0.2], [0], [4.9, 3.0, 1.4, 0.2], [0],
[4.7, 3.2, 1.3, 0.2], [0], [4.6, 3.1, 1.5, 0.2], [1],
[5.0, 3.6, 1.4, 0.2], [1], [5.4, 3.9, 1.7, 0.4], [1],
[4.6, 3.4, 1.4, 0.3], [1]]
Slice each sublist and make a new list with an element for each slice:
l = [[0, 5.1, 3.5, 1.4, 0.2],
[0, 4.9, 3.0, 1.4, 0.2],
[0, 4.7, 3.2, 1.3, 0.2],
[1, 4.6, 3.1, 1.5, 0.2],
[1, 5.0, 3.6, 1.4, 0.2],
[1, 5.4, 3.9, 1.7, 0.4],
[1, 4.6, 3.4, 1.4, 0.3]]
>>> print(*[item for sub in l for item in (sub[1:], [sub[0]])], sep='\n')
[5.1, 3.5, 1.4, 0.2]
[0]
[4.9, 3.0, 1.4, 0.2]
[0]
[4.7, 3.2, 1.3, 0.2]
[0]
[4.6, 3.1, 1.5, 0.2]
[1]
[5.0, 3.6, 1.4, 0.2]
[1]
[5.4, 3.9, 1.7, 0.4]
[1]
[4.6, 3.4, 1.4, 0.3]
[1]
A Pythonic approach in python 3.X using unpacking iteration and itertools.chain:
>>> from itertools import chain
>>>
>>> list(chain.from_iterable([[j,[i]] for i,*j in A]))
[[5.1, 3.5, 1.4, 0.2], [0],
[4.9, 3.0, 1.4, 0.2], [0],
[4.7, 3.2, 1.3, 0.2], [0],
[4.6, 3.1, 1.5, 0.2], [1],
[5.0, 3.6, 1.4, 0.2], [1],
[5.4, 3.9, 1.7, 0.4], [1],
[4.6, 3.4, 1.4, 0.3], [1]]

Categories