I have a text file, which contains a list of element and their weight, and i need to split the list up and sort it by the weight of the elements. So i need
to change the weight to a float.
The text file:
Ac 227.0
Ag 107.868
Al 26.98154
Am 241.1
Ar 39.948
As 74.9216
...
So far i have done this:
Can anybody help me??
to change the weight to a float.
The text file:
Ac 227.0
Ag 107.868
Al 26.98154
Am 241.1
Ar 39.948
As 74.9216
...
So far i have done this:
Code:
# ----The data structure---- # The input file text_file=open("avikt.txt","r") Lines = text_file.readlines() text_file.close() # The Lines get broken into columns and in 1 list "Element" Element = map(string.split,Lines) # now Element is a nested list ... # [['Ac','227.0'],['Ag','107.868'],... # The problem now is to change the atom weight into a float # to sort it. # I make the Element list into a dictionary # where the Atom is the key and the atom weight # is the value d = dict(Element) # now i have to sort the dictionary by the value, or the atom weight items = [(v,k) for k, v in d.items()] items.sort() items = [(k,v) for v, k in items]
Comment