Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11590
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: testing if a list contains a sublist |
| Date | 2011-08-16 17:45 +0000 |
| Organization | Norwich University |
| Message-ID | <9avolbFff6U1@mid.individual.net> (permalink) |
| References | <mailman.27.1313450819.27778.python-list@python.org> <roy-77629E.20531315082011@news.panix.com> <8739h18rzj.fsf@dpt-info.u-strasbg.fr> <5e85b065-698e-4cd7-a409-d23618db1c3c@eb1g2000vbb.googlegroups.com> |
On 2011-08-16, nn <pruebauno@latinmail.com> wrote:
> That can be easily fixed:
>
>>>> def sublist(lst1, lst2):
> s1 = ','.join(map(str, lst1))
> s2 = ','.join(map(str, lst2))
> return False if s2.find(s1)==-1 else True
>
>>>> sublist([1,2,3],[1,2,3,4,5])
> True
>>>> sublist([1,2,2],[1,2,3,4,5])
> False
>>>> sublist([1,2,3],[1,3,5,7])
> False
>>>> sublist([12],[1,2])
> False
>>>>
String conversion is risky:
>>> sublist(['1,2', '3,4'], [1, 2, 3, 4])
True
Since we're bike-shedding, here's my entry. It's not clear to me
if accepting iterables rather than lists is a win, but I thought,
"Why not be general if the implementation is easy?"
def is_subseq_of(x, y):
r"""Return True if the elements in iterable x are found contiguously in
iterable y.
>>> is_subseq_of([], [])
True
>>> is_subseq_of([], [1, 2, 3])
True
>>> is_subseq_of([1], [1, 2, 3])
True
>>> is_subseq_of([1], [])
False
>>> is_subseq_of([4, 5], [1, 2, 3, 4, 5])
True
>>> is_subseq_of([1, 2], [1, 3, 2])
False
>>> is_subseq_of([2, 3], [1, 2, 3, 4])
True
>>> is_subseq_of([1, 2, 2], [1, 2, 3, 4, 5])
False
>>> is_subseq_of([1, 2], [1, 2])
True
>>> is_subseq_of([1, 2, 3], [1, 2])
False
>>> is_subseq_of(['1,2', '3,4'], [1, 2, 3, 4])
False
"""
x = tuple(x)
ix = 0
lenx = len(x)
if lenx == 0:
return True
for elem in y:
if x[ix] == elem:
ix += 1
else:
ix = 0
if ix == lenx:
return True
return False
if __name__ == '__main__':
import doctest
doctest.testmod()
--
Neil Cerutti
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
testing if a list contains a sublist Johannes <dajo.mail@web.de> - 2011-08-16 01:26 +0200
Re: testing if a list contains a sublist Roy Smith <roy@panix.com> - 2011-08-15 20:53 -0400
Re: testing if a list contains a sublist Laszlo Nagy <gandalf@shopzeus.com> - 2011-08-16 08:51 +0200
Re: testing if a list contains a sublist alex23 <wuwei23@gmail.com> - 2011-08-16 00:19 -0700
Re: testing if a list contains a sublist alex23 <wuwei23@gmail.com> - 2011-08-16 00:14 -0700
Re: testing if a list contains a sublist Laszlo Nagy <gandalf@shopzeus.com> - 2011-08-16 10:00 +0200
Re: testing if a list contains a sublist Johannes <dajo.mail@web.de> - 2011-08-16 17:26 +0200
Re: testing if a list contains a sublist ChasBrown <cbrown@cbrownsystems.com> - 2011-08-16 00:24 -0700
Re: testing if a list contains a sublist Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-08-16 14:23 +0200
Re: testing if a list contains a sublist Roy Smith <roy@panix.com> - 2011-08-16 08:53 -0400
Re: testing if a list contains a sublist nn <pruebauno@latinmail.com> - 2011-08-16 07:53 -0700
Re: testing if a list contains a sublist Laszlo Nagy <gandalf@shopzeus.com> - 2011-08-16 17:17 +0200
Re: testing if a list contains a sublist Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-08-16 17:39 +0200
Re: testing if a list contains a sublist Neil Cerutti <neilc@norwich.edu> - 2011-08-16 17:45 +0000
Re: testing if a list contains a sublist Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-16 12:12 +1000
Re: testing if a list contains a sublist Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-16 18:19 +1000
Re: testing if a list contains a sublist ChasBrown <cbrown@cbrownsystems.com> - 2011-08-15 23:14 -0700
Re: testing if a list contains a sublist ChasBrown <cbrown@cbrownsystems.com> - 2011-08-15 23:13 -0700
Re: testing if a list contains a sublist ChasBrown <cbrown@cbrownsystems.com> - 2011-08-15 23:14 -0700
Re: testing if a list contains a sublist Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-16 18:37 +1000
Re: testing if a list contains a sublist ChasBrown <cbrown@cbrownsystems.com> - 2011-08-16 21:13 -0700
Re: testing if a list contains a sublist Nobody <nobody@nowhere.com> - 2011-08-16 12:21 +0100
Re: testing if a list contains a sublist John Posner <jjposner@codicesoftware.com> - 2011-08-16 09:57 -0400
Re: testing if a list contains a sublist John Posner <jjposner@optimum.net> - 2011-08-16 09:57 -0400
Re: testing if a list contains a sublist Nobody <nobody@nowhere.com> - 2011-08-17 13:28 +0100
Re: testing if a list contains a sublist Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-20 12:10 +1000
csiph-web