Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #61995 > unrolled thread
| Started by | shengjie.shengjie@live.com |
|---|---|
| First post | 2013-12-15 20:38 -0800 |
| Last post | 2013-12-15 21:26 -0800 |
| Articles | 12 — 8 participants |
Back to article view | Back to comp.lang.python
Wrapping around a list in Python. shengjie.shengjie@live.com - 2013-12-15 20:38 -0800
Re: Wrapping around a list in Python. Ben Finney <ben+python@benfinney.id.au> - 2013-12-16 15:59 +1100
Re: Wrapping around a list in Python. shengjie.shengjie@live.com - 2013-12-15 21:07 -0800
Re: Wrapping around a list in Python. shengjie.shengjie@live.com - 2013-12-15 21:10 -0800
Re: Wrapping around a list in Python. Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-16 09:33 +0000
Re: Wrapping around a list in Python. Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-16 10:12 +0000
Re: Wrapping around a list in Python. Gary Herron <gary.herron@islandtraining.com> - 2013-12-15 21:10 -0800
Re: Wrapping around a list in Python. shengjie.shengjie@live.com - 2013-12-15 21:26 -0800
Re: Wrapping around a list in Python. Peter Otten <__peter__@web.de> - 2013-12-16 10:15 +0100
Re: Wrapping around a list in Python. David Robinow <drobinow@gmail.com> - 2013-12-16 07:41 -0500
Re: Wrapping around a list in Python. Dave Angel <davea@davea.name> - 2013-12-16 07:51 -0500
Re: Wrapping around a list in Python. shengjie.shengjie@live.com - 2013-12-15 21:26 -0800
| From | shengjie.shengjie@live.com |
|---|---|
| Date | 2013-12-15 20:38 -0800 |
| Subject | Wrapping around a list in Python. |
| Message-ID | <e8123076-9f52-4a72-b262-908a5dcc8a10@googlegroups.com> |
Hi guys, I am trying to create a fixed list which would allow my values to be wrapped around it. For example i have 10 values : 0,1,2,3,4,5,6,7,8,9 I need to create a list which contains 4 numbers and when the number exceeds the list, it would overwrite the first value. [0,1,2,3] [4,1,2,3] [5,4,1,2] Thanks in advance and much help appreciated.
[toc] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2013-12-16 15:59 +1100 |
| Message-ID | <mailman.4177.1387169987.18130.python-list@python.org> |
| In reply to | #61995 |
shengjie.shengjie@live.com writes: > Hi guys, I am trying to create a fixed list which would allow my > values to be wrapped around it. This doesn't make a lot of sense to me, but I assume you have a purpose in mind for this. What is the purpose? Perhaps it will help the explanation if we know what it's for. > For example i have 10 values : 0,1,2,3,4,5,6,7,8,9 Does this mean the input is a list, ‘[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]’? Or do you mean something else? What is the input? > I need to create a list which contains 4 numbers and when the number > exceeds the list, it would overwrite the first value. > [0,1,2,3] > [4,1,2,3] > [5,4,1,2] That's three different lists. What is the input in each case? Under what circumstances would you expect each one to be produced? -- \ “As scarce as truth is, the supply has always been in excess of | `\ the demand.” —Josh Billings | _o__) | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | shengjie.shengjie@live.com |
|---|---|
| Date | 2013-12-15 21:07 -0800 |
| Message-ID | <1f1073b7-a171-4470-aa4e-76d119a4c395@googlegroups.com> |
| In reply to | #62001 |
On Monday, 16 December 2013 12:59:32 UTC+8, Ben Finney wrote: > shengjie.shengjie@live.com writes: > > > > > Hi guys, I am trying to create a fixed list which would allow my > > > values to be wrapped around it. > > > > This doesn't make a lot of sense to me, but I assume you have a purpose > > in mind for this. What is the purpose? Perhaps it will help the > > explanation if we know what it's for. > > > > > For example i have 10 values : 0,1,2,3,4,5,6,7,8,9 > > > > Does this mean the input is a list, ‘[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]’? Or > > do you mean something else? What is the input? > > > > > I need to create a list which contains 4 numbers and when the number > > > exceeds the list, it would overwrite the first value. > > > > > [0,1,2,3] > > > [4,1,2,3] > > > [5,4,1,2] > > > > That's three different lists. What is the input in each case? Under what > > circumstances would you expect each one to be produced? > > > > -- > > \ “As scarce as truth is, the supply has always been in excess of | > > `\ the demand.” —Josh Billings | > > _o__) | > > Ben Finney Im currently creating a logging function for my GUI in pyqt. I saved its feedback data values in a .txt file and I access these values by placing them in a list. However, this list gets really big and I am trying to find a work around.
[toc] | [prev] | [next] | [standalone]
| From | shengjie.shengjie@live.com |
|---|---|
| Date | 2013-12-15 21:10 -0800 |
| Message-ID | <5a27e644-5a6f-4ff3-ad70-6317c143fb8e@googlegroups.com> |
| In reply to | #62003 |
On Monday, 16 December 2013 13:07:46 UTC+8, shengjie...@live.com wrote: > On Monday, 16 December 2013 12:59:32 UTC+8, Ben Finney wrote: > > > shengjie.shengjie@live.com writes: > > > > > > > > > > > > > Hi guys, I am trying to create a fixed list which would allow my > > > > > > > values to be wrapped around it. > > > > > > > > > > > > This doesn't make a lot of sense to me, but I assume you have a purpose > > > > > > in mind for this. What is the purpose? Perhaps it will help the > > > > > > explanation if we know what it's for. > > > > > > > > > > > > > For example i have 10 values : 0,1,2,3,4,5,6,7,8,9 > > > > > > > > > > > > Does this mean the input is a list, ‘[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]’? Or > > > > > > do you mean something else? What is the input? > > > > The input is from [0 to 9] however I want a display which displays [0-3] and when the value reaches 3, it replaces the 4th value with the first value and so on. > > > > > > > > > > I need to create a list which contains 4 numbers and when the number > > > > > > > exceeds the list, it would overwrite the first value. > > > > > > > > > > > > > [0,1,2,3] > > > > > > > [4,1,2,3] > > > > > > > [5,4,1,2] > > > > > > > > > > > > That's three different lists. What is the input in each case? Under what > > > > > > circumstances would you expect each one to be produced? > > > > > > > > > > > > -- > > > > > > \ “As scarce as truth is, the supply has always been in excess of | > > > > > > `\ the demand.” —Josh Billings | > > > > > > _o__) | > > > > > > Ben Finney > > > > Im currently creating a logging function for my GUI in pyqt. > > I saved its feedback data values in a .txt file and I access these values by placing them in a list. However, this list gets really big and I am trying to find a work around.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-12-16 09:33 +0000 |
| Message-ID | <mailman.4193.1387186441.18130.python-list@python.org> |
| In reply to | #62005 |
On 16/12/2013 05:10, shengjie.shengjie@live.com wrote: > On Monday, 16 December 2013 13:07:46 UTC+8, shengjie...@live.com wrote: Would you please read and action this https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing double line spacing, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-12-16 10:12 +0000 |
| Message-ID | <l8mjm6$tjl$2@dont-email.me> |
| In reply to | #62001 |
On Mon, 16 Dec 2013 15:59:32 +1100, Ben Finney wrote: > shengjie.shengjie@live.com writes: > >> Hi guys, I am trying to create a fixed list which would allow my values >> to be wrapped around it. > > This doesn't make a lot of sense to me, but I assume you have a purpose > in mind for this. What is the purpose? Perhaps it will help the > explanation if we know what it's for. > >> For example i have 10 values : 0,1,2,3,4,5,6,7,8,9 > > Does this mean the input is a list, ‘[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]’? Or > do you mean something else? What is the input? > >> I need to create a list which contains 4 numbers and when the number >> exceeds the list, it would overwrite the first value. > >> [0,1,2,3] >> [4,1,2,3] >> [5,4,1,2] > > That's three different lists. What is the input in each case? Under what > circumstances would you expect each one to be produced? I suspect the OP means: first input is 0, list = [ 0 ] next input is 1, list = [ 0, 1 ] next input is 2, list = [ 0, 1, 2 ] next input is 3, list = [ 0, 1, 2, 3 ] next input is 4, list = [ 4, 1, 2, 3 ] next input is 5, list = [ 5, 4, 1, 2 ] But this is a bit daft, because he starts by appending, and when he hits overflow he starts prepending. What I think he should do is use collections.dequeue and a couple of helper functions to add and get data items. For a queue moving from position 0 to position 3 (left to right): from collections import deque q = dequeue([]) def add_to_queue( item, q ): if len( q ) is 4: q.pop() q.appendleft( item ) def get_next( q ): if len( q ) > 0: return q.pop() return None To move from position 3 to position 0 (right to left), swap pop and appendleft for popleft and append. -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Gary Herron <gary.herron@islandtraining.com> |
|---|---|
| Date | 2013-12-15 21:10 -0800 |
| Message-ID | <mailman.4181.1387171006.18130.python-list@python.org> |
| In reply to | #61995 |
On 12/15/2013 08:38 PM, shengjie.shengjie@live.com wrote: > Hi guys, I am trying to create a fixed list which would allow my values to be wrapped around it. > For example i have 10 values : 0,1,2,3,4,5,6,7,8,9 > I need to create a list which contains 4 numbers and when the number exceeds the list, it would overwrite the first value. > [0,1,2,3] > [4,1,2,3] > [5,4,1,2] > > Thanks in advance and much help appreciated. Is the output really three lists as you show. Or is that one list whose contents you have shown three snapshots of? Then what was the point of putting 4 in the first spot when you are just going to move it to the second spot? And why stop at 4 and 5? What about 7, 8, and 9? Are you really shifting elements onto the beginning of the list and off the end of the list? (That's easy to do, but is that what you want?) If I follow your example a few elements further I get [9,8,7,6], just the last four elements of the original list in reverse order -- so there is no need fill a list and "wrap-around" -- just grab the last four elements and reverse them. Or have I misunderstood the problem completely? (I think that's likely.) I'm sure Python is general enough to do what you want, but you'll have to do a much better job telling is what you want. While you are at it, tell us what you've already done, and how it fails to do whatever it is you want. Gary Herron
[toc] | [prev] | [next] | [standalone]
| From | shengjie.shengjie@live.com |
|---|---|
| Date | 2013-12-15 21:26 -0800 |
| Message-ID | <cf42bb27-a2c9-4a94-9c74-a6ff1253a2ee@googlegroups.com> |
| In reply to | #62007 |
On Monday, 16 December 2013 13:10:22 UTC+8, Gary Herron wrote: > On 12/15/2013 08:38 PM, shengjie.shengjie@live.com wrote: > > > Hi guys, I am trying to create a fixed list which would allow my values to be wrapped around it. > > > For example i have 10 values : 0,1,2,3,4,5,6,7,8,9 > > > I need to create a list which contains 4 numbers and when the number exceeds the list, it would overwrite the first value. > > > [0,1,2,3] > > > [4,1,2,3] > > > [5,4,1,2] > > > > > > Thanks in advance and much help appreciated. > > > > Is the output really three lists as you show. Or is that one list whose > > contents you have shown three snapshots of? Then what was the point of > > putting 4 in the first spot when you are just going to move it to the > > second spot? And why stop at 4 and 5? What about 7, 8, and 9? > > > > Are you really shifting elements onto the beginning of the list and off > > the end of the list? (That's easy to do, but is that what you want?) > > > > If I follow your example a few elements further I get [9,8,7,6], just > > the last four elements of the original list in reverse order -- so there > > is no need fill a list and "wrap-around" -- just grab the last four > > elements and reverse them. > > > > Or have I misunderstood the problem completely? (I think that's > > likely.) I'm sure Python is general enough to do what you want, but > > you'll have to do a much better job telling is what you want. While you > > are at it, tell us what you've already done, and how it fails to do > > whatever it is you want. > > > > Gary Herron The idea is to grab the last 4 elements of the array. However i have an array that contains a few hundred elements in it. And the values continues to .append over time. How would i be able to display the last 4 elements of the array under such a condition?
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-12-16 10:15 +0100 |
| Message-ID | <mailman.4188.1387185319.18130.python-list@python.org> |
| In reply to | #62008 |
shengjie.shengjie@live.com wrote: > The idea is to grab the last 4 elements of the array. However i have an > array that contains a few hundred elements in it. And the values continues > to .append over time. How would i be able to display the last 4 elements > of the array under such a condition? Use a deque: >>> from collections import deque >>> last_four = deque(maxlen=4) >>> for i in range(10): ... last_four.append(i) ... >>> last_four deque([6, 7, 8, 9], maxlen=4) >>> last_four.extend(range(100, 200)) >>> last_four deque([196, 197, 198, 199], maxlen=4) >>> last_four.append(42) >>> last_four deque([197, 198, 199, 42], maxlen=4)
[toc] | [prev] | [next] | [standalone]
| From | David Robinow <drobinow@gmail.com> |
|---|---|
| Date | 2013-12-16 07:41 -0500 |
| Message-ID | <mailman.4211.1387197726.18130.python-list@python.org> |
| In reply to | #62008 |
On Mon, Dec 16, 2013 at 12:26 AM, <shengjie.shengjie@live.com> wrote: > The idea is to grab the last 4 elements of the array. However i have an array that contains a few hundred elements in it. And the values continues to .append over time. How would i be able to display the last 4 elements of the array under such a condition? I assume you mean 'list' rather than 'array'. If all you want to do is 'display' the last 4 elements: big_list = [0,1,2,3,4,5,6,7,8,9] last4 = big_list[-4:] print(last4)
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-12-16 07:51 -0500 |
| Message-ID | <mailman.4212.1387198243.18130.python-list@python.org> |
| In reply to | #62008 |
On Sun, 15 Dec 2013 21:26:49 -0800 (PST), shengjie.shengjie@live.com wrote: > The idea is to grab the last 4 elements of the array. However i have an array that contains a few hundred elements in it. And the values continues to .append over time. How would i be able to display the last 4 elements of the array under such a condition? Your earlier example showed [5, 4, 1, 2] as one of the results, which is not "the last four". But assuming your goal has now changed and you really have an array (or list) of a few hundred elements, then you can just use data [-4:] to get them. But if you're being imprecise and these values are not really in an array, then follow Peter ' advice and use a deque. Or fake it with a list, appending to the end and popping from the beginning. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | shengjie.shengjie@live.com |
|---|---|
| Date | 2013-12-15 21:26 -0800 |
| Message-ID | <2cadbabb-768c-4c16-b52b-6bb183affd31@googlegroups.com> |
| In reply to | #61995 |
On Monday, 16 December 2013 12:38:14 UTC+8, shengjie...@live.com wrote: > Hi guys, I am trying to create a fixed list which would allow my values to be wrapped around it. > > For example i have 10 values : 0,1,2,3,4,5,6,7,8,9 > > I need to create a list which contains 4 numbers and when the number exceeds the list, it would overwrite the first value. > > [0,1,2,3] > > [4,1,2,3] > > [5,4,1,2] > > > > Thanks in advance and much help appreciated.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web