python 列表

列表

#1.定义列表

• 定义一个空列表

list = [ ]


• 定义一个包含元素的列表,元素可以是任意类型,包括数值类型,列表,元组,字符串等均可。
list = [1,'hello',('you','me'),[ ]]

#2.列表特性

(1).索引

# 正向索引
list[0]
# 反向索引
list[-1]
# 拿出列表第三个元素,第三个元素是列表, 再拿出列表的第二个元素
list[2][1]


(2).切片

list[start:stop:step]     #与字符串,元组的切片类似


(3).重复,连接

(4).成员操作符


#3.列表的增查改删

(1).增

# list.append 追加元素到列表的最后


# list.extend 增加多个元素到列表最后


# list.insert 增加元素到列表的指定位置


(2).查

# list.count 统计某个元素在列表中出现的次数


# list.index 找到某个值在列表中的索引值


(3).改

# 通过列表的索引,对列表某个索引值重新赋值


(4).删

# list.remove 删除列表中遇到的第一个 value 值


# del 删除列表中第 i 个索引值


# del 删除列表中第 i 个索引值后的所有值


# 删除列表对象


# list.pop 默认删除列表最后一项并返回其内容,可指定索引值


4.排序

# list.sort( ) 数字的话按照大小进行排序,字母的话按照ASCII值进行排序


# ord( ) 查找一个字符的ASCII值

# chr( ) 查找一个ASCII值对应的字符


#5.练习

(1).用户登录程序版本2:
        用户名和密码分别保存在列表中;
        用户登录时,判断该用户是否注册;
        用户登录时,为防止黑客暴力破解, 仅有三次机会;
        如果登录成功,显示登录成功(exit(), break).
知识点学习:
        python中特有的while....else...语句
        如果满足while后面的语句,执行while循环的程序, 如果不满足,执行else里面的程序.
提示: 用户名和密码一一对应

程序:

#!/usr/bin/env python
#coding:utf-8

users = ['root','student','westos']
passwords = ['redhat','student','westos']
trycount = 0
while trycount < 3:
	U = raw_input('username:')
	if not (U in users):
		print '该用户未注册'
		break
	index = users.index(U)
	P = raw_input('password:')
	if P == passwords[index]:
		print '登陆成功'
		break
	else:
		print '密码错误'
		trycount +=1
		
else:
	print '超过三次输入密码错误'

测试:


(2).列表构建栈和队列数据结构

#1).栈
        栈是先进后出(LIFO-first in last out);
        类似于往箱子里面放书;
        代码实现如下: (实际应用中这样太麻烦,将来会用类实现)

程序:

#!/usr/bin/env python
#coding:utf-8
stack = []
info = """
			栈操作
	1). 入栈
	2). 出栈
	3). 栈长度
	4). 查看栈
	5). 退出
请输入你的选择:"""
# 死循环
while True:
	choice = raw_input(info).strip()
	if choice == "1":
		print "入栈操作".center(40, "*")
		value = raw_input("请输入入栈元素:")
		stack.append(value)
		print "元素%s入栈成功..." %(value)
	elif choice == "2":
		print "出栈操作".center(40, "*")
		# if len(stack) == 0:
		if not stack:
			print "栈为空"
		else:
			item = stack.pop()
			print "元素%s出栈成功...." %(item)
	elif choice == "3":
		print "查看栈长度".center(40, "*")
		print len(stack)
	elif choice == "4":
		print "查看栈元素".center(40, "*")
		if not stack:
			print "栈为空"
		for i in stack:
			print i,
	elif choice == "5":
		exit()
	else:
		print "请输入正确的选择......"

测试:


#2).队列

        队列是先进先出(FIFO):
        类似于去餐厅买饭排队;

程序:

#!/usr/bin/env python
#coding:utf-8

queue = []
menu = """
		队列操作
 1). 入队
 2). 出队
 3). 队长度
 4). 查看队元素
 5). 退出
请输入你的选择:"""
while True:
	print menu,
	choice = raw_input().strip()
	if choice == "1":
		value = raw_input("请输入入队元素:")
		queue.append(value)
	elif choice == "2":
		if not queue:
			print "队为空"
		else:
			del queue[0]
	elif choice == "3":
		print len(queue)
	elif choice == "4":
		for i in queue:
			print i
	elif choice == "5":
		exit()
	else:
		print "请输入正确的序号..."

测试:




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值