一、列表
1、定义
>>> shoplist=['apple','mango','carrot','banana'] >>> print shoplist ['apple', 'mango', 'carrot', 'banana'] # 取出指定元素 >>> print shoplist[0] apple >>> print shoplist[3] banana >>> print shoplist[2:] ['carrot', 'banana']
2、列表函数
>>> shoplist. shoplist.__add__( shoplist.__getslice__( shoplist.__new__( shoplist.append( shoplist.__class__( shoplist.__gt__( shoplist.__reduce__( shoplist.count( shoplist.__contains__( shoplist.__hash__ shoplist.__reduce_ex__( shoplist.extend( shoplist.__delattr__( shoplist.__iadd__( shoplist.__repr__( shoplist.index( shoplist.__delitem__( shoplist.__imul__( shoplist.__reversed__( shoplist.insert( shoplist.__delslice__( shoplist.__init__( shoplist.__rmul__( shoplist.pop( shoplist.__doc__ shoplist.__iter__( shoplist.__setattr__( shoplist.remove( shoplist.__eq__( shoplist.__le__( shoplist.__setitem__( shoplist.reverse( shoplist.__format__( shoplist.__len__( shoplist.__setslice__( shoplist.sort( shoplist.__ge__( shoplist.__lt__( shoplist.__sizeof__( shoplist.__getattribute__( shoplist.__mul__( shoplist.__str__( shoplist.__getitem__( shoplist.__ne__( shoplist.__subclasshook__( # 增加元素append >>> shoplist.append('tea') >>> shoplist ['apple', 'mango', 'carrot', 'banana', 'tea'] # append增加元素时不会拆分列表 >>> shoplist.append([111,222]) >>> shoplist ['apple', 'mango', 'carrot', 'banana', 'tea', 'aa', 'bb', 'cc', [111, 222]] # 增加一个列表extend,extend会自动拆分列表 >>> shoplist.extend(['aa','bb','cc']) >>> shoplist ['apple', 'mango', 'carrot', 'banana', 'tea', 'aa', 'bb', 'cc'] # 取出列表里列表的元素 >>> shoplist[-1] [111, 222] >>> shoplist[-1][1] 222 >>> shoplist[8][1] 222 # 删除指定元素 >>> shoplist.remove('cc') >>> shoplist ['apple', 'mango', 'carrot', 'banana', 'tea', 'aa', 'bb', [111, 222]] # 删除多个元素del,指定索引删除 >>> del shoplist[5:6] >>> shoplist ['apple', 'mango', 'carrot', 'banana', 'tea', 'bb', [111, 222]] >>> del shoplist[5:7] >>> shoplist ['apple', 'mango', 'carrot', 'banana', 'tea'] >>> del shoplist[-1] >>> shoplist ['apple', 'mango', 'carrot', 'banana'] # for打印列表 >>> for i in shoplist: ... print i ... apple mango carrot banana
3、打印列表脚本示例
[root@saltstack python]# cat shop1.py #!/usr/bin/env python shoplist = ['apple', 'mango', 'carrot', 'banana'] print 'I have', len(shoplist), 'items to purchase.' print 'These items are: ' # Notice the comma at end of the line for item in shoplist: print '%s' % item print '-------------------------------------------------------' print 'I also have to buy rice.' shoplist.append('rice') print 'My shopping list is Now: %s' % shoplist print '-------------------------------------------------------' print 'And I will Delete by is %s.' % shoplist[0] del shoplist[0] print 'Good! My shopping list is Now: %s' % shoplist print '-------------------------------------------------------' # 执行结果 [root@saltstack python]# python shop1.py I have 4 items to purchase. These items are: apple mango carrot banana ------------------------------------------------------- I also have to buy rice. My shopping list is Now: ['apple', 'mango', 'carrot', 'banana', 'rice'] ------------------------------------------------------- And I will Delete by is apple. Good! My shopping list is Now: ['mango', 'carrot', 'banana', 'rice'] -------------------------------------------------------
二、元组
[root@saltstack python]# cat tuple.py #!/usr/bin/python zoo = ('wolf','elephant','penguin') print 'Number of animals in the zoo is', len(zoo) new_zoo = ('monkey','dolphin',zoo) print 'All animals in new zoo are', new_zoo print 'Animals brought fron old zoo are', new_zoo[2] print 'Last animal brought from old zoo is %s' % new_zoo[2][-1] [root@saltstack python]# python tuple.py Number of animals in the zoo is 3 All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin')) Animals brought fron old zoo are ('wolf', 'elephant', 'penguin') Last animal brought from old zoo is penguin
四、字典
语法:d={key1: value, key2: value, ...} |
>>> info={'name':'hh', 'mail':'11.qq.com', 'age':24} >>> info {'mail': '11.qq.com', 'age': 24, 'name': 'hh'} >>> info.items() [('mail', '11.qq.com'), ('age', 24), ('name', 'hh')] >>> for i in info.items(): ... print i ... ('mail', '11.qq.com') ('age', 24) ('name', 'hh') # 读取key值 >>> info['name'] 'hh' >>> info['mail'] '11.qq.com' # 增加字典元素 >>> info['phone']=1231212 >>> info.items() [('mail', '11.qq.com'), ('age', 24), ('name', 'hh'), ('phone', 1231212)] # for遍历字典元素 >>> for k,v in info.items(): ... print k,v ... mail 11.qq.com age 24 name hh phone 1231212
字典案例:
#!/usr/bin/env python import sys dict={ 'hh': '111111', 'mm': '222222', 'yy': '333333', 'bb': '444444' } while True: user=raw_input('Please enter Login in System username: ') pwd= raw_input('Please enter Login password: ') print if user == 'hh' and pwd == '123': print 'Welcome %s to use the find system! ' % user while True: name = raw_input('Please input your find name: ') if name in dict: num=dict[name] print 'The name %s in the find system. and number is %s.' % (name,num) else: if name == 'exit' or name == 'quit': print 'exit!' sys.exit() print 'No find the user!' # 执行过程: [root@saltstack python]# python dict.py Please enter Login in System username: hh Please enter Login password: 123 Welcome hh to use the find system! Please input your find name: yy The name yy in the find system. and number is 333333. Please input your find name: quit exit! # 错误输入处理: [root@saltstack python]# python dict.py Please enter Login in System username: qq Please enter Login password: ss Please enter Login in System username: hh Please enter Login password: 123 Welcome hh to use the find system! Please input your find name: rr No find the user! Please input your find name: bb The name bb in the find system. and number is 444444. Please input your find name: exit exit!