fout = open( 'tf2 file test.txt', 'r' )
fout.seek(0)
items = []
for index, line in enumerate( fout.readlines( ) ):
    val = line.split('=').strip()
    items.append(val)
    
for item in items:
    print item

For some reason it's telling me that strip is undefined?

Ok, I fixed it, now I need help with a sorting alrogithm, I need it to sort items from a list into another list depending on the lists [1] item, like it needs to order the items from one list's [1] item from smallest to highest into another list, makes sense?

Oh, and tf3 file test's contents are:
map1=2
map2=4
map3=1
map4=3
map5=5

Like from: items = [['map2', '2'], ['map1', '1'], ['map3', '3']] to: items = [['map1', '1'], ['map2', '2'], ['map3', '3']]

mylist = [['map2', '2'], ['map1', '1'], ['map3', '3']]

# by default sorts by item in index 0 of the sublists
print(sorted(mylist))

"""
my output -->
[['map1', '1'], ['map2', '2'], ['map3', '3']]
"""

I tried it and it didnt make any difference, heres the line i added: print sorted(items) All the other code is the same

The nice thing about using the print() function is that it works with Python25, Python26 and the new Python30. Again ...

# sort a lis of [name, age] lists by name and then age
# works with Python25 and Python30

list1 = [['zoe', 29], ['ardy', 77], ['moe', 17]]
print(list1)
# default sorts by item in index 0 (the name)
list2 = sorted(list1)
print(list2)
# use key to sort by item in index 1 (the age)
list3 = sorted(list1, key=lambda x: x[1])
print(list3)
"""
my output -->
[['zoe', 29], ['ardy', 77], ['moe', 17]]
[['ardy', 77], ['moe', 17], ['zoe', 29]]
[['moe', 17], ['zoe', 29], ['ardy', 77]]
"""

I want it to sort both the map name and the map index though should I use a dictionary instead?

I want it to sort both the map name and the map index though should I use a dictionary instead?

If you want to look up the name or index then use one dictionary for each, unless there is a very large data set. If you want to sort and print, then sort using a normal items.sort() or
items.sort(key=operator.itemgetter(-1)) for the last element

This article has been dead for over six months. Start a new discussion instead.