Re: What's an elegant way to test for list index existing?

2018-09-30 Thread Alister via Python-list
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

Re: What's an elegant way to test for list index existing?

2018-09-30 Thread Chris Green
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. >

Re: What's an elegant way to test for list index existing?

2018-09-30 Thread Chris Green
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 > >

Re: What's an elegant way to test for list index existing?

2018-09-30 Thread Chris Green
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

Re: What's an elegant way to test for list index existing?

2018-09-30 Thread Peter Otten
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,

Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Glen D souza
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

Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Glen D souza
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

Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Chris Angelico
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?

Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Alister via Python-list
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?

Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Dan Sommers
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:'

Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Peter Otten
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

Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Ben Finney
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

Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Chris Green
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?

2018-09-29 Thread Peter Otten
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

Re: What's an elegant way to test for list index existing?

2018-09-28 Thread Ben Finney
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

Re: What's an elegant way to test for list index existing?

2018-09-28 Thread jladasky
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

What's an elegant way to test for list index existing?

2018-09-28 Thread Chris Green
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. -- Chris Green · --

Test a list

2013-03-20 Thread Ana Dionísio
t= [3,5,6,7,10,14,17,21] Basically I want to print Test 1 when i is equal to an element of the list t and print Test 2 when i is not equal: while i=25: if i==t[]: print Test1 else: print Test2 What is missing here for this script work? Thank you all --

Re: Test a list

2013-03-20 Thread timothy crosley
Hi Ana, if I understand your question correctly, all you have to do to test this is to write: if i in t: print Test1 else: print Test2 On Wednesday, March 20, 2013 2:15:27 PM UTC-4, Ana Dionísio wrote: t= [3,5,6,7,10,14,17,21] Basically I want to print Test 1 when i is equal to

Re: Test a list

2013-03-20 Thread Joel Goldstick
On Wed, Mar 20, 2013 at 2:15 PM, Ana Dionísio anadionisio...@gmail.comwrote: t= [3,5,6,7,10,14,17,21] Basically I want to print Test 1 when i is equal to an element of the list t and print Test 2 when i is not equal: while i=25: You test i, but you don't set i to anything, or change it

Re: Test a list

2013-03-20 Thread Tim Chase
On 2013-03-20 11:15, Ana Dionísio wrote: t= [3,5,6,7,10,14,17,21] Basically I want to print Test 1 when i is equal to an element of the list t and print Test 2 when i is not equal: while i=25: if i==t[]: print Test1 else: print Test2 What is missing here for

Re: Test a list

2013-03-20 Thread Steven D'Aprano
On Wed, 20 Mar 2013 11:15:27 -0700, Ana Dionísio wrote: t= [3,5,6,7,10,14,17,21] Basically I want to print Test 1 when i is equal to an element of the list t and print Test 2 when i is not equal: Wouldn't it make more sense to print Equal and Not equal? If all you want to do is check

Re: Test a list

2013-03-20 Thread John Gordon
In 121be20a-c02a-4b0e-a86f-7c69597b9...@googlegroups.com =?ISO-8859-1?Q?Ana_Dion=EDsio?= anadionisio...@gmail.com writes: t= [3,5,6,7,10,14,17,21] Basically I want to print Test 1 when i is equal to an element of the list t and print Test 2 when i is not equal: while i=25: if

test for list equality

