Sorry if this is a noob question.
i have a list, for exemple "a" which has two or tree list inside a1,a2,a3 like :
[ [1,2], [0,0,0], [1,2,3,4,5] ]
[ [1,2,3,4], [0,0], [1,2,3] ]
etc...
Each list inside doesn't has the same length so when I make a simple
for i in range(len(a)):
print(a[i])
the result is :
[[1,2],[0,0,0],[1,2,3,4,5]]
[[1,2,3,4],[0,0],[1,2,3]]
While I'm waiting for this result where elements in list are aligned in columns :
[[1,2] [0,0,0] [1,2,3,4,5]]
[[1,2,3,4] [0,0] [1,2,3]]
How can I do that?
thanks in advance for any help.
You can use string's ljust method to align the lists according to the largest:
a = [[[1, 2], [0, 0, 0], [1, 2, 3, 4, 5]],
[[1, 2, 3, 4], [0, 0], [1, 2, 3]]]
size = max([len(str(x)) for sub in a for x in sub]) + 4
for sub in a:
for x in sub:
print(str(x).ljust(size), end="")
print()
This Gives:
[1, 2] [0, 0, 0] [1, 2, 3, 4, 5]
[1, 2, 3, 4] [0, 0] [1, 2, 3]
Another option using one loop and the join method could be:
for sub in a:
print(''.join((str(x).ljust(size) for x in sub)))
Another quick-and-dirty approach, using str.format:
a = [[ [1,2], [0,0,0], [1,2,3,4,5] ],
[ [1,2,3,4], [0,0], [1,2,3] ],
[ [1], [2,2]],
[]]
max_sublist = max(a, key=len)
max_len = len(str(max((i for subl in a for i in subl), key=lambda k: len(str(k)))))
for subl in a:
print('[{}]'.format(('{!s:<{width}}' * len(max_sublist)).format(*subl, *' '*len(max_sublist), width=max_len)))
Prints:
[[1, 2] [0, 0, 0] [1, 2, 3, 4, 5]]
[[1, 2, 3, 4] [0, 0] [1, 2, 3] ]
[[1] [2, 2] ]
[ ]
Related
In the help of numpy.broadcst-array, an idiom is introduced.
However, the idiom give exactly the same output as original command.
Waht is the meaning of "getting contiguous copies instead of non-contiguous views."?
https://docs.scipy.org/doc/numpy/reference/generated/numpy.broadcast_arrays.html
x = np.array([[1,2,3]])
y = np.array([[1],[2],[3]])
np.broadcast_arrays(x, y)
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
Here is a useful idiom for getting contiguous copies instead of non-contiguous views.
[np.array(a) for a in np.broadcast_arrays(x, y)]
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
To understand the difference try writing into the new arrays:
Let's begin with the contiguous copies.
>>> import numpy as np
>>> x = np.array([[1,2,3]])
>>> y = np.array([[1],[2],[3]])
>>>
>>> xc, yc = [np.array(a) for a in np.broadcast_arrays(x, y)]
>>> xc
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
We can modify an element and nothing unexpected will happen.
>>> xc[0, 0] = 0
>>> xc
array([[0, 2, 3],
[1, 2, 3],
[1, 2, 3]])
>>> x
array([[1, 2, 3]])
Now, let's try the same with the broadcasted arrays:
>>> xb, yb = np.broadcast_arrays(x, y)
>>> xb
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
Although we only write to the top left element ...
>>> xb[0, 0] = 0
... the entire left column will change ...
>>> xb
array([[0, 2, 3],
[0, 2, 3],
[0, 2, 3]])
... and also the input array.
>>> x
array([[0, 2, 3]])
It means that broadcast_arrays function doesn't create entirely new object. It creates views from original arrays which means the elements of it's results have memory addresses as those arrays which may or may not be contiguous. But when you create a list you're creating new copies within a list which guarantees that its items are stored contiguous in memory.
You can check this like following:
arr = np.broadcast_arrays(x, y)
In [144]: arr
Out[144]:
[array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
In [145]: x
Out[145]: array([[1, 2, 3]])
In [146]: arr[0][0] = 0
In [147]: arr
Out[147]:
[array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])]
In [148]: x
Out[148]: array([[0, 0, 0]])
As you can see, changing the arr's elements is changing both its elements and the original x array.
I want to create this:
[ [ 1, 1 ], [ 1, 2 ], [ 1, 3 ], [ 2, 1 ] ... [ 6, 3 ]]
Using GroovyConsole I've being trying stuff like this:
def blob = (1..6).collect{ i ->
(1..3).collect{ j ->
[ i, j ]
}
}
println "$blob ${blob.class.simpleName}"
... with various permutations of calls to flatten, as you might surmise.
Is there a way to achieve this?
It's easier to do using the combinations method:
l1 = [1,2,3,4,5,6]
l2 = [1,2,3]
[l, l2].combinations()
Which outputs:
[[1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [1, 2], [2, 2],
[3, 2], [4, 2], [5, 2], [6, 2], [1, 3], [2, 3], [3, 3], [4, 3],
[5, 3], [6, 3]]
How will i access the list in a list?
For example
data = [ [[list([1,2,3]), list([0,1])]], [[list([4,5,6]), list([1,1])]] ]
The output data should be
output = [[1,2,3],[4,5,6]]
Access individual lists as follows by index.
data[0] = [[[1, 2, 3], [0, 1]]]
data[0][0] = [[1, 2, 3], [0, 1]]
data[0][0][0] = [1, 2, 3]
data[1][0][0] = [4, 5, 6]
This is what i did
>>> data = [ [[list([1,2,3]), list([0,1])]], [[list([4,5,6]), list([1,1])]] ]
>>> output = list([row[0][0] for row in data])
>>> print (output)
[[1, 2, 3], [4, 5, 6]]
Can anyone help me. This is what i want to do.
x = [[1,2,3,4,5],[6,7,8,9,10]]
y= [0,1]
desired output = [
[[1,2,3,4,5],[0,1]],
[[6,7,8,9,10],[0,1]]
]
I try putting it in a for loop
>>> x = [[1,2,3,4,5],[6,7,8,9,10]]
>>> for value in x:
... a = []
... a += ([x,y])
... print(a)
...
[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [0, 1]]
[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [0, 1]]
I also tried doing this
>>> for value in x:
... a = []
... a += ([x,y])
... print(a)
...
[[1, 2, 3, 4, 5], [0, 1]]
[[1, 2, 3, 4, 5], [0, 1]]
[[1, 2, 3, 4, 5], [0, 1]]
[[1, 2, 3, 4, 5], [0, 1]]
[[1, 2, 3, 4, 5], [0, 1]]
Thank you for helping. I need it for putting label on my data for neural networks.
You can use a list comprehension, and iterate over each sublist in x. Since you're inserting y into different sublists, you might want to insert a copy of the list, not the original.
[[i, y[:]] for i in x]
Or,
[[i, y.copy()] for i in x]
[[[1, 2, 3, 4, 5], [0, 1]], [[6, 7, 8, 9, 10], [0, 1]]]
The copy is done as a safety precaution. To understand why, consider an example,
z = [[i, y] for i in x] # inserting y (reference copy)
y[0] = 12345
print(z)
[[[1, 2, 3, 4, 5], [12345, 1]], [[6, 7, 8, 9, 10], [12345, 1]]] # oops
Modifying the original y or the y in any other sublist will reflect changes across all sublists. You can prevent that by inserting a copy instead, which is what I've done at the top.
Try this:
for i in range(len(x)):
z[i] = [x[i],y];
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