Re: learning and experimenting python.
Nice information. Thanks a lot. Krunalkumar Shah -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
Look I am not feeding trolls. Please be aware. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
It is moderatable. You can delete your all messages except topics. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
I am not feeding the troll. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
You are wasting our time instead of learning python. On Dec 31, 2016 2:09 PM, wrote: > It is moderatable. You can delete your all messages except topics. > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
On 30/12/2016 17:50, [email protected] wrote: Hello everyone, I am the new comer and learner of python. I have a doubt that when I type python and press enter it shows a prompt like But why it is >>> ? Is there any special reason? Why it is not setted as @,& or any other special characters? "I have a doubt" is not the correct way to ask this kind of question in English. Doubt is used to mean lack of conviction or feeling of uncertainty so your question means "I feel uncertain that when I type Python the prompt will be >>>" and from the further responses this is not what you mean. "I have a question about the prompt, why is it >>>" is far, far better. HTH -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
Thanks for suggesting me how to ask. Thanks a lot. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
Sorry for that. -- https://mail.python.org/mailman/listinfo/python-list
RE: Cleaning up conditionals
On Sat, 31 Dec 2016 11:26 am, Deborah Swanson wrote: > As Mr. Bieber points out, what I had above greatly benefits from the use > of conjunctions. It now reads: > > if not len(l1[st]) and len(l2[st]): > l1[st] = l2[st] > elif not len(l2[st]) and len(l1[st]): > l2[st] = l1[st] Your code could do with more descriptive variable names, but keeping your original names for the moment: if not l1[st] and l2[st]: # first string is empty, second is not empty l1[st] = l2[st] elif not l2[st] and l1[st]: # second string is empty, first is not empty l2[st] = l1[st] The comments are, of course, redundant, and I probably wouldn't bother putting them in my own code. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
On Sat, 31 Dec 2016 07:27 pm, [email protected] wrote: > It is moderatable. You can delete your all messages except topics. What *are* you talking about? Do you realise that your messages go to at least one mailing list ([email protected]), at least two news groups (comp.lang.python and gmane.comp.python.general), Google Groups, and at least one other web archive (ActiveState)? Not to mention tens of thousands of people's private archives in their own email or news client. You are flooding this forum with dozens of short messages with no context. This is not Twitter. Don't just hit send for every trivial thought that enters your mind. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
On Sat, 31 Dec 2016 07:30 pm, mm0fmf wrote: > On 30/12/2016 17:50, [email protected] wrote: >> Hello everyone, >> I am the new comer and learner of python. >> I have a doubt [...] > "I have a doubt" is not the correct way to ask this kind of question in > English. But it is perfectly idiomatic Indian English, which is no less legitimate than American English, Australian English, South African English and of course the hundreds of dialects from the mother country, British English. In Irish and Australian English, we're just as likely to say "I'm wondering about..." rather than "I have a question". This is an international forum, and we should make allowances for variant forms of English and those whose grasp of the Queen's English is not perfect. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
That's true. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
I will You can't stop me. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
On 31/12/16 05:22, Wildman via Python-list wrote: On Fri, 30 Dec 2016 19:23:17 -0700, Michael Torrie wrote: On 12/30/2016 07:05 PM, Wildman via Python-list wrote: On Fri, 30 Dec 2016 23:39:43 +, Erik wrote: Do not feed the troll. E. Please explain how what I said is trolling. Perhaps it was a little snide but I tend to get that way when trying to explain the obvious. Hmm. I thought he was referring to einstein1410... It was his message he was replying to, not yours, and I took it as a request to the rest of us (sorry, Erik--couldn't resist posting). I took it to mean that he was telling einstein1410 to not feed the trolls. If I am wrong then my apologies to Erik. Yes, my message was to everyone, using a quote from 'einstein1410' that I thought demonstrated the point. The vast majority of the messages in this thread are from 'einstein' nd are short, nonsensical retorts to others who are trying to be helpful, that just seem designed to get a further response and nothing more. Whether or not it's on purpose, he's just trolling. I may be wrong, but it just looks like the result of a bet to me - perhaps there's a similar thread on a Perl group somewhere and whoever gets the most replies wins. E. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
On 31/12/2016 09:58, Steve D'Aprano wrote: On Sat, 31 Dec 2016 07:30 pm, mm0fmf wrote: On 30/12/2016 17:50, [email protected] wrote: Hello everyone, I am the new comer and learner of python. I have a doubt [...] "I have a doubt" is not the correct way to ask this kind of question in English. But it is perfectly idiomatic Indian English, which is no less legitimate than American English, Australian English, South African English and of course the hundreds of dialects from the mother country, British English. In Irish and Australian English, we're just as likely to say "I'm wondering about..." rather than "I have a question". This is an international forum, and we should make allowances for variant forms of English and those whose grasp of the Queen's English is not perfect. Hence the suggestion to use a less idiomatic expression. I see this from my team of programmers in our Bangalore office regularly. Just about all of them were surprised when it was pointed out. Given emails are sent amongst that team and offices around the world, when they expect local team memebers to reply they now use "doubt" and when they'd like a global response they use "question". A brief skim of the mail lets you see if it needs dealing with immediately. I've always found Australian English to be very similar to English English when written and only going "full down under" when spoken. Whereas American English is basically fewer words spelt differently. Though they do write road info upside down on the road surface, typically XING with PED underneath. I had to ask what XINGPED meant till my colleague driving said it was PEDXING, pedestrian crossing. Why write it the wrong way though? I'm not sure if this thread isn't a most excellent troll however. I see it's almost 2017 where you are Steve... have a good one. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
mm0fmf : > On 31/12/2016 09:58, Steve D'Aprano wrote: >> On Sat, 31 Dec 2016 07:30 pm, mm0fmf wrote: >> >>> On 30/12/2016 17:50, [email protected] wrote: >>> "I have a doubt" is not the correct way to ask this kind of question in >>> English. >> >> But it is perfectly idiomatic Indian English, > > Hence the suggestion to use a less idiomatic expression. I have said it before that every English-speaker in the world should be taught the international dialect of English, ie, Hollywood English. I can understand virtually every foreign accent of English in the world, but I have exceeding difficulties with the British accents. BTW, it's interesting that with Brexit, only Ireland is left to hold the candle for English in the EU. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
I warn you that I am not playing game here. Please be cautioned. And if you think so, why post here? Its seems that you are also interested in doing so. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
On Sat, Dec 31, 2016 at 10:27 AM, Dennis Lee Bieber wrote: > On Sat, 31 Dec 2016 12:04:57 +, mm0fmf declaimed the > following: > >>Though they do write road info upside down on the road surface, >>typically XING with PED underneath. I had to ask what XINGPED meant till >>my colleague driving said it was PEDXING, pedestrian crossing. Why write >>it the wrong way though? > > For those I've encountered with multiple lines of text, the lines are > placed in the order one sees them. > > Yes, in a helicopter they would be reversed, but from the view in a > car, one sees the "PED" first, with the "XING" becoming visible as the > "PED" begins to be blocked by the hood (or should I say "bonnet"). In the United States, ambulances often have their signage written backwards so that it appears normal in a rear view mirror. Do they do that in other countries? > -- > Wulfraed Dennis Lee Bieber AF6VN > [email protected]://wlfraed.home.netcom.com/ > > -- > https://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
On 31/12/2016 15:27, Dennis Lee Bieber wrote: On Sat, 31 Dec 2016 12:04:57 +, mm0fmf declaimed the following: Though they do write road info upside down on the road surface, typically XING with PED underneath. I had to ask what XINGPED meant till my colleague driving said it was PEDXING, pedestrian crossing. Why write it the wrong way though? For those I've encountered with multiple lines of text, the lines are placed in the order one sees them. Yes, in a helicopter they would be reversed, but from the view in a car, one sees the "PED" first, with the "XING" becoming visible as the "PED" begins to be blocked by the hood (or should I say "bonnet"). That could explain why there are so many crashes in the US, the drivers are looking at the road right in front of the hood/bonnet and not in the distance where you see XING and PED. I know some writing is written right to left and some is left to right and some is top to bottom but I've never come across bottom to top! ;-) -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
On Dec 31, 2016 1:48 AM, "mm0fmf" wrote: On 30/12/2016 17:50, [email protected] wrote: > Hello everyone, > I am the new comer and learner of python. > I have a doubt that when I type python and press enter it shows a prompt > like > >> But why it is >>> ? > Is there any special reason? > Why it is not setted as @,& or any other special characters? > > "I have a doubt" is not the correct way to ask this kind of question in English. Doubt is used to mean lack of conviction or feeling of uncertainty so your question means "I feel uncertain that when I type Python the prompt will be >>>" and from the further responses this is not what you mean. "Doubt" means "question" in Indian English. It's every bit as correct as using "chips" to mean "French fries". -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
On Dec 31, 2016 3:12 AM, wrote: That's true. Please include quoted context in your replies. I have no idea who or what you're responding to. -- https://mail.python.org/mailman/listinfo/python-list
Re: Cleaning up conditionals
Deborah Swanson wrote: > Here I have a real mess, in my opinion: [corrected code:] > if len(l1[st]) == 0: > if len(l2[st]) > 0: > l1[st] = l2[st] > elif len(l2[st]) == 0: > if len(l1[st]) > 0: > l2[st] = l1[st] > Anybody know or see an easier (more pythonic) way to do this? I need to > do it for four fields, and needless to say, that's a really long block > of ugly code. By "four fields", do you mean four values of st, or four pairs of l1, l2, or more elif-s with l3 and l4 -- or something else entirely? Usually the most obvious way to avoid repetition is to write a function, and to make the best suggestion a bit more context is necessary. -- https://mail.python.org/mailman/listinfo/python-list
Re: Cleaning up conditionals
Deborah Swanson writes: > Is it possible to use some version of the "a = expression1 if > condition else expression2" syntax with an elif? And for expression1 > and expression2 to be single statements? That's the kind of > shortcutting I'd like to do, and it seems like python might be able to > do something like this. I missed this question when I read the thread earlier. The answer is simply to make expression2 be another conditional expression. I tend to write the whole chain in parentheses. This allows multi-line layouts like the following alternatives: a = ( first if len(first) > 0 else second if len(second) > 0 else make_stuff_up() ) a = ( first if len(first) > 0 else second if len(second) > 0 else make_stuff_up() ) Expression1 and expression2 cannot be statements. Python makes a formal distinction between statements that have an effect and expressions that have a value. All components of a conditional expression must be expressions. A function call can behave either way but I think it good style that the calls in expresions return values. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
On 31/12/2016 15:46, Ian Kelly wrote: On Dec 31, 2016 1:48 AM, "mm0fmf" wrote: On 30/12/2016 17:50, [email protected] wrote: Hello everyone, I am the new comer and learner of python. I have a doubt that when I type python and press enter it shows a prompt like But why it is >>> ? Is there any special reason? Why it is not setted as @,& or any other special characters? "I have a doubt" is not the correct way to ask this kind of question in English. Doubt is used to mean lack of conviction or feeling of uncertainty so your question means "I feel uncertain that when I type Python the prompt will be " and from the further responses this is not what you mean. "Doubt" means "question" in Indian English. It's every bit as correct as using "chips" to mean "French fries". I could agree with you regarding "chips" but then we'd both be wrong. ;-) Other way round. Chips, meaning fried potato pieces as opposed to fried potato slices, were being made in the UK and Europe a long time before being named "French Fries" by Americans. "French Fries" are now made from reconsituted potato pulp extruded into shape as opposed to pieces of freshly cut potato. Although you do get US "French Fries" in the UK. UK chips are normally quite unlike "French Fries" served in US fast food style establishments. Typically on UK menus it will say "Fries" or "French Fries" if you are not getting chips. For the record, the best chips I've had come from The Netherlands, Belgium and Yorkishire, England in that order. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
We are not discussing here about English, But for python. Don't divert. Other wise someone thinks that I am doing this for getting more posts. -- https://mail.python.org/mailman/listinfo/python-list
OT: Re: learning and experimenting python.
On 31/12/2016 17:08, [email protected] wrote: We are not discussing here about English, But for python. Don't divert. Other wise someone thinks that I am doing this for getting more posts. Username einstein, asking bizarre questions, short responses with no included context. if this isn't trolling I'm a Dutchman. Tot ziens! p.s. Happy New Year to all on the nicest, most useful newsgroup still active on the net. -- https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
On Sat, Dec 31, 2016 at 12:08 PM, wrote: > We are not discussing here about English, > But for python. Don't divert. Other wise someone thinks that I am doing this > for getting more posts. > -- > https://mail.python.org/mailman/listinfo/python-list If you stay around a while, einstein, you will see that the group will wander off into (sometimes) interesting asides when the original question seems to have been played out. Its a form a creative ignoring. -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays -- https://mail.python.org/mailman/listinfo/python-list
RE: Cleaning up conditionals
Jussi Piitulainen wrote: > Sent: Saturday, December 31, 2016 8:30 AM > Deborah Swanson writes: > > > Is it possible to use some version of the "a = expression1 if > > condition else expression2" syntax with an elif? And for > expression1 > > and expression2 to be single statements? That's the kind of > > shortcutting I'd like to do, and it seems like python might > be able to > > do something like this. > > I missed this question when I read the thread earlier. The > answer is simply to make expression2 be another conditional > expression. I tend to write the whole chain in parentheses. > This allows multi-line layouts like the following alternatives: > > a = ( first if len(first) > 0 > else second if len(second) > 0 > else make_stuff_up() ) > > a = ( first if len(first) > 0 else > second if len(second) > 0 else > make_stuff_up() ) > > Expression1 and expression2 cannot be statements. Python > makes a formal distinction between statements that have an > effect and expressions that have a value. All components of a > conditional expression must be expressions. A function call > can behave either way but I think it good style that the > calls in expresions return values. Your second alternative is exactly what I was thinking should be possible in python, but I couldn't figure out how to do it. I'm rewriting the little function I wrote to use this form. Thanks also for the explanation of why expressions work in terniaries, but statements don't, and what the difference between statements and expressions is. -- https://mail.python.org/mailman/listinfo/python-list
RE: Cleaning up conditionals
> From: Tim Chase
> Sent: Saturday, December 31, 2016 12:41 PM
> On 2016-12-30 19:59, Deborah Swanson wrote:
> > Similar, actually the same as Cameron suggested. I really need to
> > revisit testing for empty. I probably rejected it early on for some
> > bad reason (you don't understand everything that goes wrong when
> > you're learning).
>
> If your data is anything like what I often get, it had spaces
> in it, and
>
> x = " "
> if x:
> print("yep")
> else:
> print("nope")
>
> prints "yep". So when processing data like this, I prefer to
> take a cleaning pass over everything to strip out
> leading/trailing whitespace before attempting to use any logic on it.
>
> -tkc
Good point. Like I said earlier, I don't remember the specific contexts
that testing for empty failed for me, but blank spaces in data that I
didn't realize would be there (or look for it) is a strong possibility.
In any event, I should use tests for empty more, and see if and why it
fails. Obviously, experienced python coders successfully use it all the
time.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Cleaning up conditionals
On 2016-12-30 19:59, Deborah Swanson wrote:
> Similar, actually the same as Cameron suggested. I really need to
> revisit testing for empty. I probably rejected it early on for some
> bad reason (you don't understand everything that goes wrong when
> you're learning).
If your data is anything like what I often get, it had spaces in it,
and
x = " "
if x:
print("yep")
else:
print("nope")
prints "yep". So when processing data like this, I prefer to take a
cleaning pass over everything to strip out leading/trailing
whitespace before attempting to use any logic on it.
-tkc
--
https://mail.python.org/mailman/listinfo/python-list
RE: Cleaning up conditionals
Jussi Piitulainen wrote: > Sent: Saturday, December 31, 2016 8:30 AM > Deborah Swanson writes: > > > Is it possible to use some version of the "a = expression1 if > > condition else expression2" syntax with an elif? And for > expression1 > > and expression2 to be single statements? That's the kind of > > shortcutting I'd like to do, and it seems like python might > be able to > > do something like this. > > I missed this question when I read the thread earlier. The > answer is simply to make expression2 be another conditional > expression. I tend to write the whole chain in parentheses. > This allows multi-line layouts like the following alternatives: > > a = ( first if len(first) > 0 > else second if len(second) > 0 > else make_stuff_up() ) > > a = ( first if len(first) > 0 else > second if len(second) > 0 else > make_stuff_up() ) > > Expression1 and expression2 cannot be statements. Python > makes a formal distinction between statements that have an > effect and expressions that have a value. All components of a > conditional expression must be expressions. A function call > can behave either way but I think it good style that the > calls in expresions return values. While I'm sure these terniaries will be useful for future problems, I couldn't make the second one work for my current problem. I got as far as: a = l1[v] if len(l1[v] > 0 else l2[v] if len(l2[v] > 0 else And didn't finish it because I couldn't see what a should be. I want it to be l2[v] if the first clause is true, and l1[v] if the second. If I was computing a value, this would work beautifully, but I don't see how it can if I'm choosing a list element to assign to. Maybe I just can't see it. -- https://mail.python.org/mailman/listinfo/python-list
Re: Cleaning up conditionals
On Sun, Jan 1, 2017 at 9:03 AM, Deborah Swanson wrote: > And didn't finish it because I couldn't see what a should be. I want it > to be > l2[v] if the first clause is true, and l1[v] if the second. If I was > computing a value, this would work beautifully, but I don't see how it > can if I'm choosing a list element to assign to. Maybe I just can't see > it. It's possible to select either l1 or l2 using an expression, and then subscript that with [v]. However, this does not usually make for readable code, so I don't recommend it. (l1 if whatever else l2)[v] = new_value ChrisA -- https://mail.python.org/mailman/listinfo/python-list
RE: Cleaning up conditionals
Peter Otten wrote:
> Deborah Swanson wrote:
>
> > Here I have a real mess, in my opinion:
>
> [corrected code:]
>
> > if len(l1[st]) == 0:
> > if len(l2[st]) > 0:
> > l1[st] = l2[st]
> > elif len(l2[st]) == 0:
> > if len(l1[st]) > 0:
> > l2[st] = l1[st]
>
> > Anybody know or see an easier (more pythonic) way to do
> this? I need
> > to do it for four fields, and needless to say, that's a really long
> > block of ugly code.
>
> By "four fields", do you mean four values of st, or four
> pairs of l1, l2, or
> more elif-s with l3 and l4 -- or something else entirely?
>
> Usually the most obvious way to avoid repetition is to write
> a function, and
> to make the best suggestion a bit more context is necessary.
>
I did write a function for this, and welcome any suggestions for
improvement.
The context is comparing 2 adjacent rows of data (in a list of real
estate listings sorted by their webpage titles and dates) with the
assumption that if the webpage titles are the same, they're listings for
the same property. This assumption is occasionally bad, but in far less
than one per 1000 unique listings. I'd rather just hand edit the data in
those cases so one webpage title is slightly different, than writing and
executing all the code needed to find and handle these corner cases.
Maybe that will be a future refinement, but right now I don't really
need it.
Once two rows of listing data have been identified as different dates
for the same property, there are 4 fields that will be identical for
both rows. There can be up to 10 (or even more) listings identical
except for the date, but typically I'm just adding a new one and want to
copy the field data from its previous siblings, so the copying is just
from the last listing to the new one.
Here's the function I have so far:
def comprows(l1,l2,st,ki,no):
ret = ''
labels = {st: 'st/co', ki: 'kind', no: 'notes'}
for v in (st,ki,no):
if len(l1[v]) == 0 and len(l2[v]) != 0:
l1[v] = l2[v]
elif len(l2[v]) == 0 and len(l1[v]) != 0:
l2[v] = l1[v]
elif l1[v] != l2[v]:
ret += ", " + labels[v] + " diff" if len(ret) > 0 else
labels[v] + " diff"
return ret
The 4th field is a special case and easily dispatched in one line of
code before this function is called for the other 3.
l1 and l2 are the 2 adjacent rows of listing data, with st,ki,no holding
codes for state/county, kind (of property) and notes. I want the
checking and copying to go both ways because sometimes I'm backfilling
old listings that I didn't pick up in my nightly copies on their given
dates, but came across them later.
ret is returned to a field with details to look at when I save the list
to csv and open it in Excel. The noted diffs will need to be reconciled.
I tried to use Jussi Piitulainen's suggestion to chain the conditionals,
but just couldn't make it work for choosing list elements to assign to,
although the approach is perfect if you're computing a value.
Hope this is enough context... ;)
D
--
https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
On Sun, 1 Jan 2017 06:34 am, Dennis Lee Bieber wrote: > On Sat, 31 Dec 2016 15:41:09 +, mm0fmf declaimed > the following: > > >>That could explain why there are so many crashes in the US, the drivers >>are looking at the road right in front of the hood/bonnet This fortunately is not a problem in Australia, where the drivers' eyes are firmly fixed at all times to the iPhone they are holding just below the steering wheel. >>and not in the >>distance where you see XING and PED. I know some writing is written > > For the most part, if those markings are in the distance, the effects > of foreshortening makes them illegible. You have to be in a few car > lengths before you can read them (and the upper line may still be obscured > by the vehicle in front of you -- so again the odds are that you will see > the lower "PED" first, then see the upper "XING"). I wonder whether there is any objective evidence for the usefulness of text written on the road surface, as opposed to road markings (the old-fashioned zebra crossing, stripes) and/or road signs. In Melbourne, at least in the areas I frequent, we don't have many examples of text written on the road surface myself. But when I do see them, I find them more distracting than helpful. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: [SPAM] RE: Cleaning up conditionals
On 2016-12-31 22:35, Deborah Swanson wrote:
Peter Otten wrote:
Deborah Swanson wrote:
> Here I have a real mess, in my opinion:
[corrected code:]
> if len(l1[st]) == 0:
> if len(l2[st]) > 0:
> l1[st] = l2[st]
> elif len(l2[st]) == 0:
> if len(l1[st]) > 0:
> l2[st] = l1[st]
> Anybody know or see an easier (more pythonic) way to do
this? I need
> to do it for four fields, and needless to say, that's a really long
> block of ugly code.
By "four fields", do you mean four values of st, or four
pairs of l1, l2, or
more elif-s with l3 and l4 -- or something else entirely?
Usually the most obvious way to avoid repetition is to write
a function, and
to make the best suggestion a bit more context is necessary.
I did write a function for this, and welcome any suggestions for
improvement.
The context is comparing 2 adjacent rows of data (in a list of real
estate listings sorted by their webpage titles and dates) with the
assumption that if the webpage titles are the same, they're listings for
the same property. This assumption is occasionally bad, but in far less
than one per 1000 unique listings. I'd rather just hand edit the data in
those cases so one webpage title is slightly different, than writing and
executing all the code needed to find and handle these corner cases.
Maybe that will be a future refinement, but right now I don't really
need it.
Once two rows of listing data have been identified as different dates
for the same property, there are 4 fields that will be identical for
both rows. There can be up to 10 (or even more) listings identical
except for the date, but typically I'm just adding a new one and want to
copy the field data from its previous siblings, so the copying is just
from the last listing to the new one.
Here's the function I have so far:
def comprows(l1,l2,st,ki,no):
ret = ''
labels = {st: 'st/co', ki: 'kind', no: 'notes'}
for v in (st,ki,no):
if len(l1[v]) == 0 and len(l2[v]) != 0:
l1[v] = l2[v]
elif len(l2[v]) == 0 and len(l1[v]) != 0:
l2[v] = l1[v]
elif l1[v] != l2[v]:
ret += ", " + labels[v] + " diff" if len(ret) > 0 else
labels[v] + " diff"
return ret
The 4th field is a special case and easily dispatched in one line of
code before this function is called for the other 3.
l1 and l2 are the 2 adjacent rows of listing data, with st,ki,no holding
codes for state/county, kind (of property) and notes. I want the
checking and copying to go both ways because sometimes I'm backfilling
old listings that I didn't pick up in my nightly copies on their given
dates, but came across them later.
ret is returned to a field with details to look at when I save the list
to csv and open it in Excel. The noted diffs will need to be reconciled.
I tried to use Jussi Piitulainen's suggestion to chain the conditionals,
but just couldn't make it work for choosing list elements to assign to,
although the approach is perfect if you're computing a value.
Hope this is enough context... ;)
D
Here's a slightly different way of doing it:
def comprows(l1, l2, st, ki, no):
ret = ''
labels = {st: 'st/co', ki: 'kind', no: 'notes'}
for v in (st, ki, no):
t = list({l1[v], l2[v]} - {''})
if len(t) == 1:
l1[v] = l2[v] = t[0]
elif len(t) == 2:
ret += ", " + labels[v] + " diff"
return ret[2 : ]
And here's a summary of what it does:
If l1[v] == l2[v], then {l1[v], l2[v]} will contain 1 string, otherwise
it'll contain 2 strings. Then remove any empty string.
If the set now contains 1 string, then either they were the same, or one
of them was empty; in either case, just make them the same.
On the other hand, if the set contains 2 strings, then report that they
were different.
--
https://mail.python.org/mailman/listinfo/python-list
Re: learning and experimenting python.
Joel Goldstick wrote: In the United States, ambulances often have their signage written backwards so that it appears normal in a rear view mirror. Do they do that in other countries? In NZ our fire engines have FIRE written both forwards and backwards on the front, so it's readable either way. Not sure about ambulances offhand, I'll check next time I see one. We also have CROSSING RAILWAY and BRIDGE LANE ONE signs written on the road surface. :-) -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: OT: Re: learning and experimenting python.
mm0fmf wrote: Username einstein, asking bizarre questions, short responses with no included context. if this isn't trolling I'm a Dutchman. I don't think he's trolling, I think he's using some kind of forum interface (Google Groups?) that displays whole threads together, making quoting less necessary. He seems to be under the impression that he will be able to delete all his trivial posts later, and no harm will have been done. To einstein1410: You will *not* be able to delete those posts, because they are being gatewayed to various archived mailing lists and news groups. You may be able to make them disppear from your view of the group, but they will remain in those archives. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
RE: learning and experimenting python.
Erik wrote: > Yes, my message was to everyone, using a quote from > 'einstein1410' that > I thought demonstrated the point. > > The vast majority of the messages in this thread are from > 'einstein' nd > are short, nonsensical retorts to others who are trying to be > helpful, > that just seem designed to get a further response and nothing more. > Whether or not it's on purpose, he's just trolling. > > E. I quite agree, though it's hard to say what einstein's problem is. He/She seems like a child to me, and that might explain why he/she doesn't recognize the differences between internet services: Twitter, mailing lists - moderated and not, newsgroups, etc - they're all "on the internet". He/She doesn't seem to know where he is or what behavior is appropriate to different services or even that there are differences, like someone with little contact with these environments, and I'd guess that Twitter is his/her model for how to do it. It's also likely that a child would have fantasies about the meaning and importance of unfamiliar things, and perhaps assume that some parent-like entity would understand, explain and clean up after him/her. -- https://mail.python.org/mailman/listinfo/python-list
RE: Cleaning up conditionals
Deborah Swanson wrote:
> Peter Otten wrote:
>> Deborah Swanson wrote:
>>
>> > Here I have a real mess, in my opinion:
>>
>> [corrected code:]
>>
>> > if len(l1[st]) == 0:
>> > if len(l2[st]) > 0:
>> > l1[st] = l2[st]
>> > elif len(l2[st]) == 0:
>> > if len(l1[st]) > 0:
>> > l2[st] = l1[st]
>>
>> > Anybody know or see an easier (more pythonic) way to do
>> this? I need
>> > to do it for four fields, and needless to say, that's a really long
>> > block of ugly code.
>>
>> By "four fields", do you mean four values of st, or four
>> pairs of l1, l2, or
>> more elif-s with l3 and l4 -- or something else entirely?
>>
>> Usually the most obvious way to avoid repetition is to write
>> a function, and
>> to make the best suggestion a bit more context is necessary.
>>
>
> I did write a function for this, and welcome any suggestions for
> improvement.
>
> The context is comparing 2 adjacent rows of data (in a list of real
> estate listings sorted by their webpage titles and dates) with the
> assumption that if the webpage titles are the same, they're listings for
> the same property. This assumption is occasionally bad, but in far less
> than one per 1000 unique listings. I'd rather just hand edit the data in
> those cases so one webpage title is slightly different, than writing and
> executing all the code needed to find and handle these corner cases.
> Maybe that will be a future refinement, but right now I don't really
> need it.
>
> Once two rows of listing data have been identified as different dates
> for the same property, there are 4 fields that will be identical for
> both rows. There can be up to 10 (or even more) listings identical
> except for the date, but typically I'm just adding a new one and want to
> copy the field data from its previous siblings, so the copying is just
> from the last listing to the new one.
>
> Here's the function I have so far:
>
> def comprows(l1,l2,st,ki,no):
> ret = ''
> labels = {st: 'st/co', ki: 'kind', no: 'notes'}
> for v in (st,ki,no):
> if len(l1[v]) == 0 and len(l2[v]) != 0:
> l1[v] = l2[v]
> elif len(l2[v]) == 0 and len(l1[v]) != 0:
> l2[v] = l1[v]
> elif l1[v] != l2[v]:
> ret += ", " + labels[v] + " diff" if len(ret) > 0 else
> labels[v] + " diff"
> return ret
>
> The 4th field is a special case and easily dispatched in one line of
> code before this function is called for the other 3.
>
> l1 and l2 are the 2 adjacent rows of listing data, with st,ki,no holding
> codes for state/county, kind (of property) and notes. I want the
> checking and copying to go both ways because sometimes I'm backfilling
> old listings that I didn't pick up in my nightly copies on their given
> dates, but came across them later.
>
> ret is returned to a field with details to look at when I save the list
> to csv and open it in Excel. The noted diffs will need to be reconciled.
>
> I tried to use Jussi Piitulainen's suggestion to chain the conditionals,
> but just couldn't make it work for choosing list elements to assign to,
> although the approach is perfect if you're computing a value.
>
> Hope this is enough context... ;)
At least the code into which I translate your description differs from the
suggestions you have got so far. The main differences:
- Look at the whole group, not just two lines
- If there is more than one non-empty value in the group don't change any
value.
from collections import defaultdict
def get_title(row):
return row[...]
def complete(group, label):
"""For every row in the group set row[label] to a non-empty value
if there is exactly one such value.
Returns True if values can be set consistently.
group is supposed to be a list of dicts.
>>> def c(g):
... gg = [{"whatever": value} for value in g]
... if not complete(gg, "whatever"):
... print("fixme", end=" ")
... return [row["whatever"] for row in gg]
>>> c(["", "a", ""])
['a', 'a', 'a']
>>> c(["", "a", "a"])
['a', 'a', 'a']
>>> c(["", "a", "b"])
fixme ['', 'a', 'b']
>>> c(["a"])
['a']
>>> c([''])
fixme ['']
"""
values = {row[label] for row in group}
has_empty = not min(values, key=len)
if len(values) - has_empty != 1:
# no value or multiple values; manual intervention needed
return False
elif has_empty:
for row in group:
row[label] = max(values, key=len)
return True
if __name__ == "__main__":
# read rows
rows = ...
# group rows by title
groups = collections.defaultdict(list)
for row in rows:
groups[get_title(row)].append(row)
LABELS = ['st/co', 'kind', 'notes']
# add missing values
for group in groups.values():
for label in LABELS:
complete(group, label)
# write rows
...
--
https://mail.python.org
RE: Cleaning up conditionals
Chris Angelico wrote: > Sent: Saturday, December 31, 2016 2:19 PM > > On Sun, Jan 1, 2017 at 9:03 AM, Deborah Swanson > wrote: > > And didn't finish it because I couldn't see what a should > be. I want > > it to be l2[v] if the first clause is true, and l1[v] if > the second. > > If I was computing a value, this would work beautifully, > but I don't > > see how it can if I'm choosing a list element to assign to. Maybe I > > just can't see it. > > It's possible to select either l1 or l2 using an expression, > and then subscript that with [v]. However, this does not > usually make for readable code, so I don't recommend it. > > (l1 if whatever else l2)[v] = new_value > > ChrisA I'm not sure I understand what you did here, at least not well enough to try it. What conditional can I do between the 2 rows of listings (the list names l1 and l2) that will give me which row has the value to copy from and which one is empty? I don't see how that can happen if you don't give the subscript [v] to each of l1 and l2, at a minimum. Unless python will distribute the [v] inside the preceding conditional? Amazing, if true, but then we still need what the conditional is. And what is new_value? It could be either l1[v] or l2[v], depending on which one is not empty, and I don't see how the answer magically pops into it. Not saying that it doesn't, but what should I call new_value in the real estate listings example I want to use it in? There are no new values in this situation, only values that need to be copied into their empty counterparts in the other row. (It may or may not help to read the synopsis of what I'm doing that I wrote up in my last post to Peter Otten.) No, it's not terribly readable, but I'm curious whether this actually works. D. -- https://mail.python.org/mailman/listinfo/python-list
RE: Cleaning up conditionals
Dennis Lee Bieber
> Sent: Saturday, December 31, 2016 7:29 PM
>
> On Sat, 31 Dec 2016 14:35:46 -0800, "Deborah Swanson"
> declaimed the following:
>
> >Here's the function I have so far:
> >
> >def comprows(l1,l2,st,ki,no):
> >ret = ''
> >labels = {st: 'st/co', ki: 'kind', no: 'notes'}
> >for v in (st,ki,no):
> >if len(l1[v]) == 0 and len(l2[v]) != 0:
> >l1[v] = l2[v]
> >elif len(l2[v]) == 0 and len(l1[v]) != 0:
> >l2[v] = l1[v]
> >elif l1[v] != l2[v]:
> >ret += ", " + labels[v] + " diff" if len(ret) > 0 else
> >labels[v] + " diff"
> >return ret
> >
>
> Now, out of all that, which is the most likely one to
> be activated? Granted, it's too early to be considering
> optimizations, but... If you expect the common situation to
> be the "l1 != l2", the order of your tests means you do two
> failing tests before getting to the most likely version.
> --
> Wulfraed Dennis Lee Bieber AF6VN
> [email protected]://wlfraed.home.netcom.com/
I see what you're saying, but actually it's fairly rare that l1[v] and
l2[v] are different. I just need to catch it and look at it when they
are. It usually means that either the spelling/typing in the listing was
wrong and my code didn't fix it, or in rarer cases (now), my code is
buggy. I probably won't ever be able to dispense with this test since
people are always typoing. They don't always copy from their previous
listings and sometimes it looks like they're trying to retype it from
memory, and messing up.
D.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Cleaning up conditionals
On Sun, Jan 1, 2017 at 2:58 PM, Deborah Swanson wrote: > I'm not sure I understand what you did here, at least not well enough to > try it. > > What conditional can I do between the 2 rows of listings (the list names > l1 and l2) that will give me which row has the value to copy from and > which one is empty? I don't see how that can happen if you don't give > the subscript [v] to each of l1 and l2, at a minimum. Unless python will > distribute the [v] inside the preceding conditional? Amazing, if true, > but then we still need what the conditional is. > > And what is new_value? It could be either l1[v] or l2[v], depending on > which one is not empty, and I don't see how the answer magically pops > into it. Not saying that it doesn't, but what should I call new_value > in the real estate listings example I want to use it in? There are no > new values in this situation, only values that need to be copied into > their empty counterparts in the other row. (It may or may not help to > read the synopsis of what I'm doing that I wrote up in my last post to > Peter Otten.) Start with this simple statement: foo[2] = "spam" Obviously this will subscript 'foo' with 2 and set that to the string "spam". Easy. But instead of the word 'foo', we could use any expression at all. def return_foo(): return foo return_foo()[2] = "spam" This is a little more surprising, but it does work. And if you can use a function call to provide the list, you can use anything else - even a conditional expression. (foo if True else [])[2] = "spam" It's perfectly legal, but I do NOT recommend it :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
RE: Cleaning up conditionals
On Sun, 1 Jan 2017 02:58 pm, Deborah Swanson wrote: >> It's possible to select either l1 or l2 using an expression, >> and then subscript that with [v]. However, this does not >> usually make for readable code, so I don't recommend it. >> >> (l1 if whatever else l2)[v] = new_value >> >> ChrisA > > I'm not sure I understand what you did here, at least not well enough to > try it. The evolution of a Python programmer :-) (1) Step One: the naive code. if condition: l1[v] = new_value else: l2[v] = new_value (2) Step Two: add a temporary variable to avoid repeating the assignment if condition: temp = l1 else: temp = l2 temp[v] = new_value (3) Step Three: change the if...else statement to an expression temp = l1 if condition else l2 temp[v] = new_value (4) Step Four: no need for the temporary variable (l1 if condition else l2)[v] = new_value -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: Cleaning up conditionals
Deborah Swanson writes: > Jussi Piitulainen wrote: >> Sent: Saturday, December 31, 2016 8:30 AM >> Deborah Swanson writes: >> >> > Is it possible to use some version of the "a = expression1 if >> > condition else expression2" syntax with an elif? And for >> > expression1 and expression2 to be single statements? That's the >> > kind of shortcutting I'd like to do, and it seems like python might >> > be able to do something like this. >> >> I missed this question when I read the thread earlier. The >> answer is simply to make expression2 be another conditional >> expression. I tend to write the whole chain in parentheses. >> This allows multi-line layouts like the following alternatives: >> >> a = ( first if len(first) > 0 >> else second if len(second) > 0 >> else make_stuff_up() ) >> >> a = ( first if len(first) > 0 else >> second if len(second) > 0 else >> make_stuff_up() ) >> >> Expression1 and expression2 cannot be statements. Python >> makes a formal distinction between statements that have an >> effect and expressions that have a value. All components of a >> conditional expression must be expressions. A function call >> can behave either way but I think it good style that the >> calls in expresions return values. > > While I'm sure these terniaries will be useful for future problems, I > couldn't make the second one work for my current problem. (Note that those two things are just different layouts for the exact same conditional expression.) > I got as far as: > > a = l1[v] if len(l1[v] > 0 else > l2[v] if len(l2[v] > 0 else (Parentheses needed, otherwise the first line is expected to be a whole statement and then the unfinished expression in it is considered malformed.) > And didn't finish it because I couldn't see what a should be. I want > it to be l2[v] if the first clause is true, and l1[v] if the second. > If I was computing a value, this would work beautifully, but I don't > see how it can if I'm choosing a list element to assign to. Maybe I > just can't see it. Do you here mean condition when you say clause? Then, if the first condition is true, any other condition is not considered at all. When you come to the final else-branch, you know that all conditions in the chain were false. I thought you originally wanted to keep l1[v] if it was non-empty, which is what the code here says, but this time your prose seems different. Anyhow, since you want a value when all conditions in the chain of conditions are false, you want a value to use when the field is empty in both records. To change nothing, store the old empty value back; or you can supply your own default here. With your particular conditions of non-emptiness, which is taken to be truth, you can achieve variations of this result with any of the following statements: w = ( l1[v] if len(l1[v]) > 0 else l2[v] if len(l2[v]) > 0 else l1[v] ) x = l1[v] if l1[v] else l2[v] if l2[v] else l1[v] y = l1[v] or l2[v] or l1[v] z = l1[v] or l2[v] The last one, which I originally suggested (and still prefer when otherwise appropriate), is subtly different from the others. That difference should be irrelevant. -- https://mail.python.org/mailman/listinfo/python-list
