11

I have a list of the following form:

[[0, 5.1, 3.5, 1.4, 0.2],
 [0, 4.9, 3.0, 1.4, 0.2],
 [0, 4.7, 3.2, 1.3, 0.2],
 [1, 4.6, 3.1, 1.5, 0.2],
 [1, 5.0, 3.6, 1.4, 0.2],
 [1, 5.4, 3.9, 1.7, 0.4],
 [1, 4.6, 3.4, 1.4, 0.3]]

I want to slice out the first column and add it as a new element to each row of data (so at each odd position in the list), changing it to the following form:

[[5.1, 3.5, 1.4, 0.2], [0],
 [4.9, 3.0, 1.4, 0.2], [0],
 [4.7, 3.2, 1.3, 0.2], [0],
 [4.6, 3.1, 1.5, 0.2], [1],
 [5.0, 3.6, 1.4, 0.2], [1],
 [5.4, 3.9, 1.7, 0.4], [1],
 [4.6, 3.4, 1.4, 0.3], [1],]

How could I do this?

So far, I have extracted the necessary information in the following ways:

targets = [element[0] for element in dataset]
features = dataset[1:]
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
d3pd
  • 7,935
  • 24
  • 76
  • 127
  • Not quite a duplicate, but see [here](http://stackoverflow.com/questions/34057294/flat-list-as-a-result-of-list-comprehension). – TigerhawkT3 Dec 05 '15 at 06:31

5 Answers5

5

Try indexing and then get flattened list- i used list comprehension for flattening.

>>>l=[[0, 5.1, 3.5, 1.4, 0.2],
 [0, 4.9, 3.0, 1.4, 0.2],
 [0, 4.7, 3.2, 1.3, 0.2],
 [1, 4.6, 3.1, 1.5, 0.2],
 [1, 5.0, 3.6, 1.4, 0.2],
 [1, 5.4, 3.9, 1.7, 0.4],
 [1, 4.6, 3.4, 1.4, 0.3]]
>>>[[i[1:],[i[0]]] for i in l]#get sliced list of lists
>>>[[[5.1, 3.5, 1.4, 0.2], [0]], [[4.9, 3.0, 1.4, 0.2], [0]], [[4.7, 3.2, 1.3, 0.2], [0]], [[4.6, 3.1, 1.5, 0.2], [1]], [[5.0, 3.6, 1.4, 0.2], [1]], [[5.4, 3.9, 1.7, 0.4], [1]], [[4.6, 3.4, 1.4, 0.3], [1]]]
>>>d=[[i[1:],[i[0]]] for i in l]
>>>[item for sublist in d for item in sublist]#flatten list d
>>>[[5.1, 3.5, 1.4, 0.2], [0], [4.9, 3.0, 1.4, 0.2], [0], [4.7, 3.2, 1.3, 0.2], [0], [4.6, 3.1, 1.5, 0.2], [1], [5.0, 3.6, 1.4, 0.2], [1], [5.4, 3.9, 1.7, 0.4], [1], [4.6, 3.4, 1.4, 0.3], [1]]

Just oneliner alternative-

[item for sublist in [[i[1:],[i[0]]] for i in l] for item in sublist] #Here l is that list
Learner
  • 5,192
  • 1
  • 24
  • 36
4

List comprehensions are nice but can be a bit hard to scan. Loops are still useful, especially when combined with extend:

res = []
for entry in dataset:
    res.extend([entry[1:], entry[:1]])

now:

import pprint    
pprint.pprint(res)

prints:

[[5.1, 3.5, 1.4, 0.2],
 [0],
 [4.9, 3.0, 1.4, 0.2],
 [0],
 [4.7, 3.2, 1.3, 0.2],
 [0],
 [4.6, 3.1, 1.5, 0.2],
 [1],
 [5.0, 3.6, 1.4, 0.2],
 [1],
 [5.4, 3.9, 1.7, 0.4],
 [1],
 [4.6, 3.4, 1.4, 0.3],
 [1]]
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • Best answer just because you have not used a list comprehension... `[r.extend((el[1:],el[:1])) for r in [[]] for el in dataset]; print r` – gboffi Dec 05 '15 at 08:39
  • @gboffi Nice try. But it does not work in Python 3 because `r` is not defined after the list comprehension. You would need two lines `res = []; [res.extend([entry[1:], entry[:1]]) for entry in dataset]`. List comprehensions with side effects are not really nice. Typically you want a useful return value. – Mike Müller Dec 05 '15 at 08:47
  • Well, I've learned about the side effects in P2's list comprehensions the hard way... personally I'm very happy of the new behaviour and the code in my comment was just a joke. – gboffi Dec 05 '15 at 09:05
  • Maybe a candidate for a code-examples-version of https://pypi.python.org/pypi/pyjokes ;). – Mike Müller Dec 05 '15 at 09:14
2

Slice each sublist and make a new list with an element for each slice:

l = [[0, 5.1, 3.5, 1.4, 0.2],
 [0, 4.9, 3.0, 1.4, 0.2],
 [0, 4.7, 3.2, 1.3, 0.2],
 [1, 4.6, 3.1, 1.5, 0.2],
 [1, 5.0, 3.6, 1.4, 0.2],
 [1, 5.4, 3.9, 1.7, 0.4],
 [1, 4.6, 3.4, 1.4, 0.3]]

 

>>> print(*[item for sub in l for item in (sub[1:], [sub[0]])], sep='\n')
[5.1, 3.5, 1.4, 0.2]
[0]
[4.9, 3.0, 1.4, 0.2]
[0]
[4.7, 3.2, 1.3, 0.2]
[0]
[4.6, 3.1, 1.5, 0.2]
[1]
[5.0, 3.6, 1.4, 0.2]
[1]
[5.4, 3.9, 1.7, 0.4]
[1]
[4.6, 3.4, 1.4, 0.3]
[1]
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • Using the print on the list comprehension makes it rather difficult to scan. Using two lines and having the print separated would be easier on the eye. Also, you can use `pprint`. – Mike Müller Dec 05 '15 at 07:20
2

Try this:

from itertools import chain
print list(chain(*[list((element[1:],[element[0]])) for element in a]))

Output:

[[5.1, 3.5, 1.4, 0.2], [0], [4.9, 3.0, 1.4, 0.2], [0],
 [4.7, 3.2, 1.3, 0.2], [0], [4.6, 3.1, 1.5, 0.2], [1], 
 [5.0, 3.6, 1.4, 0.2], [1], [5.4, 3.9, 1.7, 0.4], [1], 
 [4.6, 3.4, 1.4, 0.3], [1]]
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
  • Instead of unpacking you can use `list(itertools.chain.from_iterable([list((element[1:],[element[0]])) for element in l]))` – Learner Dec 05 '15 at 07:02
1

A Pythonic approach in python 3.X using unpacking iteration and itertools.chain:

>>> from itertools import chain
>>> 
>>> list(chain.from_iterable([[j,[i]] for i,*j in A]))
[[5.1, 3.5, 1.4, 0.2], [0], 
 [4.9, 3.0, 1.4, 0.2], [0], 
 [4.7, 3.2, 1.3, 0.2], [0], 
 [4.6, 3.1, 1.5, 0.2], [1], 
 [5.0, 3.6, 1.4, 0.2], [1], 
 [5.4, 3.9, 1.7, 0.4], [1], 
 [4.6, 3.4, 1.4, 0.3], [1]]
Mazdak
  • 105,000
  • 18
  • 159
  • 188