-4

I have a series of lists created from an imported file with numerous lines of data. However, when creating the list from the file, a list was also created for the headings. I have managed to delete the headers from the first list but have now been left with:

['', '', '', '']
['2.3', '82.2', '0.6', '1.5']
['3.6', '92.9', '0.5', '2.1']
['6.3', '82.9', '0.7', '2.1']
['7.0', '70.8', '0.5', '1.8']
['7.7', '56.3', '0.4', '2.0']
['8.3', '97.0', '0.8', '1.8']
['10.4', '67.0', '0.6', '1.5']
['11.8', '89.3', '0.7', '1.4']
['13.0', '75.8', '0.8', '1.3']
['14.1', '77.1', '0.6', '1.7']
['15.8', '74.6', '0.6', '1.8']
['16.9', '69.0', '0.4', '2.5']
['18.4', '89.9', '0.6', '2.4']
['20.3', '93.5', '0.9', '2.3']
['21.4', '80.9', '0.6', '1.9']
['21.9', '81.6', '0.9', '2.2']
['23.5', '65.0', '0.6', '2.5']
['24.4', '78.4', '0.4', '1.8']
['27.2', '81.5', '0.7', '2.3']
['28.8', '73.4', '0.4', '1.7']

Code:

def createPersonList(fileName):
    theFile = open(fileName)
    for line in theFile:
        aList = line.split(',')
        bList = map(lambda s: s.strip('\n'), aList)
        cList = map(lambda s: s.strip('ArrivalTime (s)'' Weight (kg)'' gait (m)' ' speed (m/s)'), bList)
        print cList

Input:

>>>createPersonList('filename')

I would like to completely delete/remove the first list ['', '', '', ''] but am struggling to do so.

Student1001
  • 43
  • 1
  • 2
  • 7
  • Can you show your raw input data and your code to generate the above, thanks – EdChum Jan 05 '16 at 10:11
  • I'm guessing what you're trying to do here. My brother did the same when I introduced to Python. If you need a two-dimensional (or larger) array, look at `numpy`: http://www.numpy.org/ – Aleksander Lidtke Jan 05 '16 at 10:14
  • Added code and raw input data – Student1001 Jan 05 '16 at 10:16
  • Possible duplicate of [How to remove an element from a list by index in Python?](http://stackoverflow.com/questions/627435/how-to-remove-an-element-from-a-list-by-index-in-python) – SiHa Jan 05 '16 at 10:18

2 Answers2

1

You can simply skip the first line with the built-in function next():

def createPersonList(fileName):
    theFile = open(fileName)
    next(theFile) # skips the first line
    for line in theFile: # goes through the rest of the lines in the file
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • @Student1001 - Glad to hear it. You can indicate to future readers that this answer solved your issue by clicking the "accept" button below its score. – TigerhawkT3 Jan 05 '16 at 10:28
0

Try this

remove = ['', '', '']
print [data for data in s if all(x not in remove for x in data)]

The remove list contains the exact same list as what you want to remove and the s is the complete full list containing all sublists

FYI s is

s=[['', '', '', ''],
['2.3', '82.2', '0.6', '1.5'],
['3.6', '92.9', '0.5', '2.1'],
['6.3', '82.9', '0.7', '2.1'],
['7.0', '70.8', '0.5', '1.8'],
['7.7', '56.3', '0.4', '2.0'],
['8.3', '97.0', '0.8', '1.8'],
['10.4', '67.0', '0.6', '1.5'],
['11.8', '89.3', '0.7', '1.4'],
['13.0', '75.8', '0.8', '1.3'],
['14.1', '77.1', '0.6', '1.7'],
['15.8', '74.6', '0.6', '1.8'],
['16.9', '69.0', '0.4', '2.5'],
['18.4', '89.9', '0.6', '2.4'],
['20.3', '93.5', '0.9', '2.3'],
['21.4', '80.9', '0.6', '1.9'],
['21.9', '81.6', '0.9', '2.2'],
['23.5', '65.0', '0.6', '2.5'],
['24.4', '78.4', '0.4', '1.8'],
['27.2', '81.5', '0.7', '2.3'],
['28.8', '73.4', '0.4', '1.7']]

Cheers

sameera sy
  • 1,708
  • 13
  • 19