Python入门 六、像个 Pythonista

pickle

import pickle

test_data = ['Save me!',123.456,True]

f = file('test.data','w')

pickle.dump(test_data,f)

f.close()
import pickle

f = file('test.data')

test_data = pickle.load(f)

f.close()

print test_data
import pickle

test_data = ['Save me!', 123.456, True]

a = 123

b = "hello"

c = 0.618

data = (a,b,c)

f = file('test.data','w')

pickle.dump(test_data,f)

pickle.dump(data,f)

f.close()
import pickle

test_data = ['Save me!', 123.456, True]

a = 123

b = "hello"

c = 0.618

e = 789

f1 = "helloc"

g = 0.719

data = (a,b,c)

data1 = (e,f1,g)

f = file('test.data','w')

pickle.dump(test_data,f)

pickle.dump(data,f)

pickle.dump(a,f)

pickle.dump(b,f)

pickle.dump(c,f)

pickle.dump(data1,f)


pickle.dump(e,f)

pickle.dump(f1,f)

pickle.dump(g,f)

f.close()
import pickle

f = file('test.data')

test_data = pickle.load(f)

x =  pickle.load(f)

y =  pickle.load(f)

z =  pickle.load(f)

f.close()

print test_data

print x

print y

print z

列表解析
 
在实际开发中,适当地使用列表综合可以让代码更加简洁、易读,降低出错的可能。
 
list_1 = [1,2,3,5,8,13,22]

list_2 = []

for i in list_1:

   if  i % 2 ==0:

    print i

    list_2.append(i)

    print list_2

    
   
list_1 = [1,2,3,5,8,13,22]

list_2 = [i for i in list_1 if i % 2 ==0]

list_2 = [i/2 for i in list_1 if i % 2 ==0]

print list_2

lambda 表达式

 
 

转载于:https://www.cnblogs.com/ZHONGZHENHUA/p/7426375.html

你可能感兴趣的:(python)