This question already has answers here:
Compute list difference [duplicate]
(17 answers)
Closed 7 years ago.
I have two lists and I want to print the difference between them (if there is 1 difference, it should print "1". How can I fix that?
So what I have is:
a= ["1","2","3"]
b= ["1","4","5"]
The answer should be 2.
It depends on what do you mean by difference. If they are equal in length and you want to find out the difference, do:
c = [i for i in a if i not in b]
print len(c)
Use set:
print len(set(L1) - set(L2))
Test:
>>> L1 = [1,2,5]
>>> L2 = [8,1]
>>> len(set(L1) - set(L2))
2
Related
This question already has answers here:
What's the best way to return multiple values from a function? [duplicate]
(6 answers)
Alternatives for returning multiple values from a Python function [closed]
(14 answers)
Closed 2 years ago.
So i have a function that has to return 3 values, i haven't found a better way to do this other than returning a list. Is this code a good programming practice? And if not how to fix it.
Example function:
def func():
#code
return [a,b,c]
Main code:
#code
list = func()
k = list[0]
l = list[1]
m = list[2]
You can pack/unpack directly in python:
def func():
a = 1
b = 2
c = 3
return a, b, c
k, l, m = func()
This question already has answers here:
Python: Find a substring in a string and returning the index of the substring
(7 answers)
Closed 3 years ago.
Find out if word 'dog' is in a string.
I tried doing this code and i dont know where the error is .
y='dogaway'
for i in range(len(y)):
if y[i:i+2]=='dog':
x=x+1
print(x)
I expected output to be 1 but the actual ouput is 0.
You can use count.
y = 'dogaway'
print(y.count('dog')) # Outputs 1
or if you want to fix your code, you are just off by one in your splice:
y = 'dogaway'
x = 0
for i in range(len(y) - 3): # Make sure your range accounts for the look ahead
# In the future add a print to make sure it is doing what you expect:
# print(y[i:i + 2])
if y[i:i + 3] == 'dog': # Here you were off by 1
x = x + 1
print(x)
Even simpler:
if 'dog' in y:
...
You can use the in membership operator in Python.
'dog' in 'dogaway'
returns True
This question already has answers here:
Find min, max, and average of a list
(5 answers)
Closed 5 years ago.
Let's say I have the below list:
o =[[-0.90405713, -0.86583093, -0.14048125]]
How do I find out how positive each element of o[0] is?
So,by looking at this I know that -0.14048125 is the most "positive" with respect to 0 on the number line. Is there a way to do this via a python code?
If you want the value closest to 0, you could use min with abs as key:
>>> o =[-0.90405713,-0.86583093,-0.14048125,3]
>>> min(o, key=abs)
-0.14048125
use max()
>>> o =[-0.90405713,-0.86583093,-0.14048125]
>>> max(o)
-0.14048125
This question already has answers here:
How does a for loop evaluate its argument
(3 answers)
Closed 6 years ago.
counter = Counter()
// fill data into counter
for a, b in counter.most_common():
if (b > 1):
counter[a] = np.log(b)
else:
counter[a] = -np.log((1 / (b+0.01)))
As I see, this is safe, based on my trial. No bad thing happens when I change the collection while I am enumerating it. In other languages, in each cycle of the for, the counter.most_common() value is evaluated.
Doesn't this happen in Python as well?
No, it doesn't. A more illustrative example:
def example():
print("Ping")
return [1,2,3,4]
for x in example():
print(x)
Output:
Ping
1
2
3
4
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 9 years ago.
This might be a stupid question, but what exactly is the is function, and when would one use it?
From the context, i guess i could infer that it's equivalent to ==; but if that's the case, why have both? The Built-in Functions Reference shows nothing, and help(is) returns a SyntaxError.
is checks if the objects have the same identity. == only checks if they are equal.
>>> L1 = [1,2,3]
>>> L2 = [1,2,3]
>>> L1 is L2
False
>>> L1 == L2
True