Adding amount of elements in nested lists [duplicate] - python

This question already has answers here:
Count all elements in list of arbitrary nested list without recursion
(3 answers)
Nested List and count()
(8 answers)
Closed 11 months ago.
I have this problem, I want to count how many integers in a list AND sub-lists.
I have this code but doesn't seem to be working:
x = [[1, 3], [0], [2, -4]]
count = 0pos = 0
for i in x[pos]:count = count + len(x[pos])pos = pos + 1
print(count)
Expected 5 because there are 5 elements.

I've edited your code to the following:
x = [[1, 3], [0], [2, -4]]
count = 0
for arr in x:
for element in arr:
count +=1
print(count)

Related

Iterate over multiple elements at once Python [duplicate]

This question already has answers here:
Iterate an iterator by chunks (of n) in Python?
(14 answers)
How to iterate over a list in chunks
(39 answers)
Closed 3 months ago.
If I have a list like l=[1,2,3,4,5,6]. If I do something like:
for i in l:
print(i)
It will print all element separately.
1
2
3
4
.
.
.
Is there a way to iterate simultaneously over multiple elements?
For example, if I want to iterate over each 3 elements
for ....:
print(..)
Should print:
1,2,3
4,5,6
So that in each iteration the variable that iterate the for will be a tuple of, in this case, 3 elements.
Iterate over indices, and step three at a time, then use a slice within the loop to get the desired values.
>>> l=[1,2,3,4,5,6]
>>> for i in range(0, len(l), 3):
... print(l[i:i+3])
...
[1, 2, 3]
[4, 5, 6]
>>>
To get exactly the required output you could do this:
lst = [1, 2, 3, 4, 5, 6]
for i in range(0, len(lst), 3):
print(*lst[i:i+3], sep=',')
Output:
1,2,3
4,5,6

ROTATION OF A MATRIX BY 90 degree CLOCKWISE [duplicate]

This question already has answers here:
List on python appending always the same value [duplicate]
(5 answers)
Creating a list of dictionaries results in a list of copies of the same dictionary
(4 answers)
Closed 3 years ago.
I was solving a question of matrix rotation by 90 degrees. In the problem, I am taking up a list k filled with 0s of the exact degree as passed by the user.
I have tried this following code:
def rotate(m):
k=[]
f=[]
print(m)
for i in range(0,len(m)):
f.append(0)
for i in range(0,len(m)):
k.append(f)
print(k)
for i in range(0,len(m)):
for j in range(0,len(m)):
print("REPLACING POSITION:",i,j )
t=m[i][j]
k[j][len(m)-i-1]=t
return (k)
print(rotate([[1,2],[3,4]]))
I expect the output:
[[1, 2], [3, 4]]
[[0, 0], [0, 0]]
REPLACING POSITION: 0 0
REPLACING POSITION: 0 1
REPLACING POSITION: 1 0
REPLACING POSITION: 1 1
[[3, 1], [4, 2]]
I am getting the output:
[[1, 2], [3, 4]]
[[0, 0], [0, 0]]
REPLACING POSITION: 0 0
REPLACING POSITION: 0 1
REPLACING POSITION: 1 0
REPLACING POSITION: 1 1
[[4, 2], [4, 2]]
Why does the last row keep on repeating? Please help.
Your first loop generates the list f.
Your next for loop generates the list k, which is meant to contain len(m) copies of list f. The problem is f (and most lists) are just pointers. So list k is actually a list of pointers to the same list f.
Thus all the modifications you make to elements of f in k are made to the same list.
One solution is to use a copy of list f when generating list k by using the slice operator:
for i in range(0,len(m)):
k.append(f[:])
Use f.copy() to get deep copy of the list.
def rotate(m):
k=[]
f=[]
print(m)
for _ in range(0,len(m)):
f.append(0)
for _ in range(0,len(m)):
k.append(f.copy())
for i in range(0,len(m)):
for j in range(0,len(m)):
print("REPLACING POSITION:",i,j )
t=m[i][j]
k[j][len(m)-i-1]=t
print(j,len(m)-i-1)
return k
print(rotate([[1,2],[3,4]]))

Remove every third element from a list [duplicate]

This question already has answers here:
How to remove/delete every n-th element from list?
(5 answers)
Closed 4 years ago.
In this code piece I print the index numbers in a list.
x = '1 2 3 4 67 8'
x = list(map(int, x.split()))
# something here with modulo??
print(x)
Output:
[1, 2, 3, 4, 67, 8]
I want to output
[1, 2, 4, 67]
So the 3th element, 6th, 9th etc...
Iterate the list and keep index value which is not multiple of 3
[value for key,value in enumerate(x,1) if key%3!=0 ]

Python: for-loop wrong output [duplicate]

This question already has answers here:
Removing elements from a List in Python
(3 answers)
Closed 6 years ago.
Can't understand why it missing some indexes in loop?
Index number 3 for example.
I just want to delete the unique elements this way. I know another one but I don't know what's wrong with this one (or me).
lst = [2, 7, 1, 9, 3, 5, 2, 1]
def check(lst):
result = lst
print(lst) #checking
for num in lst:
print("index:", num) #checking
print("num:", num) #checking
if lst.count(num) < 2:
print("count:", lst.count(num)) #checking
result.remove(num)
return(result)
print(check(lst))
Output
[2, 7, 1, 9, 3, 5, 2, 1]
index: 2
num: 2
index: 7
num: 7
count: 1
index: 9
num: 9
count: 1
index: 5
num: 5
count: 1
index: 1
num: 1
[2, 1, 3, 2, 1]
You're removing items from a list as you're iterating over it, which you shouldn't do.
(result = lst does NOT make a copy of lst; it creates a new name which refers to the same object.)
If you just want to remove duplicate elements, you can use a set (although you may lose your ordering):
lst = list(set(lst))

python - How to move back an iteration in a for loop list [duplicate]

This question already has answers here:
Making a python iterator go backwards?
(14 answers)
Closed 6 years ago.
Given the list: a = [1, 2, 3, 4 5] in a for loop, suppose that the current item is 2, and next item is 3. If some condition is true, how can I make the next item be 2 again, which means the iteration should continue from 2 again instead of 3?
a = [1, 2, 3, 4, 5]
for item in a:
print item
if condition:
do something, and go back to previous iterator
The output would be:
1
2
2
3
4
5
Beware of an infinite loop, and it's not very Pythonic.
i = 0
a = [1, 2, 3, 4, 5]
hasBeenReset = False
while i < len(a):
if a[i] == 3 and not hasBeenReset:
i = 1
hasBeenReset = True
print(a[i])
i += 1

Categories