User:Mths/rwrm writing machines: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "== script == <source lang="python"> import re, operator, sys fn = sys.argv[1] with open(fn, "r") as f: text = f.read() words = re.findall(r'\w+', text) d = {} for j in ...")
 
No edit summary
Line 31: Line 31:


</source>
</source>
==text==

Revision as of 12:01, 12 June 2013

script

import re, operator, sys

fn = sys.argv[1]

with open(fn, "r") as f:
	text = f.read()
	words = re.findall(r'\w+', text)

d = {}

for j in words:
    d[j] = d.get(j,0) + 1

sorted_d = sorted(d.iteritems(), key=operator.itemgetter(1))
newwords = []
templist = []

for string, i in sorted_d:
	while i > 0: 
		templist.append(string)	
		i -= 1

	newwords = templist
	#templist = []
	str1 = ' '.join(newwords)

print str1

text