Gromit
Gromit

Reputation: 77

slicing lists not having the correct out come. Python 3

I have some data in a file which i have converted in (int) 's and appended into a list.

minValue = open ("Mineral Values.csv", 'r')
refineList = []

for line in minValue:
    tritanium, pyerite, mexallon, isogen, nocxium, megacyte, zydrine, morphite = line.split(',')
    trit = int(tritanium)
    pye = int(pyerite)
   mexa = int(mexallon)
    iso = int(isogen)
    nocx = int(nocxium)
    mega = int(megacyte)
    zyd = int(zydrine)
    morp = int(morphite)
    refineList.append ([trit, pye, mexa, iso, nocx, mega, zyd, morp])
    minValue.close()

If i print this out it prints out all the data in the file. [[415, 0, 0, 0, 0, 0, 0, 0], [436, 0, 0, 0, 0, 0, 0, 0], [457, 0, 0, 0, 0, 0, 0, 0], [346, 173, 0, 0, 0, 0, 0, 0], etc etc

But i would like to slice a portion of these lists out so i can assign them to variables to do some math on. the code im using is the following

refineList[0:3]
print (refineList)

This should just print out 415 of the first list i thought? but instead it does nothing and just prints out the entire list again. Ive looked around on the site and all say using the slice method should work so what am i missing?

Thanks in advance

Upvotes: 0

Views: 47

Answers (1)

litepresence
litepresence

Reputation: 3277

a = [[415, 0, 0, 0, 0, 0, 0, 0], [436, 0, 0, 0, 0, 0, 0, 0], [457, 0, 0, 0, 0, 0, 0, 0], [346, 173, 0, 0, 0, 0, 0, 0]]

print a[0] # print first list in my list of lists
print a[0][0] # print first element in first list in my list of lists

>>>
[415, 0, 0, 0, 0, 0, 0, 0]
415

Upvotes: 2

Related Questions