jialiguo 发表于 2018-8-7 09:45:53

Python(五)列表

  数组
  数组存储的是同一类型的一串信息
  列表
  一、列表的定义
  · 定义一个空列表
  list = []
  · 定义一个包含元素的列表,元素可以是任意类型,包括数值类型,列表,元组,字符串等等均可。
  赋值方式定义:
  list = ["fentiao", 4, 'gender']
  list1 = ['fentiao',(4,'male')]
  工厂函数定义:
  n = list("hello")
  In : n=list("hello")
  In : print n
  ['h', 'e', 'l', 'l', 'o']
  二、支持索引、切片、拼接、重复、成员操作符
  索引、切片:
  In : li=]
  In : li
  Out: 1
  In : li[-1]
  Out:
  In : li[:]
  Out: ]
  In : li
  Out: ]
  In : li
  Out:
  In : li[::-1]
  Out: [, (1+4j), 'hello', True, 1.0, 1]
  拼接:
  In : li1=['vsftpd','apache']
  In : li2=['mariadb','nfs']
  In : li1 + li2
  Out: ['vsftpd', 'apache', 'mariadb', 'nfs']
  重复:
  In : li1=['vsftpd','apache']
  In : li1*2
  Out: ['vsftpd', 'apache', 'vsftpd', 'apache']
  成员操作符:
  In : li1=['vsftpd','apache']
  In : 'vsftpd' in li1
  Out: True
  In : 'vsftpd' not in li1
  Out: False
  题目1:
  查看1-10号主机的21,22,3306,80,69端口
  解答:
  #!/usr/bin/env python
  # coding:utf-8
  ports =
  for i in range(1,11):
  for port in ports:   #可以通过 for i in list进行遍历列表中的各个元素
  ip = '172.25.254.'+str(i)
  print "[+] Listening On:%s:%d" %(ip,port)
  @font-face {   font-family: "Times New Roman"; }@font-face {   font-family: "宋体"; }p.MsoNormal { margin: 0pt 0pt 0.0001pt; text-align: justify; font-family: 'Times New Roman'; font-size: 10.5pt; }p.p { margin: 5pt 0pt; text-align: left; font-family: 'Times New Roman'; font-size: 12pt; }span.msoIns { text-decoration: underline; color: blue; }span.msoDel { text-decoration: line-through; color: red; }div.Section0 { page: Section0; }
  三、列表的常用方法
  1.更新列表
  · append(增加一个元素)
  · extend(可以增加多个元素,可以在括号中给出一个列表,这个列表中的元素会倒入到原列表,成为他的元素)


  #可以看到同样增加一个列表,append把它当成一个元素增加进去,而extend把它当作两个元素加了进去,达到了一次性增加多个元素的目的
  如果增加一个字符,使用append表示增加这个字符串,而extend表示这个字符串的每个字母作为一个元素增加进去:
  In : li1
  Out: ['vsftpd', 'apache']
  In : li1.append('hello')
  In : li1
  Out: ['vsftpd', 'apache', 'hello']
  In : li1.extend('hello')
  In : li1
  Out: ['vsftpd', 'apache', 'hello', 'h', 'e', 'l', 'l', 'o']
  · 在指定位置添加元素使用inert方法;
  l.insert(index, object)

  · 修改列表的元素:直接重新赋值;

  2.查看列表
  · 查看某个列表元素的下表用index方法;
  · 查看某个列表元素出现的次数用count方法;

  3.删除列表
  remove

  pop
  li.pop()表示删除最后一个元素
  li.pop(0)表示删除第一个元素
  del
  #直接删除这个列表

  题目2:
  1. 用户名和密码分别保存在列表中;
  2. 用户登录时,判断该用户是否注册;
  2. 用户登录时,为防止***暴力破解, 仅有三次机会;
  3. 如果登录成功,显示登录成功(exit(), break).
  解答:
  #!/usr/bin/env python
  #coding: utf-8
  users = ["user1", "user2", "user3"]
  passwords = ["123", "456", "789"]
  i= 0
  while i < 3:
  name = raw_input(&quot;请输入用户名:&quot;)
  if name not in users:
  print &quot;用户未注册&quot;
  break
  password = raw_input(&quot;请输入密码:&quot;)
  i += 1
  index = users.index(name)
  if password == passwords:
  print &quot;恭喜登录成功&quot;
  break
  else:
  print &quot;请输入正确的用户名或密码!&quot;
  else:
  print &quot;已登录三次,请稍后再试&quot;
  题目3:
  打印栈的过程
  解答:
  #!/usr/bin/env python
  #coding:utf-8
  &quot;&quot;&quot;
  列表有容器和可变的特性,通过列表构建其他数据类型;
  1. 栈(eg: 往箱子里面放书)
  栈的工作方式后进先出(LIFO);
  2. 队列(eg:饭堂排队买饭)
  队列的工作方式先进先出(FIFO)
  &quot;&quot;&quot;
  stack = []
  info =&quot;&quot;&quot;
  栈操作
  1. 出栈
  2. 入栈
  3. 查看栈元素
  4. 退出
  &quot;&quot;&quot;
  print info
  while 1:
  choice = raw_input(&quot;Choice:&quot;).strip()
  if choice == '1':
  if not stack == []:
  stack.pop()
  else:
  print &quot;栈为空&quot;
  elif choice == '2':
  value = raw_input(&quot;请输入入栈的值:&quot;).strip()
  stack.append(value)
  elif choice == '3':
  print &quot;栈元素:&quot;,
  for i in stack:
  print i,
  print
  elif choice == '4':
  exit()
  else:
  print &quot;not valid choice&quot;
页: [1]
查看完整版本: Python(五)列表