Select sections of a list

0

If I have a list in Python:

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

How can I make a selection of several sections of the list that are merged into another list?

For example, select from position 0 to 2 ( ['a', 'b', 'c'] ) and from position 5 to the end ( ['f', 'g', 'h'] ) and get as a result the list ['a', 'b', 'c', 'f', 'g', 'h']

I edit to specify a little more, following the FJSevilla recommendation, to which I thank you for your contribution. What I'm looking for is this:

>>> # Tengo una lista con datos de una variable en tres dimensiones, por ejemplo
>>> data=[
...     [
...     [22,25,31,24,32,12,13],
...     [13,26,14,11,12,24,22],
...     [10,23,14,22,33,14,24]
...     ],
...     [
...     [13,24,31,24,14,24,34],
...     [11,22,33,13,23,42,12],
...     [14,52,12,33,22,44,12]
...     ]
... ]
>>> # De ella quiero sacar una lista con los datos de las dos primeras variables
>>> # y segmentos escogidos por mi de la ultima en cualquier orden, por ejemplo [5:]+[:3]
>>> # buscando obtener un output como esto:
output=[
[
[12 13 22 25 31]
[24 22 13 26 14]
[14 24 10 23 14]
]
[
[24 34 13 24 31]
[42 12 11 22 33]
[44 12 14 52 12]
]
]
>>> #He probado asi pero me da error de sintaxis:
>>> out=data[:][:][[5:]+[3:]]
 File "<stdin>", line 1
out=data[:][:][[5:]+[3:]]
                 ^
SyntaxError: invalid syntax

>>> #Así obtengo el resultado deseado:
>>> out=data[0][0][5:]+data[0][0][:3]
>>> print out
[12, 13, 22, 25, 31]

>>> #Yo quiero que me imprima eso, para todo x e y:
>>> #Pero cuando hago lo siguiente me imprime toda la matriz de datos:
>>> out= data[:][:][5:]+data[:][:][:3]
>>> print out
[[[22, 25, 31, 24, 32, 12, 13], [13, 26, 14, 11, 12, 24, 22], [10, 23, 14, 22, 33, 14, 24]], [[13, 24, 31, 24, 14, 24, 34], [11, 22, 33, 13, 23, 42, 12], [14, 52, 12, 33, 22, 44, 12]]]
    
asked by Atreyuk 26.09.2017 в 23:56
source

2 answers

1

You use slicing or slicing lists and then concatenates:

>>> l = ["a", "b", "c", "d", "e", "f", "g", "h"]
>>> sl = l[:3] + l[5:]
>>> sl
['a', 'b', 'c', 'f', 'g', 'h']

The general syntax is:

lista[start:end:step] 

A few observations:

  • The element with index defined in start is included in the cut, the element corresponding to end is not included:

    >>> l = ["a", "b", "c", "d", "e", "f", "g", "h"]
    >>> l[1:3]
    ['b', 'c']
    
  • If start or end is not defined, it is understood that it is from the beginning of the list or until the end of the list respectively (including in this case the last element):

    >>> l[:5]
    ['a', 'b', 'c', 'd', 'e']
    
    >>> l[4:]
    ['e', 'f', 'g', 'h']
    
    >>> l[:]
    ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
    
  • With step we define the jump we give between elements:

    >>> l[::2]
    ['a', 'c', 'e', 'g']
    

    As we can see, it returns the complete list but taking only every two elements.

  • Negative indexes are allowed as is logical (as when accessing a list by index):

    >>> l[3:-2]
    ['d', 'e', 'f']
    
  • The step can also be negative, which allows for example to invert the list:

    >>> l[::-1]
    ['h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
    
    >>> l[::-2]
    ['h', 'f', 'd', 'b']
    
  • What is returned is a new object, a new list. With l[:] we get a copy of the complete original list ( shallow copy ):

    >>> lista = [1, 1, [3]]
    >>> copia = lista[:]
    >>> copia == lista
    True
    >>> id(copia) == id(lista)
    False
    >>> copia
    [1, 1, [3]]
    >>> lista
    [1, 1, [3]]
    >>> copia[2].append(4)
    >>> copia[0] = 14  
    >>> lista
    [1, 1, [3, 4]]
    >>> copia
    [14, 1, [3, 4]]
    
answered by 27.09.2017 в 00:00
0

In the end I found a way to do it. I leave here this solution that I found reading about operations with matrices in Python in case someone is useful. If someone comes up with a better, simpler one, please put it on.

>>> matriz = [[data[i][j][5:]+data[i][j][:3] for j in range(3)] for i in range(2)]
>>> print matriz
[[[12, 13, 22, 25, 31], [24, 22, 13, 26, 14], [14, 24, 10, 23, 14]], [[24, 34, 13, 24, 31], [42, 12, 11, 22, 33], [44, 12, 14, 52, 12]]]
    
answered by 27.09.2017 в 21:23