I would like to achieve something like this:
length_of_list = 3
length_of_sublists = 5
...
output = [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
I tried various times but I just can't understand how to do it.
Thank you for your help guys!
Edit: I include my code as asked! It clearly doesn't work as the output is nested lists. I'm sorry if I broke some rules, I'm new here :)
length_of_list = 3
length_of_sublist = 5
for i in range(length_of_list):
list = []
for j in range(1, length_of_sublist+1):
list.append(j)
list.append(list)
print(list)
[1, 2, 3, 4, 5, [1, 2, 3, 4, 5, [1, 2, 3, 4, 5]]]
If you want to create a nested list, consider using a nested list comprehension:
>>> length_of_list = 3
>>> length_of_sublists = 5
>>> [[i for i in range(1, length_of_sublists + 1)] for j in range(length_of_list)]
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
I don't know what you need this for, but here is your solution
lenght_of_list = 3
length_of_sublists = 5
output = []
for i in range(length_of_list):
sub_list = []
for j in range(length_of_sublists):
sub_list.append(j+1)
output.append(sub_list)
You should try yourself. It will definitively help you if you try out many possible approaches.
You don't need a loop for this. Just break down this one liner. You will get this.
>>> [list(range(1, length_of_sublists+1))] * length_of_list
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
Be careful when the item being repeated is a list. The list will not be cloned: all the elements will refer to the same list!
>>> output[0][0] = 9
>>> output
[[9, 2, 3, 4, 5], [9, 2, 3, 4, 5], [9, 2, 3, 4, 5]]
Related
I have a matrix that I want to convert to 3D so that I can be able to print the element of list[i][j][k]
a = [[[0, 1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6], [2, 3, 2, 3, 4,5], [3, 2, 3, 4, 3, 4], [4, 3, 4, 5, 4, 3], [5, 4, 5, 6, 5, 4]]]
print(a[5][5][0]) # I want to be able to print in 3D`
I get right output if I do a[5][5] but wrong when I add the [0]. Is there anyway of converting my matrix such that this will be solved?
I tried to just wrap the list up with brackets [list], but it did not work. I also did:
b = [[i] for i in a]
which gave me [[[0,1,2,3,4,5]],[[1,2,3,4,5,6]],...
and it still did not work!
NOTE: I want the i to be the row, j to be the column and k to be 0 or 1, so k = 0 (in which case the value is the row index of the cell is pointing to), or the k = 1 (the value is the column index).
Tried to reproduce your issue. To me, it works if you use the right index. Here, it perfectly works if you do for instance
print(a[0][0][5]) # I want to be able to print in 3D`
for list a = [[[0, 1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6], [2, 3, 2, 3, 4,5], [3, 2, 3, 4, 3, 4], [4, 3, 4, 5, 4, 3], [5, 4, 5, 6, 5, 4]]] you have just a[0][n][n]. You can try a[0][5][5]
You have index like below:
a = [
#0 element i
[
#0 element j
[0, 1, 2, 3, 4, 5],
#1 element j
[1, 2, 3, 4, 5, 6],
#2 element j
[2, 3, 2, 3, 4,5],
#3 element j
[3, 2, 3, 4, 3, 4],
#4 element j
[4, 3, 4, 5, 4, 3],
#5 element j
[5, 4, 5, 6, 5, 4]
]
]
print(a[0][5][5]) # a[i][j][k]
What would be the shortest way to count the number of odd numbers in a list of lists like this with arbitrary dimensions, not necessarily 5x5:
list_of_lists = [[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]]
I tried, but i'd guess there's shorter:
counter = 0
for row in list_of_lists:
for i in row:
if i % 2 != 0:
counter += 1
print(counter)
If you always have one level of flat lists inside the main list you can use an inner loop to flatten, summing the result of each i % 2 which will be 1 for odd and 0 for even numbers:
list_of_lists = [[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]]
print(sum(i % 2 for sub in list_of_lists for i in sub))
Or use itertools.chain to do the flattening:
from itertools import chain
print(sum(i % 2 for i in chain(*list_of_lists)))
If you had arbitrary nesting, recursion would be an easy way to approach the problem:
def flat(lst):
for i in lst:
if isinstance(i, list):
for j in flat(i):
yield j % 2
else:
yield i % 2
print(sum(flat(list_of_lists)))
The numpy way of counting for a contrast:
import numpy as np
list_of_lists = [[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]]
l = np.array(list_of_lists)
print len(l[l%2!=0])
l[l%2!=0] returns all the elements that satisfy the condition l%2!=0 and len() counts it.
PS : This of course assumes that the list_of_lists is rectangular. For a solution that allows different length for each inner-list, see Padraic's answer.
I apologize for a simple question, but I cannot find an answer to it in the archives.
what should i do to sort smaller lists within a larger list.
for example
lst=[[1, 2, 3, 4], [5,9,8,7], [9, 0, 1, 2, 3 ]]
should return
[1, 2, 3, 4]
[5,7,8,9]
[ 0, 1, 2, 3,9 ]
I tried to use lambda
lst.sort(key=lambda x: x.min())
and sorted
lst.sort(key=lambda x: sorted(x))
but neither works. could you please direct me a little bit. Thanks for your guidance.
Keep it simple, just use a list comprehension and sort the list inside the list:
Output:
[[1, 2, 3, 4], [5, 7, 8, 9], [0, 1, 2, 3, 9]]
So, with respect to your code, you can just do this:
lst = [sorted(x) for x in lst]
You can use a list comprehension to sort each sublist, then assign back to your main list
>>> lst = [sorted(i) for i in lst]
>>> lst
[[1, 2, 3, 4], [5, 7, 8, 9], [0, 1, 2, 3, 9]]
You could try something like this:
lst=[[1, 2, 3, 4], [5,9,8,7], [9, 0, 1, 2, 3 ]]
for x in lst:
x.sort()
I looped through lst and sorted each inner list.
You can do this:
>>> list(map(sorted, lst))
[[1, 2, 3, 4], [5, 7, 8, 9], [0, 1, 2, 3, 9]]
If you want to sort in place, you can do something like this:
any(ll.sort() for ll in lst)
(any is really only there to ditch the return values, which are all None; it will return false.)
You can write a function:
def sort_lst(lst):
lists = []
for l in lists:
l = l[:]
l.sort()
lists.append(l)
return lists
or
l = [[1, 2, 3, 6], [1, 2, 5, 6]]
l.sort(key=lambda x: x.sort())
print(l)
Output: [[1, 2, 3, 6], [1, 2, 5, 6]]
I do not understand the following anomalous behavior with lists in Python, and would appreciate it if someone could throw some light:
Snippet 1:
myList = [1,2,3,4]
A = [myList]*3
print(A)
myList[2]=45
print(A)
Output:
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
[[1, 2, 45, 4], [1, 2, 45, 4], [1, 2, 45, 4]]
This makes sense to me, since we did not perform an additional copy function to 'shield' A from element operations on myList.
Snippet 2:
myList = [1,2,3,4]
A = myList*3
print(A)
myList[2]=45
print(A)
Output:
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
Why is a change to myList not reflected in A?
In the first case, you're duplicating 3 references to myList directly. In the second case, you're duplicating 3 references to the contents of myList, which leaves you with no connection to the original myList.
I have a list of lists like this:
i = [[1, 2, 3], [2, 4, 5], [1, 2, 3], [2, 4, 5]]
I would like to get a list containing "unique" lists (based on their elements) like:
o = [[1, 2, 3], [2, 4, 5]]
I cannot use set() as there are non-hashable elements in the list. Instead, I am doing this:
o = []
for e in i:
if e not in o:
o.append(e)
Is there an easier way to do this?
You can create a set of tuples, a set of lists will not be possible because of non hashable elements as you mentioned.
>>> l = [[1, 2, 3], [2, 4, 5], [1, 2, 3], [2, 4, 5]]
>>> set(tuple(i) for i in l)
{(1, 2, 3), (2, 4, 5)}
i = [[1, 2, 3], [2, 4, 5], [1, 2, 3], [2, 4, 5]]
print([ele for ind, ele in enumerate(i) if ele not in i[:ind]])
[[1, 2, 3], [2, 4, 5]]
If you consider [2, 4, 5] to be equal to [2, 5, 4] then you will need to do further checks
You can convert each element to a tuple and then insert it in a set.
Here's some code with your example:
tmp = set()
a = [[1, 2, 3], [2, 4, 5], [1, 2, 3], [2, 4, 5]]
for i in a:
tmp.add(tuple(i))
tmp will be like this:
{(1, 2, 3), (2, 4, 5)}
Here's another way to do it:
I = [[1, 2, 3], [2, 4, 5], [1, 2, 3], [2, 4, 5]]
mySet = set()
for j in range(len(I)):
mySet = mySet | set([tuple(I[j])])
print(mySet)