import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import statsmodels.api as sm
from scipy.stats import norm
from scipy.stats.stats import pearsonr
str(3)
int('5')
float('7.1')
range(10)
range(1, 10)
dir(str)[:5]
dir(str)[0:5]
dir(str)[-5:]
x = ' Hello WorlD '
dir(x)[-10:]
x.lower()
x.upper()
x.rstrip()
x.strip()
x.replace('lo', '')
x.split('lo')
','.join(['a', 'b'])
x = 'hello world'
type(x)
l = [1,2,3,3]
t = (1, 2, 3, 3)
s = set([1,2,3,3])
d = {'a':1,'b':2,'c':3}
a = np.array(l)
print l, t, s, d, a
l = [1,2,3,3]
l.append(4)
l
d = {'a':1,'b':2,'c':3}
d.keys()
d = {'a':1,'b':2,'c':3} # dict
d.values()
d = {'a':1,'b':2,'c':3} # dict
d['b']
d = {'a':1,'b':2,'c':3} # dict
d.items()
def devidePlus(m, n):
y = float(m)/n+ 1
return y
range(10)
range(1, 10)
for i in range(10):
print i, i*10, i**2
for i in range(10):
print devidePlus(i, 2)
r = [devidePlus(i, 2) for i in range(10)]
r
map(devidePlus, [4,3,2], [2, 1, 5])
map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
map(lambda x, y, z: x + y - z, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10], [3, 3, 2, 2, 5])
j = 3
if j%2 == 1:
print
elif j%2 ==2:
print
else:
print
j = 3
if j%2 == 1:
print "余数是1"
elif j%2 ==2:
print "余数是2"
else:
print "余数不是1也不是2"
x = 5
if x < 5:
y = -1
z = 5
elif x > 5:
y = 1
z = 11
else:
y = 0
z = 10
print(x, y, z)
j=0
while j<10:
print j
j+=1
j = 0
while j <50:
if j == 30:
break
if j%2 != 0:
print j**2
j+=1
a = 4
while a:
print a
a -= 1
if a < 0:
break
a = 4
while a:
print a
a -= 1
if a < 0:
a=None
for i in [2, 0, 5]:
try:
print devidePlus(4, i)
except Exception, e:
print e
pass
alist = [[1,1], [0, 0, 1]]
for aa in alist:
try:
for a in aa:
print 10 / a
except Exception, e:
print e
pass
alist = [[1,1], [0, 0, 1]]
for aa in alist:
for a in aa:
try:
print 10 / a
except Exception, e:
print e
pass
data =[[i, i**2, i**3] for i in range(10)]
data
for i in data:
print '\t'.join(map(str, i))
type(data)
len(data)
data[0:2]
data =[[i, i**2, i**3] for i in range(10000)]
f = open("/Users/chengjun/github/cjc/data/data_write_to_file.txt", "wb")
for i in data:
f.write('\t'.join(map(str,i)) + '\n')
f.close()
f = [1, 2, 3, 4, 5]
for k, i in enumerate(f):
print k, i
with open('/Users/chengjun/github/cjc/data/data_write_to_file.txt','r') as f:
for i in f:
print i
data =[[i, i**2, i**3] for i in range(100)]
f = open("/Users/jamiezhu/data_write_to_file.txt", "wb")
for i in data:
f.write('\t'.join(map(str,i)) + '\n')
f.close()
data =[[i, i**2, i**3] for i in range(100)]
f = open("/jamiezhu/data_write_to_file.txt", "wb")
for i in data:
f.write('\t'.join(map(str,i)) + '\n')
f.close()
data =[[i, i**2, i**3] for i in range(100)]
f = open("/Users/conghuizhu/downloads/data_write_to_file.txt", "wb")
for i in data:
f.write('\t'.join(map(str,i)) + '\n')
f.close()
with open('/Users/conghuizhu/downloads/data_write_to_file.txt','r') as f:
data = f.readlines()
data[:5]
with open('/Users/conghuizhu/downloads/data_write_to_file.txt','r') as f:
data = f.readlines(100)
len(data)
with open('/Users/conghuizhu/downloads/data_write_to_file.txt','r') as f:
print f.readline()
f = [1, 2, 3, 4, 5]
for k, i in enumerate(f):
print k, i
with open('/Users/conghuizhu/downloads/data_write_to_file.txt','r') as f:
for i in f:
print i
with open('/Users/conghuizhu/downloads/data_write_to_file.txt','r') as f:
for k, i in enumerate(f):
if k%2000 ==0:
print i
data = []
line = '0\t0\t0\n'
line = line.replace('\n', '')
line = line.split('\t')
line = [int(i) for i in line] # convert str to int
data.append(line)
data
data = []
with open('/Users/chengjun/github/cjc/data/data_write_to_file.txt','r') as f:
for line in f:
#line = line.replace('\n', '').split('\t')
#line = [int(i) for i in line]
data.append(line)
data
data = []
with open('/Users/conghuizhu/downloads/data_write_to_file.txt','r') as f:
for line in f:
#line = line.replace('\n', '').split('\t')
#line = [int(i) for i in line]
data.append(line)
data
import pandas as pd
df = pd.read_csv('/Users/chengjun/github/cjc/data/data_write_to_file.txt', sep = '\t', names = ['a', 'b', 'c'])
df[:5]
df = pd.read_csv('/Users/conghuizhu/downloads/data_write_to_file.txt', sep = '\t', names = ['a', 'b', 'c'])
df[:5]
import json
data_dict = {'a':1, 'b':2, 'c':3}
with open('/Users/chengjun/github/cjc/save_dict.json', 'w') as f:
json.dump(data_dict, f)
import json
data_dict = {'a':1, 'b':2, 'c':3}
with open('/Users/conghuizhu/downloads/save_dict.json', 'w') as f:
json.dump(data_dict, f)
dd = json.load(open("/Users/conghuizhu/downloads/save_dict.json"))
dd
data_list = range(10)
with open('/Users/chengjun/github/cjc/save_list.json', 'w') as f:
json.dump(data_list, f)
data_list = range(10)
with open('/Users/conghuizhu/downloads/save_list.json', 'w') as f:
json.dump(data_list, f)
dl = json.load(open("/Users/chengjun/github/cjc/save_list.json"))
dl
dl = json.load(open("/Users/conghuizhu/downloads/save_list.json"))
dl
import dill # pip insstall dill
# http://trac.mystic.cacr.caltech.edu/project/pathos/wiki/dill
def myFunction(num):
return num,num
with open('/Users/conghuizhu/downloads/data.pkl', 'wb') as f:
dill.dump(myFunction, f)
with open('/Users/conghuizhu/downloads/data.pkl', 'r') as f:
newFunction = dill.load(f)#, strictio=strictio))
newFunction('hello')