l1 = [4, 8, 12,16,3,7,11,15,2,6,10,14,1,5,9,13]
output :
[4,8,12,16]
[3,7,5,11]
[1,6,10,14]
[1,5,9,13]
m =4
n=4
tmp = [[0]*m]*n
a = 0
for i in range(m):
for j in range(n):
tmp[i][j] = l1[a]
a += 1
not printing in required format.
What's wrong here ? Can you please help me.
You could achieve this with the following list of comprehension:
list1 = [4, 8, 12,16,3,7,11,15,2,6,10,14,1,5,9,13]
[list1[x:x+4] for x in list(range(0,len(list1),4))]
Output
[[4, 8, 12, 16], [3, 7, 11, 15], [2, 6, 10, 14], [1, 5, 9, 13]]
The issue with the code is the array initialization part.
tmp = [[0]*m]*n This will create a common memory address and whatever changes you are making to one index will be reflected to other index as well.
So to initialize the list you can use the following code
tmp = [[0 for _ in range(m)] for _ in range(n)]
The simple method is already shared by #Sebastien D
You just need to change the way you initialize your tmp matrix
tmp = [[[0] for _ in range(m)] for _ in range(n)]
Iterate through the initial list with step of m and every time append list of m elements to the final list where m is number of elements in each row.
t = []
for i in range(0, len(l1), m):
t.append(l1[i:i+m])
print(t)
Related
I am a try to do for loop print a list of the number one of the forward and other from the back end.
Example: input: [1,2,3,4,5,6,7,8], output: [1,8,2,7,3,6,5]
You can use modulo operator %:
lst = [1, 2, 3, 4, 5, 6, 7, 8]
out = []
for i in range(len(lst)):
out.append(lst[len(lst) - i // 2 - 1 if i % 2 else i // 2])
print(out)
Prints:
[1, 8, 2, 7, 3, 6, 4, 5]
Thank you for asking this question.
You can iterate in half of its range and print list[i] and list[len(list)-i-1].
-1 because backward indexing starts from 1.
box = []
while mylist:
x = mylist.pop(0)
box.append(x)
while mylist:
m = mylist.pop(-1)
box.append(m)
break
return box
def selection_sort(li):
new_list = []
a = li[0]
for x in range(1, len(li)):
if li[x] < a:
a = li[x]
new_list = a
print(new_list)
b = [1, 2, 5, 3, 7, 4]
selection_sort(b)
Why does the above code returns empty list.
Thank you
Learn what is selection sort using visualization. Do follow the steps how to use selection sort
def selection_sort(li):
for i in range(len(li)):
min_idx = i
for j in range(i+1, len(li)):
if li[min_idx] > li[j]:
min_idx = j
li[i], li[min_idx] = li[min_idx], li[i]
print(li)
b = [1, 2, 5, 3, 7, 4]
selection_sort(b)
Now, from your code perspective, your selection sort algorithm isn't correct. Furthermore, you don't need to initialize another list to store sort element rather your function parameter list is kind enough to store the sort element.
I have a 2-D list:
lst = [[1,2,3,4,5,6,7,8,9],[11,12,13,14,15]]
I want to store 0 to N-1 of each list in the 2-D list in a separate list and 1 to N in another one. So I create two new lists to be appended where they have the same length as the 2-D lst:
alpha, beta = [[]]*len(lst), [[]]*len(lst)
Then I run this code:
for i in range(len(lst)):
for j in range(len(lst[i])-1):
alpha[i].append(lst[i][j])
beta[i].append(lst[i][j+1])
But the for-loops seem to be iterating through all lists every time.
I want to get the result
alpha = [[1,2,3,4,5,6,7,8],[11,12,13,14]]
beta = [[2,3,4,5,6,7,8,9],[12,13,14,15]]
Instead, I am getting
alpha = [[1,2,3,4,5,6,7,8,11,12,13,14],[1,2,3,4,5,6,7,8,11,12,13,14]]
beta = [[2,3,4,5,6,7,8,9,12,13,14,15],[2,3,4,5,6,7,8,9,12,13,14,15]]
There is definitely something wrong with my code and I'm not able to figure it out, any help is appreciated!
I think list comprehensions might make your code more succinct:
alpha = [i[:-1] for i in lst]
beta = [i[1:] for i in lst]
>>> alpha
[[1, 2, 3, 4, 5, 6, 7, 8], [11, 12, 13, 14]]
>>> beta
[[2, 3, 4, 5, 6, 7, 8, 9], [12, 13, 14, 15]]
foo, bar= [], []
for i in range(len(lst)):
for j in range(len(lst[i])-1):
foo.append(lst[i][j])
bar.append(lst[i][j+1])
alpha[i] = foo
beta[i] = bar
foo, bar = [], []
I found this code from stackoverflow ... and wondering how can I move index position that I want.
I tried to use for loop and [::1]. And by making, len(a)*[0]...I couldn't make it.
Is there any way to fix items on its position in list?
Second, without using method below, is there another way to reorder items in list?
'''
mylist=['a','b','c','d','e']
myorder=[3,2,0,1,4]
'''
a = [1,2,3,4,5,6,7]
b = ((a+a[:0:-1])*len(a))[::len(a)][:len(a)]
[1, 7, 2, 6, 3, 5, 4] <=b
[7, 1, 6, 2, 5, 3, 4] <= the result i want
Thanks in advance.
I'm not sure if this is what you want:
someList = [1,2,3,4,5,6,7]
orderedList = sorted(someList)
reversedOrderedList = orderedList[::-1]
finalList = []
for i in range(len(someList)):
if i % 2 == 0:
finalList.append(reversedOrderedList[i//2])
else:
finalList.append(orderedList[i//2])
print(finalList)
output:
[7, 1, 6, 2, 5, 3, 4]
if so, you can write it in shorter way (without reversedOrderedList):
someList = [1,2,3,4,5,6,7]
orderedList = sorted(someList)
finalList = []
for i in range(len(someList)):
if i % 2 == 0:
finalList.append(orderedList[-1-i//2])
else:
finalList.append(orderedList[i//2])
print(finalList)
and from here you can write it without if statement:
someList = [1,2,3,4,5,6,7]
orderedList = sorted(someList)
for i in range(len(someList)):
finalList.append(orderedList[((-1)**(i%2+1)-1)//2 + ((- 1)**(i%2+1))*(i//2)])
print(finalList)
It is not pretty but after that you can easily write a generator.
Zip the list with its reversed version, flatten it and take the first half:
from itertools import chain
a = [1,2,3,4,5,6,7]
b = list(chain.from_iterable(zip(a[::-1], a)))
print(b[:len(b) // 2])
Output
[7, 1, 6, 2, 5, 3, 4]
for i in range(1, len(A)):
A[i] = A[i-1] + A[i]
You can't do that with a list comprehension as they don't allow assignments.
You can use a simple generator function:
def func(lis):
yield lis[0]
for i,x in enumerate(lis[1:],1):
lis[i] = lis[i-1] + x
yield lis[i]
>>> A = [1, 2, 3, 4, 5, 6, 7]
>>> list(func(A))
[1, 3, 6, 10, 15, 21, 28]
though less efficient, this does give the desired output. But I think I'm getting closer to O(n**2) on this one.
A = [sum(A[:i+1]) for i, _ in enumerate(A)]
afaik this can't be done with a list comprehension the way you want to. I would suggest using the for loop version you've provided. Even if it was possible with a list comprehension, there's no point when you can just modify the list in place.
Use B (another temporary variable).
This should do the trick.
def func(L):
it = iter(L)
v = it.next()
yield v
for x in it:
v += x
yield v
A = [1, 2, 3, 4, 5, 6, 7]
print list(func(A))
This creates an iterator that returns one value at the time. To get the full new list at once you need to use a list() call around the function call, like:
list(func(A))
This generator function should work on any iterable (also those that don't support getting value based on index, like L[0])
I don't think there's an efficient way to do this with a comprehension list.
A one-line solution use reduce:
>>> the_list = [1,2,3,4,5]
>>> reduce(lambda result, x: result+[result[-1] + x] ,the_list, [0])[1:]
[1, 3, 6, 10, 15]