+ 2
Need help with my python program
I have implemented a program but I am not able to use the pop() method with my list. Please look at the code for better explanation. Thank you! https://code.sololearn.com/cSd4arJZp3Rd/?ref=app
20 Respostas
+ 7
# this one should be at least as much efficient and more 'pythonic' / short:
def array_diff(a, b):
return list(filter(lambda v: v not in b,a))
x = [1, 2, 2, 3]
z = [2]
print(array_diff(x, z))
+ 5
Bahhaš§ , i have some doubts with your code, but maybe you can explain it.
your code produces this output:
[1, 2, 3]
when giving input:
x = [1, 2, 2, 2, 3]
z = [2]
but should be:
[1, 3]
+ 5
Bahhaš§ ,
as visph already mentioned, using set for this task is not recommended as set does not allow duplicates.
an other issue by using sets is, that it not may keep the initial order as described by the op.
using:
x = [1, 2, 2, 3, 0]
z = [2]
should return [1, 3, 0]
but it resulted to:
[0, 1, 3]
+ 3
rather use:
def array_diff(a, b):
if len(a) == 0:
return []
elif len(b) == 0:
return a
else:
for v in b:
while v in a:
a.remove(v)
return a
+ 2
lst1 = [1,2,3,4,5]
lst2 = [3,4]
it should return [1,2,5] ?
+ 2
ā³AsterisKā³ Thank you for the reply. I have tried that on line 27 after the "if b[i]== a[j]" condition it gives me error says, " int object is not subscriptable"
+ 2
much efficient solution would be to build a new list and return it, until you want to modify the list in place (in wich case, that's not required to return it)
+ 2
Bahhaš§ Thank you for the explanation now I got it.
+ 2
def array_diff(a,b):
for i in b:
while i in a:
try:
a.remove(i)
except ValueError:
return[]
return a
a=[1,3,3,4]
b=[1,3]
print(array_diff(a,b))
#Alternate solution by me
+ 2
Bahhaš§ in my last post, I talked about your 'set' solution wich fail for:
x = [1,1,2,2,3]
z = [2]
as result would be: [1,3] but expected is [1,1,3] ^^
+ 2
Bahhaš§ I see...
however the code name as well as the function name seems to me enough explicit ;)
+ 1
lisa Yes,
Some test cases examples
array_diff([1,2], [1]), [2], "a was [1,2], b was [1], expected [2]")
array_diff([1,2,2], [1]), [2,2], "a was [1,2,2], b was [1], expected [2,2]")
array_diff([1,2,2], [2]), [1], "a was [1,2,2], b was [2], expected [1]")
array_diff([1,2,2], []), [1,2,2], "a was [1,2,2], b was [], expected [1,2,2]")
array_diff([], [1,2]), [], "a was [], b was [1,2], expected []")
array_diff([1,2,3], [1, 2]), [3], "a was [1,2,3], b was [1, 2], expected [3]")
+ 1
Use a = a.pop(a[j])
+ 1
you are using range() when the length of the list changes with pop(), it will go out of index range.
+ 1
visph Thanks this one seems much efficient
+ 1
Bahhaš§ your 'set' solution would fail for:
x = [1,1,2,2,3]
z = [2]
as result would be: [1,3] but expected is [1,1,3] ^^
+ 1
visph
wow thank you so much guys, from the factor of O(n) your solution seems much faster and efficient.
+ 1
Scooby you doesn't need the try..except statement, as you remove value only 'while i in a'...
def array_diff(a,b):
for i in b:
while i in a:
a.remove(i)
return a
that's what I suggested in my first post, without removing the unuseful first tests ;P
+ 1
Scooby Here's a possible solution:
def arr_dif(a, b): return [x for x in a if x not in b]
# Hope this helps
- 3
Hi