0

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?

3
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by woooee
-1

output will be:

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

Edited by Xantipius: correction

0

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

Edited by woooee

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.