Re: How Do You Replace Variables With Their Values?
dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main
Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']}
# Don't ask where I got the dinner from
for meal in dinner.keys():
exec(meal.replace(' ','_') + ' = list(dinner[meal])')
print(Starters)
print(Main_Course)
print(Desert)
OUTPUT:
['Fried Calamari', 'Potted crab']
['Fish', 'Meat']
['Cake', 'Banana Split']
On Thu, Jul 11, 2019 at 10:34 AM Ben Finney
wrote:
> Terry Reedy writes:
>
> > On 7/10/2019 6:37 PM, CrazyVideoGamez wrote:
> >
> > > and I'm just changing it with the code above (you can find that by
> > > printing it out). How can I make separate lists called 'Starters',
> > > 'Main Course', and 'Desert'?
> >
> > starters = dinner['Starters']
> > main_course = dinner['Main Course']
> > desert = dinner['Desert']
>
> The question comes, though, why you (CrazyVideoGamez) are doing this.
>
> You have the lists immediately accesible as dictionary elements, by
> name.
>
> Why do you need to also have them bound to separate names; what problem
> are you trying to solve that you think this will help?
>
> --
> \ “If [a technology company] has confidence in their future |
> `\ ability to innovate, the importance they place on protecting |
> _o__) their past innovations really should decline.” —Gary Barnett |
> Ben Finney
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
https://mail.python.org/mailman/listinfo/python-list
Seeking help regarding Python code
Hi, I have a dataset like this: RecTime NO2_RAW NO2 LAQN_NO2 10980 06/6/19 01:45 17.9544 53.4626 17.7 10981 06/6/19 01:45 17.9444 53.4434 17.7 10982 06/6/19 01:45 17.9211 53.3988 17.7 I want to calculate the difference between LAQN_NO2 and NO2_RAW everyday at 04:00 and add that value to NO2_RAW values in all rows on a particular day. I have 2 months of data and I want to do this for each day. So, I want a table like this RecTime NO2_RAW NO2 LAQN_NO2 NO2_Cal 10980 06/6/19 01:45 17.9544 53.4626 17.7 (LAQN_NO2-NO2_RAW)+NO2_RAW 10981 06/6/19 01:45 17.9444 53.4434 17.7 10982 06/6/19 01:45 17.9211 53.3988 17.7 Can someone please help? Thanks Debasree -- https://mail.python.org/mailman/listinfo/python-list
Re: Seeking help regarding Python code
Hi, Afraid the formatting gremlins got to your data before we saw it, so I am taking a guess at what you want to achieve. On 11/07/19 06:54, Debasree Banerjee wrote: I want to calculate the difference between LAQN_NO2 and NO2_RAW everyday at 04:00 and add that value to NO2_RAW values in all rows on a particular day. I have 2 months of data and I want to do this for each day. What is the difference value at 04:00? Your times all appear to be the same. You doubt you mean you run the program at 04:00 every day, because that will not update any rows that don't yet exist, and anyway NO2_RAW + (LAQN_NO2 - NO2_RAW) simply equals LAQN_NO2. Once calculated, I would either store it in a new field (and add to NO2_RAW on every use), or copy the whole day's dataset to a new table. That way, should you have to re-run, you have not lost the old value of NO2_RAW. Writing the code will come after you understand exactly what to achieve, and how a computer could do it. Regards Ian (Another one). -- https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
On 7/11/2019 12:51 AM, Aldwin Pollefeyt wrote:
dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main
Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']}
# Don't ask where I got the dinner from
for meal in dinner.keys():
exec(meal.replace(' ','_') + ' = list(dinner[meal])')
If dinner came from an untrusted source, and the OP does not say
differently, this would be a stupid thing to do.
--
Terry Jan Reedy
--
https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
On 11/07/2019 05:51, Aldwin Pollefeyt wrote:
dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main
Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']}
# Don't ask where I got the dinner from
for meal in dinner.keys():
exec(meal.replace(' ','_') + ' = list(dinner[meal])')
print(Starters)
print(Main_Course)
print(Desert)
OUTPUT:
['Fried Calamari', 'Potted crab']
['Fish', 'Meat']
['Cake', 'Banana Split']
If you think you need to do this, you are almost certainly wrong. Ew!
--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
On Fri, Jul 12, 2019 at 4:37 AM Terry Reedy wrote:
>
> On 7/11/2019 12:51 AM, Aldwin Pollefeyt wrote:
> > dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main
> > Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']}
> >
> > # Don't ask where I got the dinner from
> >
> > for meal in dinner.keys():
> > exec(meal.replace(' ','_') + ' = list(dinner[meal])')
>
> If dinner came from an untrusted source, and the OP does not say
> differently, this would be a stupid thing to do.
>
Yes, but if your dinner came from an untrusted source, you have bigger
problems than remote code execution exploits.
But exec is a bad idea even without security concerns. Python has FAR
better metaprogramming features (mutating globals() comes to mind
here) - but even that's probably not necessary. Needs more info from
the OP.
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Seeking help regarding Python code
On Thu, 11 Jul 2019 06:54:21 +0100, Debasree Banerjee wrote: > > I have a dataset like this: > > RecTime > > NO2_RAW > > NO2 > > LAQN_NO2 > > 10980 > > 06/6/19 01:45 > > 17.9544 [snip] > > Can someone please help? Your question might appear intelligibly on the mailing list (I can't tell), but it is mangled into unintelligibility on the newsgroup. My guess is that instead of double-spaced mixed-type data fields, you intended to present an input table like this: RecTime NO2_RAW NO2 LAQN_NO2 10980 06/6/19 01:45 17.9544 53.4626 17.7 10981 06/6/19 01:45 17.9444 53.4434 17.7 10982 06/6/19 01:45 17.9211 53.3988 17.7 I really can't tell what you want the output to look like. -- To email me, substitute nowhere->runbox, invalid->com. -- https://mail.python.org/mailman/listinfo/python-list
Problem to delete or modify Python program and to read "signature.asc"
Dear Sir/ Madam, 1. I try to modify, some of the check boxes can not be selected. 2. I try to uninstall, "successfully uninstall" message is come out. When I exit it "if you have any problem, please contact [email protected] " message is come out. I install Python 3.7.2 at my laptop in Windows 10 operating system. I cannot get reply yet. I saw "signature.asc" and I cannot read Best Regards, Kyi -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem to delete or modify Python program and to read "signature.asc"
On 12/07/19 1:52 PM, Hla Kyi via Python-list wrote: Dear Sir/ Madam, 1. I try to modify, some of the check boxes can not be selected. 2. I try to uninstall, "successfully uninstall" message is come out. When I exit it "if you have any problem, please contact [email protected] " message is come out. I install Python 3.7.2 at my laptop in Windows 10 operating system. I cannot get reply yet. I saw "signature.asc" and I cannot read Best Regards, Kyi Not true. I replied within one hour of your post, yesterday. <<< Please describe the problem you are having when you say "modify". It would also help if you identified your PC's operating system... >>> The information you have given is insufficient to identify a particular problem. -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
Aldwin Pollefeyt writes:
> dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main
> Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']}
>
> # Don't ask where I got the dinner from
>
> for meal in dinner.keys():
> exec(meal.replace(' ','_') + ' = list(dinner[meal])')
>
> print(Starters)
> print(Main_Course)
> print(Desert)
Why do you think this is needed? Why (there may be some reason! but you
have not told us what that is) can your program not just::
print(dinner['Starters'])
print(dinner['Main Course'])
print(dinner['Desert'])
> OUTPUT:
> ['Fried Calamari', 'Potted crab']
> ['Fish', 'Meat']
> ['Cake', 'Banana Split']
The above code produces this output, without any need for binding new
names. So what is it you are actually trying to achieve, and why do you
think the new bindings are necessary?
--
\“The number of UNIX installations has grown to 10, with more |
`\ expected.” —Unix Programmer's Manual, 2nd Ed., 1972-06-12 |
_o__) |
Ben Finney
--
https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
Wow, I'm so sorry I answered on the question : "How do you replace a
variable with its value". For what i understood with the example values,
CrazyVideoGamez wants 3 variables named like the meal-names in dictionary.
Yes, it's not secure unless you work with your own dataset (just like
sending your own created commands with set=True in subprocess). Yes there
might be better solutions for the real problem. But maybe the user really
has a purpose for it, in a secure environment with own datatset, it's a
valid answer for "How do you replace a variable with its value".
On Fri, Jul 12, 2019 at 12:10 PM Ben Finney
wrote:
> Aldwin Pollefeyt writes:
>
> > dinner = {'Starters':['Fried Calamari', 'Potted crab'],'Main
> > Course':['Fish', 'Meat'], 'Desert':['Cake', 'Banana Split']}
> >
> > # Don't ask where I got the dinner from
> >
> > for meal in dinner.keys():
> > exec(meal.replace(' ','_') + ' = list(dinner[meal])')
> >
> > print(Starters)
> > print(Main_Course)
> > print(Desert)
>
> Why do you think this is needed? Why (there may be some reason! but you
> have not told us what that is) can your program not just::
>
> print(dinner['Starters'])
> print(dinner['Main Course'])
> print(dinner['Desert'])
>
> > OUTPUT:
> > ['Fried Calamari', 'Potted crab']
> > ['Fish', 'Meat']
> > ['Cake', 'Banana Split']
>
> The above code produces this output, without any need for binding new
> names. So what is it you are actually trying to achieve, and why do you
> think the new bindings are necessary?
>
> --
> \“The number of UNIX installations has grown to 10, with more |
> `\ expected.” —Unix Programmer's Manual, 2nd Ed., 1972-06-12 |
> _o__) |
> Ben Finney
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
https://mail.python.org/mailman/listinfo/python-list
Re: How Do You Replace Variables With Their Values?
On Fri, Jul 12, 2019 at 2:30 PM Aldwin Pollefeyt
wrote:
>
> Wow, I'm so sorry I answered on the question : "How do you replace a
> variable with its value". For what i understood with the example values,
> CrazyVideoGamez wants 3 variables named like the meal-names in dictionary.
> Yes, it's not secure unless you work with your own dataset (just like
> sending your own created commands with set=True in subprocess). Yes there
> might be better solutions for the real problem. But maybe the user really
> has a purpose for it, in a secure environment with own datatset, it's a
> valid answer for "How do you replace a variable with its value".
>
What you gave was dangerous advice, and yes, there IS a better
solution - and an easier one. If you want to create variables
dynamically, then just create them!
for meal, parts in dinner.items():
globals()[meal.replace(' ','_')] = dinner[meal]
Python has a rich set of metaprogramming tools. Don't just always
reach for exec and caveat it with "it's okay if you trust everything".
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
