Discussion:
[Tutor] skip/slice more than every second?
questions anon
2015-09-29 03:16:36 UTC
Permalink
a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]

how can I show the first then skip three and show the next and so on?
For example:
show 1
then skip 2,3,4
then show 5
then skip 6,7,8
then show 9
then skip 10,11,12,

etc.

Any feedback will be greatly appreciated.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
questions anon
2015-09-29 03:54:44 UTC
Permalink
thankyou but I just realised I wrote the question wrong -

how do I do the inverse of above
so
hide 1 show 2,3,4 hide 5, show 6,7,8 etc.

thanks in advance

On Tue, Sep 29, 2015 at 1:33 PM, Sebastian Cheung <
print range(1,15,4)
ans: [1, 5, 9,13]
Sent from my iPhone
Post by questions anon
a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
how can I show the first then skip three and show the next and so on?
show 1
then skip 2,3,4
then show 5
then skip 6,7,8
then show 9
then skip 10,11,12,
etc.
Any feedback will be greatly appreciated.
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-09-29 04:16:39 UTC
Permalink
Post by questions anon
thankyou but I just realised I wrote the question wrong -
how do I do the inverse of above
so
hide 1 show 2,3,4 hide 5, show 6,7,8 etc.
thanks in advance
A little more tricky. The version I showed using iter() and a while loop
will work, but perhaps this is simpler:

py> for i in range(0, len(a), 4):
... print(a[i+1:i+4])
...
[2, 3, 4]
[6, 7, 8]
[10, 11, 12]
[14]
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-09-29 07:33:16 UTC
Permalink
Post by questions anon
thankyou but I just realised I wrote the question wrong -
how do I do the inverse of above
so
hide 1 show 2,3,4 hide 5, show 6,7,8 etc.
thanks in advance
Post by questions anon
a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
del a[::4]
a
[2, 3, 4, 6, 7, 8, 10, 11, 12, 14]

As this modifies the list you may want to make a copy first.

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-09-29 09:36:13 UTC
Permalink
Post by questions anon
hide 1 show 2,3,4 hide 5, show 6,7,8 etc.
thanks in advance
And just for kicks you can use a list comprehension:

print [n for i,n in enumerate(a) if i%4]

hth
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-09-29 10:34:17 UTC
Permalink
The idea is learn how to merge different Flask apps as one.
Please don't hijack an existing thread for new topics, it messes
up the archive for searching.

However, in this case the question is very Flask specific and would
be better directed to a Flask community forum such as one of those
described here:

http://flask.pocoo.org/community/

This list is really for core Python language/library questions.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-09-29 03:55:41 UTC
Permalink
Post by questions anon
a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
how can I show the first then skip three and show the next and so on?
show 1
then skip 2,3,4
then show 5
then skip 6,7,8
then show 9
then skip 10,11,12,
Here is one method, using slicing:

py> a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
py> a[::4]
[1, 5, 9, 13]


The slice a[::4] is equivalent to a[0:len(a):4], that is, it starts at
position 0, keeps going until the end, and extracts every fourth value.

Here is another method:


py> it = iter(a)
py> while True:
... try:
... print(next(it))
... who_cares = next(it), next(it), next(it)
... except StopIteration:
... break
...
1
5
9
13

This grabs each item in turn, prints the first, then throws away three,
then prints the next, and so on. This is a good technique for when you
want something that doesn't fit into the fairly simple patterns
available using slicing, e.g.:

- print 1 item;
- throw away 2;
- print 3 items;
- throw away 4;
- print 5 items;
- throw away 6;
- print 7 items;
- throw away 8;

etc.


But for cases where slicing does the job, it is better to use that.
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-09-29 10:30:04 UTC
Permalink
Post by questions anon
a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
how can I show the first then skip three and show the next and so on?
print range(1,15,4)
ans: [1, 5, 9,13]
While this gives the output that the OP asked for, its
what's known as a coincidental solution. It is just
a coincidence that the data generated by range()
matches the sample data provided. If the OP had provided
a random set of data or used a different type
(eg characters) then this would not help.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...