python学习中遇到的一些Question?

mac2022-06-30  33

Q1:文件操作时,打开文件(句柄)赋值给变量时是一个可迭代对象?可以用for直接遍历?

old_f=open('file.txt','r') for line in old_f: print(line.strip())

  

Q2:在高阶函数中,返回值也是一个可以迭代对象?怎么样才能打印输出?

def fun(x,y,z): return x+y+z r=map(fun,[1,2,3,4],[3,4,5,6],[4,5,6]) print(list(r)) 为什么要用list对象?

  

Q3:dict字典和str 字符串转换?

import simplejson as json #将str类型转化为dict policy = json.loads(p) #将dict类型转化为str p = json.dumps(policy)

  

转载于:https://www.cnblogs.com/ronnybin/p/7327665.html

最新回复(0)