I have several lists with the same number of elements, example:
[[1, 2, 3, 1], [3, 2, 1, 2], [3, 3, 1, 1], [...], .....etc...
I would like to get a single list containing all the items in the listed lists, example:
[1, 2, 3, 1, 3, 2, 1, 2, 3, 3, 1, 1, ..........]
How can I get this result in the simplest way possible?
I tried this way but I can not find a solution.
a = [[1, 2, 3, 1], [3, 2, 1, 2], [3, 3, 1, 1]]
b = len(a)
for n in range(b):
a[0] += a[n]
print(a[0])
result:
[1, 2, 3, 1, 1, 2, 3, 1]
[1, 2, 3, 1, 1, 2, 3, 1, 3, 2, 1, 2]
[1, 2, 3, 1, 1, 2, 3, 1, 3, 2, 1, 2, 3, 3, 1, 1]
I would only use the last list produced without the repetition of the first list, but I can not correct it and extrapolate it.
Thank you.
A straightforward solution would use extend method of a list.
a = [[1, 2, 3, 1], [3, 2, 1, 2], [3, 3, 1, 1]]
result = []
for aa in a:
result.extend(aa)
I hope you will find Python's itertools interesting for this as well.
from itertools import chain
a = [[1, 2, 3, 1], [3, 2, 1, 2], [3, 3, 1, 1]]
result = list(chain(*a))
*a is basically unpacking the list for you.
Python has a reduce() function that can be used like this:
a = [[1, 2, 3, 1], [3, 2, 1, 2], [3, 3, 1, 1]]
import functools
functools.reduce(lambda x, y: x+y, a)
Result:
[1, 2, 3, 1, 3, 2, 1, 2, 3, 3, 1, 1]
This may be less efficient than modifying an element in-place, as in the question. If you want to keep using that, the simple thing to do is to start with an empty list rather than re-using a[0]:
result = []
for e in a:
result += e
print(result)
Related
Tensor a:
tensor([[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
Tensor b:
tensor([4,4,4,4])
Question 1:
How to merge two tensors and get result c:
tensor([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])
Question 2: How to divide tensor c and get original a and b.
Question 1: Merge two tensors -
torch.cat((a, b.unsqueeze(1)), 1)
>>> tensor([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])
First, we use torch.unsqueeze to add single dim in b tensor to match a dim to be concanate. Then use torch.cat Concatenates tensors a and b.
Question 2:
a = c[:][:,:-1]
a
>>> tensor([[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
b = c[:][:,-1:].squeeze(1)
b
>>> tensor([4, 4, 4, 4])
You have to slightly modify tensor b:
a = torch.tensor([[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
b = torch.tensor([4,4,4,4])
b = b.reshape(1, 4)
Then you get your "joined" tensor:
c = torch.cat((a, torch.t(b)), 1)
And backward:
a1 = c[:,:-1]
b1 = torch.t(c[:,-1:])
I want to ask how numpy remove columns in batch by list.
The value in the list corresponds to the batch is different from each other.
I know this problem can use the for loop to solve, but it is too slow ...
Can anyone give me some idea to speed up?
array (batch size = 3):
[[0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6]]
remove index in the list (batch size = 3)
[[2, 3, 4], [1, 2, 6], [0, 1, 5]]
output:
[[0, 1, 5, 6], [0, 3, 4, 5], [2, 3, 4, 6]]
Assuming the array is 2d, and the indexing removes equal number of elements per row, we can remove items with a boolean mask:
In [289]: arr = np.array([[0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6]]
...: )
In [290]: idx = np.array([[2, 3, 4], [1, 2, 6], [0, 1, 5]])
In [291]: mask = np.ones_like(arr, dtype=bool)
In [292]: mask[np.arange(3)[:,None], idx] = False
In [293]: arr[mask]
Out[293]: array([0, 1, 5, 6, 0, 3, 4, 5, 2, 3, 4, 6])
In [294]: arr[mask].reshape(3,-1)
Out[294]:
array([[0, 1, 5, 6],
[0, 3, 4, 5],
[2, 3, 4, 6]])
`So,I am given a list-
group = [2,1,3,4]
Each index of this list represents a group.
So group 0 = 2
group 1 = 1
group 2 = 3
group 3 = 4
I am given another list called :
l =[[[0, 0, 3, 3, 3, 3], [0, 0, 1, 3, 3, 3, 3]], [[0, 1]], [[2, 2, 2, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3, 3]], [[0, 0, 2, 2, 2, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3, 3]]]
the output I want is:
dict = {0:[0,3],1;[1],2:[2,3],3:[0,2]}
If the number of times the element appears in each sublist of l ie if both l[0][0] and l[0][1] have 0's appear 2 times, it is added to the index 0 of the dict. Since both l[0][0] and l[0][1] have 3 appear 4 times(this is because group[3] is 4), it is added to the index 0.
now in l[1][0] and 0 appears just once(instead of twice) so its not added to index 1. However 1 just appears once so it is added to index 1.Thanks!
What I have tried so far:
def tryin(l,groups):
for i in range(len(l)):
count = 0
for j in range(len(l[i])):
if j in (l[i][j]):
count+=1
if count == groups[i]:
print(i,j)
try this code :
input:
group = [2,1,3,4]
l =[[[0, 0, 3, 3, 3, 3], [0, 0, 1, 3, 3, 3, 3]], [[0, 1]], [[2, 2, 2, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3, 3], [2, 2, 2, 3, 3, 3, 3]], [[0, 0, 2, 2, 2, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3, 3], [0, 0, 2, 2, 2, 3, 3, 3, 3]]]
def IntersecOfSets(list_):
result = set(list_[0])
for s in list_[1:]:
result.intersection_update(s)
return result
def nb_occ(l,group):
d = {}
for i in l:
l2=[]
for j in i:
l1 = []
for x in group:
if j.count(group.index(x)) >= x :
y=group.index(x)
l1.append(y)
l2.append(l1)
if len(l2)>1:
d[str(l.index(i))]= IntersecOfSets(l2)
else:
d[str(l.index(i))]= l2[0]
return d
print(nb_occ(l,group))
output:
{'2': {2, 3}, '1': [1], '0': {0, 3}, '3': {0, 2}}
I want to use a python function or library - if any - for creating a new matrix whose first row beginning from the right-below is created by using old matrix's first column beginning from the left-top. That matrix can have different columns and rows but of course my new matrix have to have same dimension as previous one. My will is something like that:
In keeping with the brief style of the question:
In [467]: alist = [5,6,4,3,4,5,3,2,5,3,1,2,2,3,2,1,3,1,1,1]
In [468]: arr = np.array(alist).reshape(4,5)
In [469]: arr
Out[469]:
array([[5, 6, 4, 3, 4],
[5, 3, 2, 5, 3],
[1, 2, 2, 3, 2],
[1, 3, 1, 1, 1]])
In [470]: arr.reshape(5,4)
Out[470]:
array([[5, 6, 4, 3],
[4, 5, 3, 2],
[5, 3, 1, 2],
[2, 3, 2, 1],
[3, 1, 1, 1]])
In [471]: arr.reshape(5,4,order='F')
Out[471]:
array([[5, 3, 2, 1],
[5, 2, 1, 4],
[1, 3, 3, 3],
[1, 4, 5, 2],
[6, 2, 3, 1]])
In [473]: np.rot90(_)
Out[473]:
array([[1, 4, 3, 2, 1],
[2, 1, 3, 5, 3],
[3, 2, 3, 4, 2],
[5, 5, 1, 1, 6]])
I have a really weird issue in Python3. I have some code that generates some different list from a list of integers (what this does exactly is not relevant).
def get_next_state(state):
max_val = max(state)
max_index = state.index(max_val)
new_state = state
for n in range(max_val + 1):
new_state[max_index] -= 1
new_state[(n + max_index) % len(state)] += 1
return new_state
Now all I want to do is add a next state to some list that keeps the states. This is my code:
l = [0, 2, 7, 0]
seen_states = []
for _ in range(10):
print(l)
seen_states += [l]
l = get_next_state(l)
print(seen_states)
For some reason list l is assigned correctly when I print it, but it is not added correctly to the list of seen states. Note that I have also tried seen_states.append(l) instead of using += [l]. Does anyone know why this happend?
To prevent these problems use copy, deepcopy or list[:]
import copy
def get_next_state(state):
max_val = max(state)
max_index = state.index(max_val)
new_state = copy.deepcopy(state)
for n in range(max_val + 1):
new_state[max_index] -= 1
new_state[(n + max_index) % len(state)] += 1
return new_state
You are sending your list l inside the function. When you assign it to another name, you are not making a copy but referencing the original object. Any modification on these lists is a change in the original object.
Using new_state = state[:] (copies the list instead of referencing the original) in the get_next_state produces the following output:
[0, 2, 7, 0]
[2, 4, 1, 2]
[3, 1, 2, 3]
[0, 2, 3, 4]
[1, 3, 4, 1]
[2, 4, 1, 2]
[3, 1, 2, 3]
[0, 2, 3, 4]
[1, 3, 4, 1]
[2, 4, 1, 2]
[[0, 2, 7, 0], [2, 4, 1, 2], [3, 1, 2, 3], [0, 2, 3, 4], [1, 3, 4, 1], [2, 4, 1, 2], [3, 1, 2, 3], [0, 2, 3, 4], [1, 3, 4, 1], [2, 4, 1, 2]]
Not sure if this is your desired output, as you did not specify what you are getting as "wrong" output