0

I want to iterate through list of list. To iterate through every list inside list also.

list=[[0.9 0.8 0.1 0.2 0.5 ][0.5 0.3 0.2 0.1 0.7 ][0.6 0.1 0.3 0.2  0.9][0.3 0.7 0.4 0.1 0.8]]

Thus to iterate through every list inside, only to the third position, eg:

list=[[0.9 0.8 0.1][0.5 0.3 0.2][0.6 0.1 0.3][0.3 0.7 0.4 ]]

Somebody tell me, how can do this? This is my code:

list=[]
i=0
j=0
data=open('BDtxt.txt','r')
for line in data.xreadlines():
    lista.append(line.strip().split())
while i<len(lista):
    while j < len(lista[i]):
        print lista[j]
        j+=1
    i+=1

and the output is:

['0.9', '0.8', '0.1', '0.2', '0.5']
['0.5', '0.3', '0.2', '0.1', '0.7']
['0.6', '0.1', '0.3', '0.2', '0.9']
['0.3', '0.7', '0.4', '0.1', '0.8']

and I want the output to be

[0.9 0.8 0.1]
[0.5 0.3 0.2]
[0.6 0.1 0.3]
[0.3 0.7 0.4]
smci
  • 32,567
  • 20
  • 113
  • 146
Angel
  • 11
  • 3
  • Also, please read the Python documentation on looping – thefourtheye Feb 23 '15 at 02:18
  • I'm sorry salida is j – Angel Feb 23 '15 at 02:23
  • If you just want to print an output slice, use `print(a[:,i:j])`. And if you want more control on output format, see [np.set_printoptions](http://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html) and duplicate questions like [Pretty-printing of numpy.array](http://stackoverflow.com/questions/2891790/pretty-printing-of-numpy-array). There are tons of duplicate questions on all of that. – smci Feb 23 '15 at 02:44

1 Answers1

3

This is called taking a slice of an array (not iterating or looping). Use numpy.array. To read in your csv file use either numpy.genfromtxt() or pandas.read_csv() - there are tons of duplicate questions on SO for those.

import numpy as np
a = np.array([[0.9,0.8,0.1,0.2,0.5], [0.5,0.3,0.2,0.1,0.7], [0.6,0.1,0.3,0.2,0.9], [0.3,0.7,0.4,0.1,0.8]])

a[:,0:3]
array([[ 0.9,  0.8,  0.1],
       [ 0.5,  0.3,  0.2],
       [ 0.6,  0.1,  0.3],
       [ 0.3,  0.7,  0.4]])
Community
  • 1
  • 1
smci
  • 32,567
  • 20
  • 113
  • 146
  • what if numpy is not avaliable? – Marcin Feb 23 '15 at 02:25
  • @Marcin: install it. numpy and pandas. Make your life easy. If your IT dept blocks installs, take it up with them. There's really no point in wasting time on a weak native Python rewrite of these superb packages that contain many man-years of work. – smci Feb 23 '15 at 02:35
  • And how can i do it ? if my list inside change of value to two and display something like this: [0.9 0.8] [0.5 0.3] [0.6 0.1] [0.3 0.7] Can be iterable? – Angel Feb 23 '15 at 02:36
  • Angel, then take the slice `a[:,0:2]`. You can use variables as slice indices e.g. `a[:,i:j]` and run a for-loop over ,ij or whatever. Please read the [numpy Basics of indexing](http://docs.scipy.org/doc/numpy/user/basics.indexing.html) documentation. You can pretty much do anything you might ever want. – smci Feb 23 '15 at 02:38
  • But if you only want to print a slice of the array, no need for nested loops. Just directly do `print(a[:,0:3])` – smci Feb 23 '15 at 02:40
  • And if you want more control on output format, see [np.set_printoptions](http://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html) and duplicate questions like [Pretty-printing of numpy.array](http://stackoverflow.com/questions/2891790/pretty-printing-of-numpy-array) – smci Feb 23 '15 at 02:42
  • @smci I have installed numpy and when i want do a iteration displays a error – Angel Feb 23 '15 at 02:45
  • Angel, you need to show us the error message. (Did `import numpy as np` work correctly? Is your PYTHONPATH correct? etc.) And search SO and Google to find the solution. – smci Feb 23 '15 at 02:46
  • smci Can i do something like this? i=3 a[:,0:i] Further i have to find the min of each list inside but first i need do this – Angel Feb 23 '15 at 02:48
  • Well @smci you gave me some ideas, Thank you – Angel Feb 23 '15 at 03:00
  • You're welcome! numpy and pandas are brilliant, check them out! – smci Feb 23 '15 at 04:47
  • Hi! @smci I have a trouble, I want to get the minimum of a row 0.9 0.8 0.1 minimum = .1, 0.5 0.3 0.2 minimum = .2, 0.6 0.1 0.3 minimum = .1, 0.3 0.7 0.4 minimum = .3, – Angel Feb 25 '15 at 03:41
  • And my code is this : import numpy as np data=np.genfromtxt('BDentrada.txt') j=0; lista=[]; minimo=0.0; fijo=0; while j<=3: minimo=min(data[:,fijo:j]) print data j+=1 ------------------------------------------------------------------------------------------- But display me a error: Traceback (most recent call last): File "test2.py", line 11, in minimo=min(data[:,fijo:j]) ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() – Angel Feb 25 '15 at 03:45
  • @Angel: first make sure the type of entries in `data[]` got read in as integer, not logical or string. Then, Python is telling you it objects to calling min on an array (or array slice). `min(data[:,fijo:j])`. For solution, see these posts: http://stackoverflow.com/search?q=%5Bnumpy%5D+min+array . Please read those before opening a new question. – smci Feb 25 '15 at 10:56