How to build data frame out of two lists in a way shown below?
I have tried iterate over lists but I couldn't figure out way add only one element to each list.
like this:
for e in listone:
for list in listtwo:
list.insert(0, e)
Example:
listone = [1, 2, 3]
listtwo = [[4, 5, 6],[7, 8, 9]]
Expected outcome:
[[1, 4, 5, 6],
[1 ,7, 8, 9],
[2, 4, 5, 6],
[2, 7, 8, 9],
[3, 4, 5, 6],
[3, 7, 8, 9]]
A simple list comprehension is enough:
listone = [1, 2, 3]
listtwo = [[4, 5, 6],[7, 8, 9]]
result = [[k, *v] for k in listone for v in listtwo]
print(result)
Output
[[1, 4, 5, 6], [1, 7, 8, 9], [2, 4, 5, 6], [2, 7, 8, 9], [3, 4, 5, 6], [3, 7, 8, 9]]
Let's try a list comprehension on product:
from itertools import product
[[x]+y for x,y in product(listone, listtwo)]
Output:
[[1, 4, 5, 6],
[1, 7, 8, 9],
[2, 4, 5, 6],
[2, 7, 8, 9],
[3, 4, 5, 6],
[3, 7, 8, 9]]
A solution without itertools:
listone = [1, 2, 3]
listtwo = [[4, 5, 6],[7, 8, 9]]
out = []
for i in listone:
for j in listtwo:
out.append([i, *j])
print(out)
Prints:
[[1, 4, 5, 6],
[1, 7, 8, 9],
[2, 4, 5, 6],
[2, 7, 8, 9],
[3, 4, 5, 6],
[3, 7, 8, 9]]
Related
I have two 2d array, A and B, like the following.
A=array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7],
[8, 8, 8],
[9, 9, 9]])
B=array([[1, 1, 1],
[3, 3, 3],
[8, 8, 8]])
I want to remove subarrays of A if they exist in B. Then return a new 2d array C like the following:
C=array([[2, 2, 2],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7],
[9, 9, 9]])
Currently I have tried the np.isin function but the result is no longer a 2d array.
mask = np.isin(A, B, invert=True)
A[mask]
>>array([2, 2, 2, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 9, 9, 9])
You need to aggregate the booleans on axis 1:
C = A[~np.isin(A,B).all(1)]
output:
array([[2, 2, 2],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7],
[9, 9, 9]])
Or in1d:
C = A[~np.in1d(A, B).all(axis=1)]
And now:
print(C)
Output:
[[2 2 2]
[4 4 4]
[5 5 5]
[6 6 6]
[7 7 7]
[9 9 9]]
I have a list which contains both ints and also lists of ints within the list.
I am trying to make a method which checks if there are any 1 number lists within the list, and converts that one number list into just an int. So it is no longer a list within the list.
I am using the below code as a guide, but this code works with a matrix and mine is a 3d list.
# Process grid with Possible array values as cells are solved
for row in range(0, GRIDSIZE):
for col in range(0, GRIDSIZE):
# Found correct cell value = Only 1 possible value
if np.size(P[row, col]) == 1:
singleton = P[row][col][0]
grid[row][col] = singleton
# Remove from Possible list
P[row, col].remove(singleton)
This is what I'm trying to do but I get indexerror: invalid index to scalar variable:
for row in range(0,9):
for col in range(0,9):
if np.size((values[row][col])) == 1:
singleton = values[row][col][0]
values[row][col] = singleton
Also for the 'remove from possible list' part, is there a simple pythonic command i can do to remove that singleton element from the 2d list?
This is what the list looks like at the moment:
[[[7], 8, 5, [6], 1, 3, [2, 6], [4], 9], [6, 3, 4, [8, 9], [8, 9], 2, 1, 7, 5], [[1], 2, [1, 9], 5, 7, 4, [6], 3, [8, 6]], [2, 4, 8, 3, 6, 7, 9, 5, 1], [9, 6, [1], 4, 5, 8, [7], 2, 3], [3, 5, 7, 2, [9], [1], 4, 8, [6]], [5, 7, 3, 1, [4], [6], 8, 9, 2], [4, 9, 6, [8, 7], 2, 5, 3, 1, [7]], [8, 1, 2, [7], 3, 9, 5, 6, 4]]
You can use a nested list comprehension:
original_list = [[[7], 8, 5, [6], 1, 3, [2, 6], [4], 9], [6, 3, 4, [8, 9], [8, 9], 2, 1, 7, 5], [[1], 2, [1, 9], 5, 7, 4, [6], 3, [8, 6]], [2, 4, 8, 3, 6, 7, 9, 5, 1], [9, 6, [1], 4, 5, 8, [7], 2, 3], [3, 5, 7, 2, [9], [1], 4, 8, [6]], [5, 7, 3, 1, [4], [6], 8, 9, 2], [4, 9, 6, [8, 7], 2, 5, 3, 1, [7]], [8, 1, 2, [7], 3, 9, 5, 6, 4]]
new_list = [[elem[0] if (isinstance(elem, list) and len(elem)==1) else elem for elem in sublist] for sublist in original_list ]
# new_list
"""
[[7, 8, 5, 6, 1, 3, [2, 6], 4, 9],
[6, 3, 4, [8, 9], [8, 9], 2, 1, 7, 5],
[1, 2, [1, 9], 5, 7, 4, 6, 3, [8, 6]],
[2, 4, 8, 3, 6, 7, 9, 5, 1],
[9, 6, 1, 4, 5, 8, 7, 2, 3],
[3, 5, 7, 2, 9, 1, 4, 8, 6],
[5, 7, 3, 1, 4, 6, 8, 9, 2],
[4, 9, 6, [8, 7], 2, 5, 3, 1, 7],
[8, 1, 2, 7, 3, 9, 5, 6, 4]]"""
EDIT: Using for loops:
new_list = []
for sublist in original_list:
new_sublist = []
for elem in sublist:
if (isinstance(elem, list) and len(elem) == 1):
new_sublist.append(elem[0])
else:
new_sublist.append(elem)
new_list.append(new_sublist)
m = [[5,9,1,8],
[2,4,5,7],
[6,3,3,2],
[1,7,6,3]]
rotated_map = []
for i in range(len(m[0])):
rotated_map.append([x[i] for x in m])
print(rotated_map)
"""
my result = [[5, 2, 6, 1], [9, 4, 3, 7], [1, 5, 3, 6], [8, 7, 2, 3]]
desired result = [[8,7,2,3],
[1,5,3,6],
[9,4,3,7],
[5,2,6,1]]
"""
I am trying to rotate the list by putting all the last elements first from the lists into one list then the second to last element into another and so on until i get to the first element.
Transpose the list with zip, then reverse it with the [::-1] syntax.
>>> m = [[5, 9, 1, 8], [2, 4, 5, 7], [6, 3, 3, 2], [1, 7, 6, 3]]
>>> list(map(list, zip(*m)))[::-1]
>>> [[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
edit:
If you want pretty printing, it's probably easiest to use numpy arrays all the way.
>>> import numpy as np
>>>
>>> m = [[5, 9, 1, 8], [2, 4, 5, 7], [6, 3, 3, 2], [1, 7, 6, 3]]
>>> m = np.array(m)
>>> m
>>>
array([[5, 9, 1, 8],
[2, 4, 5, 7],
[6, 3, 3, 2],
[1, 7, 6, 3]])
>>>
>>> m.T[::-1]
>>>
array([[8, 7, 2, 3],
[1, 5, 3, 6],
[9, 4, 3, 7],
[5, 2, 6, 1]])
Note that m and m.T[::-1] share the same data, because m.T[::-1] is just another view of m. If you need to duplicate the data, use
result = m.T[::-1].copy()
You could use zip, unpacking your list of lists with the *, and inversing the result with [::-1]:
m = [[5,9,1,8],
[2,4,5,7],
[6,3,3,2],
[1,7,6,3]]
res = [list(i) for i in zip(*m)][::-1]
>>> res
[[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
If numpy is an option, transposing is easier:
import numpy as np
>>> np.transpose(m)[::-1]
array([[8, 7, 2, 3],
[1, 5, 3, 6],
[9, 4, 3, 7],
[5, 2, 6, 1]])
# or:
>>> np.flip(np.transpose(m),0)
array([[8, 7, 2, 3],
[1, 5, 3, 6],
[9, 4, 3, 7],
[5, 2, 6, 1]])
You can use numpy module to do it. It has the property to transpose the array. Check the below code:
import numpy as np
m = [[5,9,1,8],
[2,4,5,7],
[6,3,3,2],
[1,7,6,3]]
arr = np.array(m).transpose()
new_list = []
for i in range(arr.shape[0]-1,-1,-1):
new_list.append(list(arr[i]))
print(new_list)
Output:
[[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
If you want to rotate the list clockwise:
list(map(list, zip(*m[::-1])))
Else, for anti-clockwise:
list(map(list, zip(*m)))[::-1]
use the reverse keyword
result = [[5, 2, 6, 1], [9, 4, 3, 7], [1, 5, 3, 6], [8, 7, 2, 3]]
result.reverse()
print(result)
output:
[[8, 7, 2, 3], [1, 5, 3, 6], [9, 4, 3, 7], [5, 2, 6, 1]]
I able to split a list into array of chunks from a list as demonstrated in the bellow python code:
def split_list(the_list, chunk_size):
result_list = []
while the_list:
result_list.append(the_list[:chunk_size])
the_list = the_list[chunk_size:]
return result_list
a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print split_list(a_list, 3)
which yield the bellow result of array of chunks:
# [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
I am also aware of making a random sample through numpy.random.choice (even with replacement) as demonstrated bellow:
import numpy as np
a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
np.random.choice(a_list, size=20,
replace=True)
which yields the bellow result:
#array([ 6, 9, 4, 9, 1, 1, 6, 10, 8, 5, 10, 6, 2, 6, 7, 1, 3,
2, 7, 6])
What I want
I want to sample chunk in the array (while the elements of each chunk is left as it is) with replacement.
I am looking forward to get a code that will produce something like this:
# [[7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9], [10], [1, 2, 3], [1, 2, 3], [10], [1, 2, 3], [7, 8, 9], [1, 2, 3], [1, 2, 3], [10], [4, 5, 6], [4, 5, 6], [10], [10], [7, 8, 9],, [1, 2, 3], [7, 8, 9]]
I picked the above sample of chunk myself, I need help to get a working python code to do that for me.
You can determine the number of different chunks in your list (4 in your example), then randomly choose the index of the one you want (between 0 and 3 in your example).
So, you could do:
import math
import random
def random_chunk(lst, chunk_size):
nb_chunks = int(math.ceil(len(lst)/chunk_size))
choice = random.randrange(nb_chunks) # 0 <= choice < nb_chunks
return lst[choice*chunk_size:(choice+1)*chunk_size]
a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
out = [random_chunk(a_list, chunk_size=3) for _ in range(20)]
print(out)
# [[10], [7, 8, 9], [4, 5, 6], [4, 5, 6], [1, 2, 3], [7, 8, 9], [7, 8, 9], [4, 5, 6],
# [10], [7, 8, 9], [10], [10], [10], [7, 8, 9], [10], [10], [10], [4, 5, 6], [4, 5, 6], [10]]
Let's say I have the following pattern:
PATTERN = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
and I want to use this to create the following:
PATTERN | HORIZONTAL_MIRROR (PATTERN)
VERTICAL_MIRROR(PATTERN) | HORIZONTAL_MIRROR(VERTICAL_MIRROR(PATTERN))
In other words:
[[1, 2, 3, 3, 2, 1],[4, 5, 6, 6, 5, 4],[7, 8, 9, 9, 8, 7], [7, 8, 9, 9, 8, 7], [4, 5, 6, 6, 5, 4], [1, 2, 3, 3, 2, 1]]
is there an efficient method that can be used in Python apart from copying each element from the PATTERN?
Just an idea:
PATTERN = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
def patternify(l):
for sl in l:
yield sl+sl[::-1]
for sl in l[::-1]:
yield sl+sl[::-1]
list(patternify(PATTERN))
#output: [[1, 2, 3, 3, 2, 1], [4, 5, 6, 6, 5, 4], [7, 8, 9, 9, 8, 7], [7, 8, 9, 9, 8, 7], [4, 5, 6, 6, 5, 4], [1, 2, 3, 3, 2, 1]]
If I understood the requirement correctly:
def mirror(pat):
return pat + pat[::-1]
SUPERPATTERN = mirror(map(mirror, PATTERN))