How to store some elements from a list into another

breamoreboy at gmail.com breamoreboy at gmail.com
Mon Jun 12 22:37:00 EDT 2017


On Monday, June 12, 2017 at 7:33:03 PM UTC+1, 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:
> 
>         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]:
> 
>             list7.append(list6[x][y])
>             list7.append(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
> 
> 
> It does not work
> 
> I appreciate your help 
> Thank you

Perhaps use the recipe for consecutive numbers from here https://docs.python.org/2.6/library/itertools.html#examples  It will have to be modified for Python 3, I'll leave that as an exercise.

Kindest regards.

Mark Lawrence.



More information about the Python-list mailing list