User:Mths/rwrm writing machines

From XPUB & Lens-Based wiki
< User:Mths
Revision as of 13:00, 12 June 2013 by Mths (talk | contribs) (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 ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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