This question already has answers here:
Unpacking, extended unpacking and nested extended unpacking
(4 answers)
Closed 3 years ago.
I'm trying to fundamentally understand while loops but don't understand assigning multiple variables on a single line of code.
total, x = 0, 1
What does the 1 mean? Where does the 1 belong? Plz help
total, x = 0, 1
it means :
total = 0
and
x = 1
You are basically assigning two variables simultaneously.
cat, dog = 'meow', 'woof'
is the same as:
cat = 'meow'
dog = 'woof'
You can use this method when you have a function that needs to return multiple variables.
def my_func(text):
return text, text.upper()
(original, edited) = my_func('hello')
print(original)
print(edited)
>>> hello
>>> HELLO
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:
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:
How do I create variable variables?
(17 answers)
Closed 6 years ago.
I have a list of variables in Python:
a = 1
b = 0
c = 0
These come from part of a script that gets user input for certain fields to process, so 1 means "do something to 'a'" and 0 means "do not include." So it would be useful to know which fields need to be used later in the script, i.e. which of them equal 1.
What's the best way to find which of a, b or c is equal to 1?
I tried this method (from here):
test_list = [a,b,c]
next((x for x in test_list if x == 1), None)
Which returns 1, but I'm looking for it to return a. This is precondition, the next step in the script takes the variables that equal 1 and does something with them, by variable name rather than value.
I could do it long-hand like this:
if a == 1:
print "a"
if b == 1:
...
but I suspect there's a nice Pythonic way to find the name of a variable from a list of variables that equals a specific value.
You should consider using a dictionary (a mapping) instead of a list, as names only reference objects but are not the objects themselves:
d = dict(a = 1, b = 2, c = 0)
print(next((k for k, v in d.items() if v == 1), None))
# a
This question already has answers here:
setting user input to variable name [duplicate]
(2 answers)
Closed 6 years ago.
I have a large amount of data in variable form:
abc_123 = 5
def_456 = 7
ghi_789 = 9
etc.
I ask the user for various inputs, which builds up a string. For example:
temp = "abc_123"
How do I make it so that temp = abc_123, thus making temp = 5?
Make a dictionary and then look up the values by key:
>>> d = {"abc_123": 5, "def_456": 7, "ghi_789": 9}
>>> temp = "abc_123"
>>> d[temp]
5
>>> d.get("invalid", "not found")
'not found'