2011-12-15 Thread noydb
I want to test for equality between two lists. For example, if I have two lists that are equal in content but not in order, I want a return of 'equal' -- dont care if they are not in the same order. In order to get that equality, would I have to sort both lists regardless? if yes, how (having

Re: test for list equality

2011-12-15 Thread noydb
On Dec 15, 11:36 am, noydb jenn.du...@gmail.com wrote: I want to test for equality between two lists.  For example, if I have two lists that are equal in content but not in order, I want a return of 'equal' -- dont care if they are not in the same order.  In order to get that equality, would I

Re: test for list equality

2011-12-15 Thread MRAB
On 15/12/2011 16:36, noydb wrote: I want to test for equality between two lists. For example, if I have two lists that are equal in content but not in order, I want a return of 'equal' -- dont care if they are not in the same order. In order to get that equality, would I have to sort both

Re: test for list equality

2011-12-15 Thread John Gordon
In 61edc02c-4f86-45ef-82a1-61c701300...@t38g2000yqe.googlegroups.com noydb jenn.du...@gmail.com writes: My sort issue... as in this doesn't work if x.sort =3D=3D y.sort: ... print 'equal' ... else: ... print 'not equal' ... not equal ??? Use x.sort() instead of x.sort . -- John

Re: test for list equality

2011-12-15 Thread Stefan Behnel
noydb, 15.12.2011 18:49: On Dec 15, 11:36 am, noydb wrote: I want to test for equality between two lists. For example, if I have two lists that are equal in content but not in order, I want a return of 'equal' -- dont care if they are not in the same order. In order to get that equality,

Re: test for list equality

2011-12-15 Thread Miki Tebeka
My sort issue... as in this doesn't work if x.sort == y.sort: You're missing the () to make it a function call. Also list.sort() returns none, it mutates the original list. You can either sorted(x) == sorted(y) or set(x) == set(y) -- http://mail.python.org/mailman/listinfo/python-list

Re: test for list equality

2011-12-15 Thread Chris Kaynor
On Thu, Dec 15, 2011 at 9:57 AM, John Gordon gor...@panix.com wrote: In 61edc02c-4f86-45ef-82a1-61c701300...@t38g2000yqe.googlegroups.com noydb jenn.du...@gmail.com writes: My sort issue... as in this doesn't work if x.sort =3D=3D y.sort: ... print 'equal' ... else: ... print

Re: test for list equality

2011-12-15 Thread noydb
Ahh, I see (on the sort issue), thanks All! Still, any other slicker ways to do this? Just for learning. -- http://mail.python.org/mailman/listinfo/python-list

Re: test for list equality

2011-12-15 Thread darnold
On Dec 15, 11:59 am, Miki Tebeka miki.teb...@gmail.com wrote: My sort issue... as in this doesn't work if x.sort == y.sort: You're missing the () to make it a function call. Also list.sort() returns none, it mutates the original list. You can either     sorted(x) == sorted(y) or    

Re: test for list equality

2011-12-15 Thread MRAB
On 15/12/2011 17:49, noydb wrote: On Dec 15, 11:36 am, noydbjenn.du...@gmail.com wrote: I want to test for equality between two lists. For example, if I have two lists that are equal in content but not in order, I want a return of 'equal' -- dont care if they are not in the same order. In

Re: test for list equality

2011-12-15 Thread MRAB
On 15/12/2011 17:59, Miki Tebeka wrote: My sort issue... as in this doesn't work if x.sort == y.sort: You're missing the () to make it a function call. Also list.sort() returns none, it mutates the original list. You can either sorted(x) == sorted(y) or set(x) == set(y) But

Re: test for list equality

2011-12-15 Thread Tim Chase
On 12/15/11 11:59, Miki Tebeka wrote: My sort issue... as in this doesn't work if x.sort == y.sort: You're missing the () to make it a function call. Also list.sort() returns none, it mutates the original list. You can either sorted(x) == sorted(y) or set(x) == set(y) Duplicates

Re: test for list equality

2011-12-15 Thread Miki Tebeka
set(x) == set(y) Duplicates cause issues in the set() version: You're right, I stand corrected. -- http://mail.python.org/mailman/listinfo/python-list

Re: test for list equality

2011-12-15 Thread Ian Kelly
On Thu, Dec 15, 2011 at 11:07 AM, noydb jenn.du...@gmail.com wrote: Ahh, I see (on the sort issue), thanks All! Still, any other slicker ways to do this?  Just for learning. MRAB's collections.Counter suggestion is what I would do. Very tidy, and also more efficient I think: O(n) instead of

Re: test for list equality

2011-12-15 Thread Terry Reedy
On 12/15/2011 12:59 PM, Miki Tebeka wrote: My sort issue... as in this doesn't work if x.sort == y.sort: You're missing the () to make it a function call. Also list.sort() returns none, it mutates the original list. You can either sorted(x) == sorted(y) or set(x) == set(y) or

Re: test for list equality

2011-12-15 Thread Alec Taylor
Just for fun, use the Hungarian Algorithm (Python implementation: http://software.clapper.org/munkres/) On Fri, Dec 16, 2011 at 3:36 AM, noydb jenn.du...@gmail.com wrote: I want to test for equality between two lists.  For example, if I have two lists that are equal in content but not in

Re: test for list equality

2011-12-15 Thread Ian Kelly
On Thu, Dec 15, 2011 at 11:30 PM, Alec Taylor alec.tayl...@gmail.com wrote: Just for fun, use the Hungarian Algorithm (Python implementation: http://software.clapper.org/munkres/) That's a pretty silly approach, but okay: def listequals(a, b): if len(a) != len(b): return False

Re: test for list equality

2011-12-15 Thread Ian Kelly
On Fri, Dec 16, 2011 at 12:11 AM, Ian Kelly ian.g.ke...@gmail.com wrote: On Thu, Dec 15, 2011 at 11:30 PM, Alec Taylor alec.tayl...@gmail.com wrote: Just for fun, use the Hungarian Algorithm (Python implementation: http://software.clapper.org/munkres/) That's a pretty silly approach, but

Re: Test if list contains another list

2008-11-20 Thread str1442
Ali wrote: Its funny, I just visited this problem last week. http://dulceetutile.blogspot.com/2008/11/strange-looking-python- statement_17.html ./Ali -- http://mail.python.org/mailman/listinfo/python-list That use of reduce is nice, but you better use all() / any(). --

Re: Test if list contains another list

2008-11-16 Thread Ali
Its funny, I just visited this problem last week. http://dulceetutile.blogspot.com/2008/11/strange-looking-python- statement_17.html ./Ali -- http://mail.python.org/mailman/listinfo/python-list

Re: Test if list contains another list

2008-09-29 Thread Derek Martin
On Fri, Sep 26, 2008 at 01:39:16PM -0700, [EMAIL PROTECTED] wrote: # building prefix-function m = 0 for i in xrange(1, len_sub): while m 0 and sub[m] != sub[i]: m = table[m - 1] if sub[m] == sub[i]: m += 1 table[i] = m #

Re: Test if list contains another list

2008-09-29 Thread bearophileHUGS
Derek Martin: Quite a lot faster than mine... even without using psyco. It's designed for Psyco. However they don't appear to buy you much, given that the cases they optimize would probably be rare, and the difference in execution time gained by the optimization is not noticable to the user.

Re: Test if list contains another list

2008-09-29 Thread Derek Martin
On Mon, Sep 29, 2008 at 04:12:13AM -0700, [EMAIL PROTECTED] wrote: Derek Martin: Unless you're doing lots and lots of these in your application, I don't agree. That's library code, so it has to be efficient and flexible, because it's designed to be used in many different situations That's

Re: Test if list contains another list

2008-09-29 Thread bearophileHUGS
Derek Martin: code that implements non-obvious algorithms ought to explain what it's doing in comments, I am sorry, you are right, of course. In my libs/code there are always docstrings and doctests/tests, and most times comments too, like you say. When I post code here I often strip away part

Re: Test if list contains another list

2008-09-26 Thread Derek Martin
On Thu, Sep 18, 2008 at 03:24:16AM -0700, [EMAIL PROTECTED] wrote: I looked inside this thread for my query which brought me the following google search result Test if list contains another list - comp.lang.python | Google Groups But then I was disappointed to see the question asked

Re: Test if list contains another list

2008-09-26 Thread bearophileHUGS
I suggest Python programmers to fill the holes in the Python std lib with some debugged tuned implementations, their bag of tricks, so they don't have to re-invent and debug things all the time. This works well with Psyco: def issubseq(sub, items): issubseq(sub, items): return true if the

Re: Test if list contains another list

2008-09-26 Thread bearophileHUGS
bearophile: # searching m, i = 0, 0 ... i += 1 The name 'i' can be removed, sorry. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Test if list contains another list

2008-09-18 Thread gauravatnet
this works for arbitrary integers. Cheers, Cliff Hi, I looked inside this thread for my query which brought me the following google search result Test if list contains another list - comp.lang.python | Google Groups But then I was disappointed to see the question asked was not exactly right. Other

Re: Test if list contains another list

2008-09-18 Thread gauravatnet
... actually, with the '0x' prefix that hex() puts on numbers, I think this works for arbitrary integers. Cheers, Cliff Hi, I looked inside this thread for my query which brought me the following google search result Test if list contains another list - comp.lang.python | Google Groups

Re: Test if list contains another list

2008-09-09 Thread Bruno Desthuilliers
Matimus a écrit : On Sep 8, 12:32 am, Bruno Desthuilliers [EMAIL PROTECTED] wrote: (snip) set(a).issubset(set(b)) True Just to clarify, doing it using sets is not going to preserve order OR number of elements that are the same. That is: a = [1,1,2,3,4] b = [4,5,3,7,2,6,1]

Re: Test if list contains another list

2008-09-09 Thread J. Cliff Dyer
On Tue, 2008-09-09 at 10:49 +0200, Bruno Desthuilliers wrote: Matimus a écrit : On Sep 8, 12:32 am, Bruno Desthuilliers [EMAIL PROTECTED] wrote: (snip) set(a).issubset(set(b)) True Just to clarify, doing it using sets is not going to preserve order OR number of elements

Re: Test if list contains another list

2008-09-08 Thread James Mills
Hi, a = [1,2,3] b = [3,2,1,4] a = set(a) b = set(b) a.intersection(b) set([1, 2, 3]) Is this what you want ? cheers James On 9/8/08, mathieu [EMAIL PROTECTED] wrote: Hi there, I am trying to write something very simple to test if a list contains another one: a = [1,2,3] b

Re: Test if list contains another list

2008-09-08 Thread Bruno Desthuilliers
mathieu a écrit : Hi there, I am trying to write something very simple to test if a list contains another one: a = [1,2,3] b = [3,2,1,4] but 'a in b' returns False. Indeed. Lists are not sets, and the fact that all elements of list a happens to also be part of list b doesn't make

Re: Test if list contains another list

2008-09-08 Thread Christian Heimes
mathieu wrote: Hi there, I am trying to write something very simple to test if a list contains another one: a = [1,2,3] b = [3,2,1,4] but 'a in b' returns False. How do I check that a is indeed contained in b ? Use sets: a = [1,2,3] b = [3,2,1,4] set(a).issubset(set(b)) True

Re: Test if list contains another list

2008-09-08 Thread mathieu
On Sep 8, 9:32 am, Bruno Desthuilliers [EMAIL PROTECTED] wrote: mathieu a écrit : Hi there, I am trying to write something very simple to test if a list contains another one: a = [1,2,3] b = [3,2,1,4] but 'a in b' returns False. Indeed. Lists are not sets, and the fact

Test if list contains another list

2008-09-08 Thread mathieu
Hi there, I am trying to write something very simple to test if a list contains another one: a = [1,2,3] b = [3,2,1,4] but 'a in b' returns False. How do I check that a is indeed contained in b ? thanks -- http://mail.python.org/mailman/listinfo/python-list

Re: Test if list contains another list

2008-09-08 Thread Matimus
On Sep 8, 12:32 am, Bruno Desthuilliers [EMAIL PROTECTED] wrote: mathieu a écrit : Hi there,   I am trying to write something very simple to test if a list contains another one: a = [1,2,3] b = [3,2,1,4] but 'a in b' returns False. Indeed. Lists are not sets, and the fact