简明python教程五----数据结构(下)
引用
当你创建一个对象并给它赋一个变量的时候,这个变量仅仅引用那个对象,而不是表示这个对象本身!即,变量名指向你计算机中存储那个对象的内存。
print 'Simple Assignment' shoplist = ['apple','mango','carrot','banana'] mylist = shoplist del shoplist[0] print'shoplist is',shoplist print 'mylist is',mylist print 'Copy by making a full slice' mylist = shoplist[:] del mylist[0] print 'shoplist is',shoplist print 'mylist is',mylist
结果:
Simple Assignment shoplist is ['mango', 'carrot', 'banana'] mylist is ['mango', 'carrot', 'banana'] Copy by making a full slice shoplist is ['mango', 'carrot', 'banana'] mylist is ['carrot', 'banana']
复制一个列表或者类似的序列或者其他复杂的对象(不是整数那样 的简单对象),那必须使用切片操作符来取得拷贝。
字符串
name='Swaroop' if name.startswith('Swar'): print 'Yes,the string starts with "Swar"' if 'a' in name: print 'Yes,it contains the string "a"' if name.find('war')!=-1: print 'Yes,it contains the string "war"' delimiter='_*_' mylist=['Brazil','Russia','India','China'] print delimiter.join(mylist)
结果:
Yes,the string starts with "Swar" Yes,it contains the string "a" Yes,it contains the string "war" Brazil_*_Russia_*_India_*_China
startswith():测试字符串是否以给定字符串开始,
in操作符:检验一个给定字符串是否为另一个字符串的一部分。
find:找出给定字符串在另一个字符串的位置,返回-1表示找不到字符串。