How to store some elements from a list into another

Thomas Jollans tjol at tjol.eu
Mon Jun 12 15:09:31 EDT 2017


On 12/06/17 20:32, José Manuel Suárez Sierra wrote:
> Hello,
> I am stuck with a (perhaps) easy problem, I hope someone can help me:
> 
> My problem is:
> I have a list of lists like this one:
> [[55, 56, 57, 58, 83, 84, 85, 86, 89, 90, 91, 92, 107, 108, 109, 110, 111, 117, 118, 119, 120, 128, 129, 130, 131, 135, 136, 137, 138, 184, 185, 186, 187, 216, 217, 218, 219, 232, 233, 234, 235, 236, 237, 238, 267, 268, 269, 270, 272, 273, 274, 275], [2, 3, 4, 5, 9, 10, 11, 12, 21, 22, 23, 24, 29, 30, 31, 32, 56, 57, 58, 59, 65, 66, 67, 68, 74, 75, 76, 77, 78, 89, 90, 91, 92, 98, 99, 100, 101, 102, 125, 126, 127, 128, 131, 132, 133, 134, 135]]
> 
> And what I want is to store some of these datum into another list according to the next conditions:
> 1. I just want to store data if these values are consecutive (four in four), for instance, for first element I would like to store into the new list: [[[55,58],[83,86],....[n,n+3]]] and so on.
>  I tried something like this:

Something similar to this should work with a few fixes (though it won't
be very "pythonic"). Let's look at what this code is doing!

> 
>         x=0
>         y=0
>         while list6[x][y] == list6[x][y+1]-1 and list6[x][y] == list6[x][y+1]-2 and list6[x][y] == list6[x][y+1]-3 or list6[x][0]:

Right now, x=0, y=0, and I assume list6[0] is [55, 56, 57, 58, 83, ...]

so:

 - list6[x][y] == list6[x][y+1]-1 is True: 55 == 56-1
 - list6[x][y] == list6[x][y+1]-2 is False: 55 != 56-2
 - list6[x][y] == list6[x][y+1]-3 is False: 55 != 56-3
 - list6[x][0] is True: 55 != 0.

So the entire expression is equivalent to (at this moment:

while True and False and False or True:

which is equivalent to while True.

(Do you see the problem with your while statement?)


> 
>             list7.append(list6[x][y])
>             list7.append(list6[x][y+3])
We enter the look and things are added to your list.


Anyway, I hope you see the mistake you made in your while condition.

>             y = y+1

Are you sure you want to increase y by one only?

>             if (list6[x][y])%(list6[x][len(list6[x])-1]) == 0:
>                 x= x+1
> 
>             if len(list6[x]) == x and len(list6[x][y]) == y:
>                 break
> 
> 
> It does not work

You should be able to figure out why it doesn't work (and fix it!) by
carefully checking what each step does. It's often helpful to add
print() calls at strategic places in your code to check what exactly
it's doing, and track what's going on. For example:

py> list6 = [[55, 56, 57, 58, 83, 84, 85, 86, 89, 90, 91, 92, 107, 108,
109, 110, 111, 117, 118, 119, 120, 128, 129, 130, 131, 135, 136, 137,
138, 184, 185, 186, 187, 216, 217, 218, 219, 232, 233, 234, 235, 236,
237, 238, 267, 268, 269, 270, 272, 273, 274, 275], [2, 3, 4, 5, 9, 10,
11, 12, 21, 22, 23, 24, 29, 30, 31, 32, 56, 57, 58, 59, 65, 66, 67, 68,
74, 75, 76, 77, 78, 89, 90, 91, 92, 98, 99, 100, 101, 102, 125, 126,
127, 128, 131, 132, 133, 134, 135]]
py> list7 = []
py> x=0
py> y=0
py> while list6[x][y] == list6[x][y+1]-1 and list6[x][y] ==
list6[x][y+1]-2 and list6[x][y] == list6[x][y+1]-3 or list6[x][0]:
...     list7.append(list6[x][y])
...     list7.append(list6[x][y+3])
...     print('appended', list6[x][y], list6[x][y+3])
...     y = y+1
...     if (list6[x][y])%(list6[x][len(list6[x])-1]) == 0:
...         x = x+1
...     if len(list6[x]) == x and len(list6[x][y]) == y:
...         break
...
appended 55 58
appended 56 83
appended 57 84
[...]
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
IndexError: list index out of range
py> list7
[55, 58, 56, 83, 57, 84, 58, 85, 83, 86, 84, 89, 85, 90, 86, 91, 89, 92,
90, 107, 91, 108, 92, 109, 107, 110, 108, 111, 109, 117, 110, 118, 111,
119, 117, 120, 118, 128, 119, 129, 120, 130, 128, 131, 129, 135, 130,
136, 131, 137, 135, 138, 136, 184, 137, 185, 138, 186, 184, 187, 185,
216, 186, 217, 187, 218, 216, 219, 217, 232, 218, 233, 219, 234, 232,
235, 233, 236, 234, 237, 235, 238, 236, 267, 237, 268, 238, 269, 267,
270, 268, 272, 269, 273, 270, 274, 272, 275, 273]
py>


I hope these hints were of some help. I'll leave the next steps as an
exercise for you (learning to debug is important, and hard!). If you
come back when you've made some more progress, (or got it working) I'll
show you some more elegant solutions (unless someone else beats me to
it, of course).

Good luck
Thomas


> 
> I appreciate your help 
> Thank you
> 




More information about the Python-list mailing list