In [19]:
#-*- coding:utf-8 -*-
In [50]:
hightemp = "".join(map(str, [i.replace('\t', ' ') for i in open('hightemp.txt', 'r')]))
print(hightemp)
In [53]:
col1 = open('col1.txt', 'w')
col2 = open('col2.txt', 'w')
hightemp = [i.replace('\t', ' ').split() for i in open('hightemp.txt', 'r')]
col1.write("\n".join(map(str, [i[0] for i in hightemp])))
col1.close()
col2.write("\n".join(map(str, [i[1] for i in hightemp])))
col2.close()
In [13]:
%%timeit
col3 = open('col3.txt', 'w')
f1 = [i for i in open('col1.txt', 'r')]
f2 = [i for i in open('col2.txt', 'r')]
# [col3.write(i+'\t'+j) for i, j in zip(f1, f2)]
col3.close()
In [55]:
def display_nline(n, filename):
return "".join(map(str, [i for i in open(filename, 'r')][:n]))
display = display_nline(5, "col3.txt")
print(display)
In [85]:
def display_back_nline(n, filename):
return "".join(map(str, [i for i in open(filename, 'r')][-n-1:-1]))
display = display_back_nline(5, "col3.txt")
print(display)
In [6]:
%%timeit
def split_file(n, filename):
line = [i.strip('\n') for i in open(filename, 'r')]
length = len(line)
n = length//n
return [line[i:i+n] for i in range(0, length, n)]
split_file(2, "col1.txt")
In [19]:
def first_char(filename):
return set(i[0] for i in open(filename, 'r'))
print(first_char('hightemp.txt'))
In [10]:
%%timeit
from operator import itemgetter
def column_sort(sort_key, filename):
return sorted([i.split() for i in open(filename, 'r')], key=itemgetter(sort_key-1))
column_sort(3, 'hightemp.txt')
In [15]:
%%timeit
from operator import itemgetter
def frequency(filename):
first_char = [i[0] for i in open(filename, 'r')]
dictionary = set([(i, first_char.count(i)) for i in first_char])
return sorted(dictionary, key=itemgetter(1), reverse=True)
frequency('hightemp.txt')
In [16]:
%%timeit
from operator import itemgetter
first_char = [i[0] for i in open('hightemp.txt', 'r')]
dictionary = set([(i, first_char.count(i)) for i in first_char])
sorted(dictionary, key=itemgetter(1), reverse=True)
In [ ]: