0

I'm given a text file that resembles the following...

hello 20
world 30
i'm 50
ok 20

I'm trying to use insertion sort to arrange the numerical part of the data. My code looks like the following...

def insertion_sort():
    filename = input('Enter filename: ')
    lst = []
    for line in open(filename):
        lst.append(int(line))
    print(lst)
    for index in range(1,len(lst)):
        value = lst[index]
        leftvalue = index -1
        while leftvalue >= 0 and lst[leftvalue] > value:
            if value < lst[leftvalue]:
                lst[leftvalue + 1] = lst[leftvalue]
                lst[leftvalue] = value
                leftvalue = leftvalue - 1
            else:
                break
    return lst == insertion_sort()

I'm getting the following error... ValueError: invalid literal for int() with base 10: 'Merchant_0 96918\n'

I have tried using float in replacement with int but I can't convert strings to floats.

Kara
  • 6,115
  • 16
  • 50
  • 57
steve
  • 61
  • 1
  • 1
  • 6
  • Well, what integer do you think that the string `Merchant_0 96918\n` should convert to? – Steve Jessop Oct 08 '13 at 16:02
  • It should convert to the number 96918 – steve Oct 08 '13 at 16:02
  • 1
    So what have you tried, in order to extract the part of the string that you want to convert to integer? What string-handling functions in Python do you know about? – Steve Jessop Oct 08 '13 at 16:04
  • We have covered most string functions except the strip function. I'm thinking of doing a bit of research on it to see how it'll help me in this case. – steve Oct 08 '13 at 16:08

1 Answers1

0

You can't get an integer with :

lst.append(int(line))

if line contains, say 'world 30'

What do you try to obtain exactly ? If you want to get the numeric value, try :

int(line.split(' ')[1])

Or better (works even if there are multiple spaces in the input lines) :

import re
reobj=re.search('(\d+)$',line)
int(reobj.group(1))

The whole sorting process becomes :

import operator
import re

lines = {}
for line in open('tst.txt'):
    reobj=re.search('(\d+)$',line)
    int_key=int(reobj.group(1))
    value=line[0:reobj.start()]
    lines[int_key]=value

# See : http://stackoverflow.com/questions/613183/python-sort-a-dictionary-by-value?rq=1
print "Sorted : ", sorted(lines.iteritems(), key=operator.itemgetter(0))