How to store some elements from a list into another

MRAB python at mrabarnett.plus.com
Mon Jun 12 14:54:16 EDT 2017


On 2017-06-12 19: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:
> 
>          x=0
>          y=0

Here you're checking whether list6[x][y+1] is list6[x][y]+1 _and_ 
list6[x][y]+2 _and_ list6[x][y]+3, all at the same time. That's not 
going to happen!

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




More information about the Python-list mailing list