Jman
BAN USER
Comments (3)
Reputation 20
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
line = "Hi I am there, am good"
table = dict()
for word in line.split():
word = ''.join(e for e in word if e.isalnum()) # Strip special characters
if word not in table:
table[word] = 1
else:
table[word] += 1
sorted_table = sorted(table.iteritems(), key = lambda (k, v): (-v, k))
print sorted_table
Output:
[('am', 2), ('Hi', 1), ('I', 1), ('good', 1), ('there', 1)]
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
I solved this by scanning each line for the IP address, incrementing the respective count in a hash, then sorting the hash by value in the end.
- Jman March 12, 2013