Re: how to append to list in list comprehension
On Saturday, 1 October 2016 14:17:06 UTC+10, Rustom Mody wrote: > On Saturday, October 1, 2016 at 9:08:09 AM UTC+5:30, Sayth Renshaw wrote: > > I do like [(f + ['0'] if len(f) < 5 else f) for f in fups ] Rustom, if > > there are better non list comprehension options I would like to know as > > generally I find then confusing. > > Two points here — best taken independently: > 1. List comprehensions are confusing > 2. When to want/not want them > > > For 1 I suggest you (privately) rewrite them with '|' for 'for' and '∈' for > 'in' > Once you do that they will start looking much more like the origin that > inspires > them — set builder notation: > https://en.wikipedia.org/wiki/Set-builder_notation > > From there I suggest you play with replacing '[]' with '{}' ie actually try > out set comprehensions and then others like dict-comprehensions — very nifty > and oft-neglected. And the mother of all — generator comprehensions. > > Of course to check it out in python you will need to invert the translation: > '|' for 'for' and '∈' for 'in' > the point of which is to use python as a kind of math assembly language > *into* which you *code* but not in which you *think* > > For 2 its important that you always keep in front of you whether you want to > approach a problem declaratively (the buzzword FP!) or imperatively. > Python is rather unique in the extent to which it allows both > This also makes it uniquely difficult because its all too easy to garble the > two styles as John's .append inside a LC illustrates. > > And the way to ungarble your head is by asking yourself the meta-question: > Should I be asking "How to solve this (sub)problem?" or more simply > "What is the (sub)problem I wish to solve?" > > How questions naturally lead to imperative answers; whats to declarative > > You may be helped with [plug!] my writings on FP: > http://blog.languager.org/search/label/FP > > Particularly the tables in: > http://blog.languager.org/2016/01/primacy.html Your insight has helped. May lack elegance but I have got it working. from lxml import etree import csv import re def clean(attr): p = re.compile('\d+') myList = p.findall(attr) if len(myList) < 5: myList.append('0') return myList[0], myList[1], myList[2], myList[3], myList[4] with open("20161001RAND0.xml", 'rb') as f, open( "output/310916RABD.csv", 'w', newline='') as csvf: tree = etree.parse(f) root = tree.getroot() race_writer = csv.writer(csvf, delimiter=',') for meet in root.iter("meeting"): for race in root.iter("race"): for nom in root.iter("nomination"): meetattr = meet.attrib raceattr = race.attrib nomattr = nom.attrib if nomattr['number'] != '0': print(clean(nomattr['firstup'])) Cheers Sayth -- https://mail.python.org/mailman/listinfo/python-list
Re: how to append to list in list comprehension
On Saturday, 1 October 2016 14:17:06 UTC+10, Rustom Mody wrote: > On Saturday, October 1, 2016 at 9:08:09 AM UTC+5:30, Sayth Renshaw wrote: > > I do like [(f + ['0'] if len(f) < 5 else f) for f in fups ] Rustom, if > > there are better non list comprehension options I would like to know as > > generally I find then confusing. > > Two points here — best taken independently: > 1. List comprehensions are confusing > 2. When to want/not want them > > > For 1 I suggest you (privately) rewrite them with '|' for 'for' and '∈' for > 'in' > Once you do that they will start looking much more like the origin that > inspires > them — set builder notation: > https://en.wikipedia.org/wiki/Set-builder_notation > > From there I suggest you play with replacing '[]' with '{}' ie actually try > out set comprehensions and then others like dict-comprehensions — very nifty > and oft-neglected. And the mother of all — generator comprehensions. > > Of course to check it out in python you will need to invert the translation: > '|' for 'for' and '∈' for 'in' > the point of which is to use python as a kind of math assembly language > *into* which you *code* but not in which you *think* > > For 2 its important that you always keep in front of you whether you want to > approach a problem declaratively (the buzzword FP!) or imperatively. > Python is rather unique in the extent to which it allows both > This also makes it uniquely difficult because its all too easy to garble the > two styles as John's .append inside a LC illustrates. > > And the way to ungarble your head is by asking yourself the meta-question: > Should I be asking "How to solve this (sub)problem?" or more simply > "What is the (sub)problem I wish to solve?" > > How questions naturally lead to imperative answers; whats to declarative > > You may be helped with [plug!] my writings on FP: > http://blog.languager.org/search/label/FP > > Particularly the tables in: > http://blog.languager.org/2016/01/primacy.html Thank You Rustom -- https://mail.python.org/mailman/listinfo/python-list
Re: how to append to list in list comprehension
On Saturday, October 1, 2016 at 9:08:09 AM UTC+5:30, Sayth Renshaw wrote: > I do like [(f + ['0'] if len(f) < 5 else f) for f in fups ] Rustom, if there > are better non list comprehension options I would like to know as generally I > find then confusing. Two points here — best taken independently: 1. List comprehensions are confusing 2. When to want/not want them For 1 I suggest you (privately) rewrite them with '|' for 'for' and '∈' for 'in' Once you do that they will start looking much more like the origin that inspires them — set builder notation: https://en.wikipedia.org/wiki/Set-builder_notation From there I suggest you play with replacing '[]' with '{}' ie actually try out set comprehensions and then others like dict-comprehensions — very nifty and oft-neglected. And the mother of all — generator comprehensions. Of course to check it out in python you will need to invert the translation: '|' for 'for' and '∈' for 'in' the point of which is to use python as a kind of math assembly language *into* which you *code* but not in which you *think* For 2 its important that you always keep in front of you whether you want to approach a problem declaratively (the buzzword FP!) or imperatively. Python is rather unique in the extent to which it allows both This also makes it uniquely difficult because its all too easy to garble the two styles as John's .append inside a LC illustrates. And the way to ungarble your head is by asking yourself the meta-question: Should I be asking "How to solve this (sub)problem?" or more simply "What is the (sub)problem I wish to solve?" How questions naturally lead to imperative answers; whats to declarative You may be helped with [plug!] my writings on FP: http://blog.languager.org/search/label/FP Particularly the tables in: http://blog.languager.org/2016/01/primacy.html -- https://mail.python.org/mailman/listinfo/python-list
Re: how to append to list in list comprehension
> > I want to go threw and for each index error at [4] append a 0. > > You want to append 0 if the list does not have at least 5 items? > > > p = re.compile('\d+') > > fups = p.findall(nomattr['firstup']) > > [x[4] for x in fups if IndexError fups.append(0)] > > print(fups) > > > Unsure why I cannot use append in this instance > > Because that's incorrect syntax. > > > how can I modify to acheive desired output? > > for f in fups: > if len(f) < 5: > f.append(0) > > Or, if you really want to use a list comprehension: > > [f.append(0) for f in fups if len(f) < 5] > > However there's no reason to use a list comprehension here. The whole > point of list comprehensions is to create a *new* list, which you don't > appear to need; you just need to modify the existing fups list. > > -- > John Gordon A is for Amy, who fell down the stairs B is for Basil, assaulted by bears > -- Edward Gorey, "The Gashlycrumb Tinies" You are right John in that I don't want a new list I just wish to modify in-place to acheive the desired output. I had no direct desire to use list comprehension just seemed an option. Ultimately once it works I will abstract it into a function for other lists that will have a similar issue. def listClean(fups) holder = [(f + ['0'] if len(f) < 5 else f) for f in fups ] return holder[0], holder[1], holder[2], holder[3], holder[4] and then call it in my csv.writer that I have, which currently errors quite correctly that it cannot write index[4] as some of my lists fail it. I do like [(f + ['0'] if len(f) < 5 else f) for f in fups ] Rustom, if there are better non list comprehension options I would like to know as generally I find then confusing. Cheers Sayth -- https://mail.python.org/mailman/listinfo/python-list
Re: how to append to list in list comprehension
On Saturday, October 1, 2016 at 7:48:19 AM UTC+5:30, John Gordon wrote: > In Sayth Renshaw writes: > > > I want to go threw and for each index error at [4] append a 0. > > You want to append 0 if the list does not have at least 5 items? > > > p = re.compile('\d+') > > fups = p.findall(nomattr['firstup']) > > [x[4] for x in fups if IndexError fups.append(0)] > > print(fups) > > > Unsure why I cannot use append in this instance > > Because that's incorrect syntax. > > > how can I modify to acheive desired output? > > for f in fups: > if len(f) < 5: > f.append(0) > > Or, if you really want to use a list comprehension: > > [f.append(0) for f in fups if len(f) < 5] Wrong fups = [['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0'], ['7', '2', '1', '0', '142647', '00'], ['7', '2', '0', '1', '87080', '00'], ['6', '1', '1', '1', '51700', '00'], ['4', '1', '1', '0', '36396', '00'] ] >>> [f.append(0) for f in fups if len(f) < 5] [None, None, None, None, None, None] Or right if you re-squint: >>> fups [['0', '0', '0', '0', 0], ['0', '0', '0', '0', 0], ['0', '0', '0', '0', 0], ['0', '0', '0', '0', 0], ['0', '0', '0', '0', 0], ['0', '0', '0', '0', 0], ['7', '2', '1', '0', '142647', '00'], ['7', '2', '0', '1', '87080', '00'], ['6', '1', '1', '1', '51700', '00'], ['4', '1', '1', '0', '36396', '00']] >>> Which is to say that imperative code — .append — inside a comprehension is a bad idea One comprehension way to do it would be: >>> [(f + ['0'] if len(f) < 5 else f) for f in fups ] [['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0'], ['7', '2', '1', '0', '142647', '00'], ['7', '2', '0', '1', '87080', '00'], ['6', '1', '1', '1', '51700', '00'], ['4', '1', '1', '0', '36396', '00']] -- https://mail.python.org/mailman/listinfo/python-list
Re: how to append to list in list comprehension
In <534d5506-1810-4a79-ac8f-95a664d17...@googlegroups.com> Sayth Renshaw writes: > I want to go threw and for each index error at [4] append a 0. You want to append 0 if the list does not have at least 5 items? > p = re.compile('\d+') > fups = p.findall(nomattr['firstup']) > [x[4] for x in fups if IndexError fups.append(0)] > print(fups) > Unsure why I cannot use append in this instance Because that's incorrect syntax. > how can I modify to acheive desired output? for f in fups: if len(f) < 5: f.append(0) Or, if you really want to use a list comprehension: [f.append(0) for f in fups if len(f) < 5] However there's no reason to use a list comprehension here. The whole point of list comprehensions is to create a *new* list, which you don't appear to need; you just need to modify the existing fups list. -- John Gordon A is for Amy, who fell down the stairs gor...@panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" -- https://mail.python.org/mailman/listinfo/python-list
how to append to list in list comprehension
I have a list of lists of numbers like this excerpt. ['0', '0', '0', '0'] ['0', '0', '0', '0'] ['0', '0', '0', '0'] ['0', '0', '0', '0'] ['0', '0', '0', '0'] ['0', '0', '0', '0'] ['7', '2', '1', '0', '142647', '00'] ['7', '2', '0', '1', '87080', '00'] ['6', '1', '1', '1', '51700', '00'] ['4', '1', '1', '0', '36396', '00'] I want to go threw and for each index error at [4] append a 0. I have called the lists fups. p = re.compile('\d+') fups = p.findall(nomattr['firstup']) [x[4] for x in fups if IndexError fups.append(0)] print(fups) Unsure why I cannot use append in this instance, how can I modify to acheive desired output? Desired Output ['0', '0', '0', '0', '0'] ['0', '0', '0', '0', '0'] ['0', '0', '0', '0', '0'] ['0', '0', '0', '0', '0'] ['0', '0', '0', '0', '0'] ['0', '0', '0', '0', '0'] ['7', '2', '1', '0', '142647', '00'] ['7', '2', '0', '1', '87080', '00'] ['6', '1', '1', '1', '51700', '00'] ['4', '1', '1', '0', '36396', '00'] Thanks Sayth -- https://mail.python.org/mailman/listinfo/python-list