This question already has answers here:
How do I put a variable’s value inside a string (interpolate it into the string)?
(9 answers)
Closed 3 years ago.
coming_monday = today + datetime.timedelta(days=-today.weekday(), weeks=1)
month = coming_monday.strftime("%B")
collection = db['empmain']
record = collection.find_one({'Availablity.<want to pass in the month variable here'})
format of the collection:
enter image description here
This example worked for me:
a = 1
b = 2
c = 3
mydict = {a : "a_", b : "b_", c : "c_"}
I've declard some variables with some values as above.
>>> mydict[1]
'a_'
>>> mydict[b]
'b_'
I was able to access values in the dictionary using both the variable names or the values.
Related
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed last year.
a = "Jack"
b = "Sam"
Now I want some way to create this:
c_{b value} = 10
That means:
c_Jack = 10
And the following:
c_{a value} = 20
That means:
c_Sam = 20
This is actually a bad practice to do this. You better to use a dict.
BTW, you can do this:
In [3]: a = "Jack"
In [4]: exec(f'c_{a} = 10')
In [5]: c_Jack
Out[5]: 10
This question already has answers here:
Edit the values in a list of dictionaries?
(4 answers)
Closed 1 year ago.
I have the following JSON, I want to be able to get the value for a certain key and update it accordingly.
So for example take a look at the key 'a' in the first JSON object, I want to be able to get its value '2' and then update the value for 'a' to whatever I want. It is important I can work with the value, incase I want to reformat date/time for example and update the key.
All help appreciated
x = "test" : [{
"a":"2",
"b":"12",
"c":"24",
"d":"223",
"e":"23",
},
{"a":"22",
"x":"24",
"c":"25",
"d":"21",
"e":"25",
},
{"a":"12",
"y":"23",
"c":"25",
"d":"23",
"e":"21",
}],
You could do this.
keyFor = 'a'
#get value
aVal = x['test'][0][keyFor]
#change value
aVal = int(aVal) + 2
#substitute in x
x['test'][0][keyFor] = aVal
print(x)
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 1 year ago.
I am attempting to set the value of a variable to incrementally equal that of another variable based on the iteration of the loop I am on.
I have the following code:
no_keywords=3
cat_0='Alpha'
cat_1='Bravo'
cat_2='Charlie'
cat_3='Delta'
for _ in range(no_keywords):
keyword1 = exec(f'cat_{_}')
print(keyword1)
However the printed value just returns a NoneType object. I would like the value of Keyword1 to take on the value of the cat_ variable based on the iteration of loop I am on.
Please can somebody help in explaining in what I am doing wrong and help me recitfy?
Thanks,
D
Try this:
no_keywords = 3
cat_0 = 'Alpha'
cat_1 = 'Bravo'
cat_2 = 'Charlie'
cat_3 = 'Delta'
for _ in range(no_keywords):
keyword1 = None
exec(f'keyword1 = cat_{_}')
print(keyword1)
Seems like exec does not return a value, but you can set your variable within the command passed to it.
This question already has answers here:
How to postpone/defer the evaluation of f-strings?
(14 answers)
Closed 3 years ago.
Let's suppose I get some string and set it to variable - it can't be created as f-string:
str1 = 'date time for {val1} and {val2}'
Then variables inside the string initialized:
val1 = 1
val2 = 77
Calling print(str1) will return 'date time for {val1} and {val2}'. But I would like to get
'date time for 1 and 77'
Is there any function to make a string as a F string? So I want to call something make_F(str1) and get f-string
PS I cant use dict {'val1':1, 'val2':77} with .format because don't know which variables will be needed in the string. I just want magic happen with F-string.
You need:
str1 = 'date time for {val1} and {val2}'
val1 = 1
val2 = 77
print(eval(f"f'{str1}'"))
You first need to describe variables as e.g
var1 = None
var2 = None
Then you can use it with f-string like this:
x = f'print {var1} and {var2}'
print(x)
And thats it, you will get the result.
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 5 years ago.
I am having a function in python
def func(dataFrame,country,sex):
varible_name=dataFrame[(dataFrame['land']==country) & (dataFrame['sex']==sex)]
Now, for example, I call this function
func(dataFrame,'England','M')
I want that variable name be England_M instead of variable_name.
You can't do that in Python.
What you can do instead is store the results under a dictionary with key = England_M for instance.
In your case, you could do the following :
def func(dataFrame,country,sex):
tmp = dataFrame[(dataFrame['land']==country) & (dataFrame['sex']==sex)]
variable_name = "{c}_{s}".format(c=country, s=sex)
return dict(variable_name=tmp)
Now using it :
results = func(dataFrame, "England", "M")
print(results['England_M'])