I have two files, "ranked.txt" and "sorted.txt". Sorted.txt is a smaller subset from ranked.txt that is sorted in alpha order. However ranked.txt preserves the ranking of words I would like to keep.

How do I check the rank of every word in sorted.txt when matched to the original ranked.txt? I want to output the rank (which is the line number) from ranked.txt into sorted.txt.

ranked.txt:
1 the
2 quick
3 brown
4 fox
5 jumped

sorted:
brown
fox
jumped
quick
the

output.txt:
3 brown
4 fox
5 jumped
2 quick
1 the

This is what I have so far:

for word in sorted_list:
    for num, line in enumerate(ranked_list, 1):
        if word in line:
            print word, '\t', num

But this doesn't do exactly what I want... anyone help?

r = ['1 zzz', '2 bbb', '3 hhhhh', '4 c']
r.sort(key=lambda x: x[x.find(' '):])
print r

output will be:

['2 bbb', '4 c', '3 hhhhh', '1 zzz']

But this doesn't do exactly what I want... anyone help?

How are we supposed to know what this means?

In general, you want to compare words to words. Your code will also find sub-words, ie. "the" is in "then" and "and" is in "random", so use == instead for word to word, or better yet create a dictionary of words pointing to their ranking and look up each word in the dictionary.

Edit: duplicate of this post http://python-forum.org/pythonforum/viewtopic.php?f=18&t=36620

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.