Wrapping around a list in Python.

S

shengjie.shengjie

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.
 
S

shengjie.shengjie

(e-mail address removed) 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.
 
S

shengjie.shengjie

(e-mail address removed) 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.

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 valuesby placing them in a list. However, this list gets really big and I am trying to find a work around.
 
G

Gary Herron

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
 
S

shengjie.shengjie

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?
 
S

shengjie.shengjie

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.
 
P

Peter Otten

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:
.... 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)
 
D

Denis McMahon

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.
 
D

David Robinow

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)
 
D

Dave Angel

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top