python_基础04
2015-09-04 12:54
615 查看
将元组转化为list列表 myli2 = list((1,2,3)) : [1, 2, 3] cmp:比较两个列表的大小 **根据最短的哪个生成元组长度的。** zip("abckdfe",[1,2,3,3,]) [('a', 1), ('b', 2), ('c', 3), ('k', 3)]
#给定一个列表,统计列表中元素个数 name = "zhangsan" sorce_1 = [90,23,89]; sorce_2 = [21,45,89]; source = [name,sorce_1,sorce_2]; count =0; len_sorce = len(source); i =0; while(i < len_sorce): #判断列表中的元素是不是列表类型的数据: isinstance if(isinstance(source[i], list)): count += len(source[i]) else: count +=1; i+=1; print("%s count=%d"%(source,count));
#模拟堆栈数据结构 stack = []; #存入数据 def stack_push(): val = raw_input("push val:") stack.append(val) #移出数据 def stack_pop(): if(len(stack)>0): print stack.pop(-1) #弹出最上面的这个值 #显示数据 def stack_show(): print stack #主函数 while(True): cmd = raw_input("enter cmd:") if(cmd == 'p'): stack_push() elif(cmd == 'o'): stack_pop() elif(cmd == 's'): stack_show() elif(cmd == 'q'): break else: pass
#模拟队列数据结构 seq = []; #入队 def seq_enter(): val = raw_input("push val:") seq.append(val) #出队 def seq_out(): if(len(seq)>0): return seq.pop(0) #队列:先进先出 else: return None #显示数据 def seq_show(): print seq #主函数 while(True): cmd = raw_input("enter cmd:") if(cmd == 'p'): seq_enter() elif(cmd == 'o'): val = seq_out() #出队的数据 if(val !=None): print("out val:%s"%val); else: print("seq is None") elif(cmd == 's'): seq_show() elif(cmd == 'q'): break else: pass
元组: >>> t2 = tuple("what") >>> t2 ('w', 'h', 'a', 't')
tup = ("zhang",[10,289]) >>> tup ('zhang', [10, 289]) >>> tup[1][0] = 90 #元组中的列表是可变的 >>> tup ('zhang', [90, 289]) #不让对方修改我的数据 #修改数据函数 def change_func(list1): print list1; if(isinstance(list1,list)): #传入是类型 是不是列表,如果是,则修改第一个元素 list1[0] = 10; #原数据 def main_func(): lista = [1,2,3] change_func(tuple(lista)) #将列表强制转为元组 print lista #执行方法 main_func();
>>> child = [("age","height"),[10,12]] >>> child [('age', 'height'), [10, 12]] >>> child1 = list(child) >>> child2 = list(child1) >>> id(child1) 44729168 >>> id(child2) 44728968 >>> id(child) 44389240 >>> child1[0] = ("age","height","weight") >>> child [('age', 'height'), [10, 12]] >>> child1[1].append(38) >>> child1 [('age', 'height', 'weight'), [10, 12, 38]] >>> child2 [('age', 'height'), [10, 12, 38]] child2 也改变了,这是不希望看到的, 原因: child1 和 child2 进行和深拷贝 child 可以看出:当改变值时,其实是新创建了一个元组,证明元组是不可变 而列表追加值时,是在原来的列表中添加,而不是创建新的列表,所以 child1 child2中的值不同的。 >>> id(child[0]) 44397152 >>> id(child1[0]) 44703336 >>> id(child2[0]) 44397152 >>> id(child[1]) 44703256 >>> id(child1[1]) 44703256 >>> id(child2[1]) 44703256
解决上面的问题 :进行深拷贝
>>> import copy >>> child = [("age,height"),[10,29]] #数据进行深拷贝,这样当child1修时,就不会影响child2中的数据 元组:内存地址相同 列表:内存地址不相同 ##深拷贝的list类型数据后的内存地址是不一样的,而浅拷贝的list数据类型内存地址是一样的 >>> child1 = copy.deepcopy(child) >>> child1 ['age,height', [10, 29]] >>> id(child) 43896880 >>> id(child1) 44389240 >>> id(child[0]) 44704384 >>> id(child1[0]) 44704384 >>> id(child1[1]) 44391720 >>> id(child[1]) 44391240
相关文章推荐
- numpy教程:逻辑函数Logic functions
- Numpy详解教程
- 在python中的使用Libsvm
- 在python中的使用Libsvm
- Python 正则表达式
- Python异常
- Python下载文件时出现乱码的解决方法之一:Content-Encoding: gzip
- Python urllib
- 在Notepad++里配置python环境
- python_基础03
- 理解Python中的with…as…语法
- python包装和授权
- 《Python 3面向对象编程》 购买原因——《Python 3面向对象编程》 读后感:弄懂何时使用面向对象...
- python挑战题(转+个人解答)
- 基于python的贝叶斯分类
- Python xml.etree.ElementTree 用法
- 网路基础(python)
- python 错误集
- python_基础02
- black hat python