Re: What's an elegant way to test for list index existing?
On Sun, 30 Sep 2018 11:45:21 +0100, Bart wrote: > On 30/09/2018 11:14, Chris Green wrote: >> Chris Angelico wrote: >>> On Sat, Sep 29, 2018 at 12:21 PM Chris Green wrote: I have a list created by:- fld = shlex.split(ln) It may contain 3, 4 or 5 entries according to data read into ln. What's the neatest way of setting the fourth and fifth entries to an empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels clumsy somehow. >>> >>> shlex.split(ln) + ["", ""] >>> >> Now that *is* neat, I will probably do this. > > Won't it give you 7 entries, when shlex.split(ln) returns 5? > > Or doesn't that matter? (In which case that's something not mentioned in > the specification.) if only 5 entries are required than simply prune off the extra using a slice as I suggested yesterday -- DalNet is like the special olympics of IRC. There's a lot of drooling goin' on and everyone is a 'winner'. -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
Bart wrote: > On 30/09/2018 11:14, Chris Green wrote: > > Chris Angelico wrote: > >> On Sat, Sep 29, 2018 at 12:21 PM Chris Green wrote: > >>> > >>> I have a list created by:- > >>> > >>> fld = shlex.split(ln) > >>> > >>> It may contain 3, 4 or 5 entries according to data read into ln. > >>> What's the neatest way of setting the fourth and fifth entries to an > >>> empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels > >>> clumsy somehow. > >> > >> shlex.split(ln) + ["", ""] > >> > > Now that *is* neat, I will probably do this. > > Won't it give you 7 entries, when shlex.split(ln) returns 5? > > Or doesn't that matter? (In which case that's something not mentioned in > the specification.) No, it doesn't matter, I just need at least 5. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
Chris Angelico wrote: > On Sat, Sep 29, 2018 at 12:21 PM Chris Green wrote: > > > > I have a list created by:- > > > > fld = shlex.split(ln) > > > > It may contain 3, 4 or 5 entries according to data read into ln. > > What's the neatest way of setting the fourth and fifth entries to an > > empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels > > clumsy somehow. > > shlex.split(ln) + ["", ""] > Now that *is* neat, I will probably do this. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
Glen D souza wrote: > i have a approach, it may not be best > > fld = [ ] > for data in shlex.split(ln): >fld.append(data) > It's certainly simple! :-) I (OP) have actually done something quite similar:- fld = shlex.split(ln) fld.append(999) fld.append(999) It means I can test for '999' as when read from ln they are text fields and it's text I have created so (unless I get *very* confused) it will never be 999. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
Glen D souza wrote: > fld = [ ] > data = shlex.split(ln) > for item in data: >fld.append(item) > fld = fld + [0] * (5 - len(data)) There's no need to make a copy of data, one item at the time. It's a tedious way to build a new list, and you are throwing it away in the next line anyway, as adding first_list + second_list creates a new list containing the items of both. So: data = shlex.split(ln) fld = data + [""] * (5 - len(data)) # the OP wants strings -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
i have a approach, it may not be best fld = [ ] for data in shlex.split(ln): fld.append(data) On Sat, 29 Sep 2018 at 07:52, wrote: > On Friday, September 28, 2018 at 11:03:17 AM UTC-7, Chris Green wrote: > > I have a list created by:- > > > > fld = shlex.split(ln) > > > > It may contain 3, 4 or 5 entries according to data read into ln. > > What's the neatest way of setting the fourth and fifth entries to an > > empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels > > clumsy somehow. > > How about this? > > from itertools import chain, repeat > temp = shlex.split(ln) > fld = list(chain(temp, repeat("", 5-len(temp > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
fld = [ ] data = shlex.split(ln) for item in data: fld.append(item) fld = fld + [0] * (5 - len(data)) On Sat, 29 Sep 2018 at 11:03, Glen D souza wrote: > i have a approach, it may not be best > > fld = [ ] > for data in shlex.split(ln): >fld.append(data) > > > > On Sat, 29 Sep 2018 at 07:52, wrote: > >> On Friday, September 28, 2018 at 11:03:17 AM UTC-7, Chris Green wrote: >> > I have a list created by:- >> > >> > fld = shlex.split(ln) >> > >> > It may contain 3, 4 or 5 entries according to data read into ln. >> > What's the neatest way of setting the fourth and fifth entries to an >> > empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels >> > clumsy somehow. >> >> How about this? >> >> from itertools import chain, repeat >> temp = shlex.split(ln) >> fld = list(chain(temp, repeat("", 5-len(temp >> -- >> https://mail.python.org/mailman/listinfo/python-list >> > -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
On Sat, Sep 29, 2018 at 12:21 PM Chris Green wrote: > > I have a list created by:- > > fld = shlex.split(ln) > > It may contain 3, 4 or 5 entries according to data read into ln. > What's the neatest way of setting the fourth and fifth entries to an > empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels > clumsy somehow. shlex.split(ln) + ["", ""] ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
On Fri, 28 Sep 2018 19:00:29 +0100, Chris Green wrote: > I have a list created by:- > > fld = shlex.split(ln) > > It may contain 3, 4 or 5 entries according to data read into ln. What's > the neatest way of setting the fourth and fifth entries to an empty > string if they don't (yet) exist? Using 'if len(fld) < 4:' feels clumsy > somehow. how about simply adding 2 or more null entries to the list then slicing? string = "a b c" a=string.split() (a+['',''])[0:5] -- QOTD: If you're looking for trouble, I can offer you a wide selection. -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
On 9/28/18 2:00 PM, Chris Green wrote: I have a list created by:- fld = shlex.split(ln) It may contain 3, 4 or 5 entries according to data read into ln. What's the neatest way of setting the fourth and fifth entries to an empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels clumsy somehow. Do you care whether there are more than 5 entries in the list? If not, then just add two empty strings to the end of the list: fld.extend(["", ""]) If fld already contained 5 entries, then the extra two empty strings may or may not affect the subsequent logic. If possible extra entries bother you, then truncate the list: fld = (fld + ["", ""])[:5] Or add empty strings as long as the list contains 5 entries: while len(fld) < 5: fld.append("") Which one is "better" or "best"? Your call, depending on what your criteria are. I think the last one expresses the intent the most clearly, but YMMV. -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
Ben Finney wrote: > Ben Finney writes: > >> You can use a comprehension, iterating over the full range of index you >> want:: >> >> words = shlex.split(line) >> padding_length = 5 >> words_padded = [ >> (words[index] if index < len(words)) >> for index in range(padding_length)] > > That omits the important case you were concerned with: when `index < > len(words)` is false. In other words, that example fails to actually pad > the resulting list. It would if it weren't a syntax error. No harm done ;) > Try this instead:: > > words = shlex.split(line) > padding_length = 5 > padding_value = None > words_padded = [ > (words[index] if index < len(words) else padding_value) > for index in range(padding_length)] > -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
Ben Finney writes: > You can use a comprehension, iterating over the full range of index you > want:: > > words = shlex.split(line) > padding_length = 5 > words_padded = [ > (words[index] if index < len(words)) > for index in range(padding_length)] That omits the important case you were concerned with: when `index < len(words)` is false. In other words, that example fails to actually pad the resulting list. Try this instead:: words = shlex.split(line) padding_length = 5 padding_value = None words_padded = [ (words[index] if index < len(words) else padding_value) for index in range(padding_length)] -- \ “When a well-packaged web of lies has been sold to the masses | `\over generations, the truth will seem utterly preposterous and | _o__)its speaker a raving lunatic.” —Dresden James | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
Thanks all, several possible ways of doing it there. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
jlada...@itu.edu wrote: > On Friday, September 28, 2018 at 11:03:17 AM UTC-7, Chris Green wrote: >> I have a list created by:- >> >> fld = shlex.split(ln) >> >> It may contain 3, 4 or 5 entries according to data read into ln. >> What's the neatest way of setting the fourth and fifth entries to an >> empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels >> clumsy somehow. > > How about this? > > from itertools import chain, repeat > temp = shlex.split(ln) > fld = list(chain(temp, repeat("", 5-len(temp If you are OK with silently dropping extra entries fld = list(islice(chain(shlex.split(ln), repeat("")), 5)) or its non-itertools equivalent fld = (shlex.split(ln) + [""] * 5)[:5] are also possible. Personally I consider none of these to be elegant. If you are going to unpack the fld entries I'd do it in a function with default values: >>> def use_args(foo, bar, baz, ham="", spam=""): ... print("\n".join("{} = {!r}".format(*p) for p in locals().items())) ... >>> use_args(*shlex.split("a b c")) spam = '' ham = '' baz = 'c' foo = 'a' bar = 'b' >>> use_args(*shlex.split("a b c d")) spam = '' ham = 'd' baz = 'c' foo = 'a' bar = 'b' This has the advantage that it fails for the unforeseen cases: >>> use_args(*shlex.split("a b c d e f")) Traceback (most recent call last): File "", line 1, in TypeError: use_args() takes from 3 to 5 positional arguments but 6 were given >>> use_args(*shlex.split("a b")) Traceback (most recent call last): File "", line 1, in TypeError: use_args() missing 1 required positional argument: 'baz' -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
Chris Green writes: > I have a list created by:- > > fld = shlex.split(ln) > > It may contain 3, 4 or 5 entries according to data read into ln. Because of what an index means for the 'list' type, that's equivalent to saying "the result of `len(fld)` may be 3, 4, or 5". > What's the neatest way of setting the fourth and fifth entries to an > empty string if they don't (yet) exist? You have the right idea: testing the length of the object is a correct and expressive way to ask "is there an item at this index in the list". > Using 'if len(fld) < 4:' feels clumsy somehow. One reason I finx that clumsy is that you're testing against a hard-coded value; and you'd have to write a loop to get the value each time. You can use a comprehension, iterating over the full range of index you want:: words = shlex.split(line) padding_length = 5 words_padded = [ (words[index] if index < len(words)) for index in range(padding_length)] That accomplishes the construction of the padded list in a single expression, hopefully expressive, and definitely making use of whatever optimisations the in-built comprehension mechanics provide. -- \ “Pray, v. To ask that the laws of the universe be annulled in | `\ behalf of a single petitioner confessedly unworthy.” —Ambrose | _o__) Bierce, _The Devil's Dictionary_, 1906 | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: What's an elegant way to test for list index existing?
On Friday, September 28, 2018 at 11:03:17 AM UTC-7, Chris Green wrote: > I have a list created by:- > > fld = shlex.split(ln) > > It may contain 3, 4 or 5 entries according to data read into ln. > What's the neatest way of setting the fourth and fifth entries to an > empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels > clumsy somehow. How about this? from itertools import chain, repeat temp = shlex.split(ln) fld = list(chain(temp, repeat("", 5-len(temp -- https://mail.python.org/mailman/listinfo/python-list