This question already has answers here:
How do I find the duplicates in a list and create another list with them?
(42 answers)
Closed 2 years ago.
Example 1:
input: [1,3,2,1,3,5]
Output: [1,3]
def duplicateNumber(mylist):
??
Thanks
Next time at least make an honest effort, describe the problem you are facing and then ask a question :
def duplicateNumber(x):
_size = len(x)
repeated = []
for i in range(_size):
k = i + 1
for j in range(k, _size):
if x[i] == x[j] and x[i] not in repeated:
repeated.append(x[i])
return repeated
Related
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 1 year ago.
Here is my code.
import numpy as np
m = 2
n = 2
arr = [[0]*m]*n
for i in np.arange(n):
for j in np.arange(m):
print(i)
arr[i][j] = i
print(arr)
the output is:
0
0
1
1
[[1,1],[1,1]]
I do not understand why the output array is not [[0,0],[1,1]].
Please I am losing my mind.
Because [0]*m object is being repeated n times. Whatever your i you're changing the same object, so you see only the last change printed.
arr = [[0]*m for _ in range(n)] solves the problem
This question already has answers here:
Subtracting 2 lists in Python
(16 answers)
Closed 2 years ago.
So I want to be able to subtract a list to another list.
For example, if I had two lists:
x = [0,8,10]
y = [1,7,9]
I would like to be able to basically subtract y[i] - x[i] using a FOR LOOP.
So ultimately, inside the loop, it would go 1-0, 7-8, 9-10, and so on if the list is longer.
Additionally, would there be any way to check if all or only one of the differences are equal to each other?
Thank you.
Try this :
import operator
x = [0,8,10]
y = [1,7,9]
ans = list(map(operator.sub, y, x))
print(ans)
Output :
[1, -1, -1]
This question already has answers here:
How do you create different variable names while in a loop? [duplicate]
(9 answers)
Closed 2 years ago.
I want to declare variable name in sequence:
Something like x_1, x_2, x_3,......., x_5 and assign value with them in python.
Something like x_i = i^2.
n = 10
for i in range(1, n):
x_i = i**2
print(x_2)
Can someone please help?
You could use this but its not recommended:
n = 10
for i in range(1, n):
globals()[f'x_{i}'] = i**2
print(x_2) #4
This question already has answers here:
Algorithm to check if a number if a perfect number
(4 answers)
Closed 3 years ago.
I am trying to find perfect number with using a function in python but I want to do this without using any parameters in function and using double for loop.
I wonder that if it is possible. My example code is under the below. I would appreciate it if you help.
def perfectnumber():
sum = 0
#These numbers going to be our numbers which will be divided.
for j in range(1,1001):
#These numbers going to be our numbers which will divide first loop numbers(j)
for k in range(1,1001):
import math
def divisors(n):
divs = [1]
for i in range(2,int(math.sqrt(n))+1):
if n%i == 0:
divs.extend([i,n/i])
return list(set(divs))
def is_perfect_number(n):
return n == sum(divisors(n))
res = []
for i in range(2,1001):
if is_perfect_number(i):
res.append(i)
print(res)
This question already has answers here:
Sort a list to form the largest possible number
(9 answers)
Closed 6 years ago.
x = [9,2,1]
l=[]
for i in range(len(x)):
p = max(x)
l.append(p)
x.remove(p)
print(l)
b = int(''.join(str(n) for n in l))
print(b)
The answer I got is 921
But if the list is [9,2,11].This won't work.
x = [9,2,11]
from itertools import combinations
l=combinations(x, len(x))
print max(map(lambda k: int("".join(map(str,k))),l))
You can use combinations here.
Output:
9211