首页 > 编程语言 >python12

python12

时间:2023-10-17 22:37:06浏览次数:42  
标签:Alex python12 list 列表 user print data

1.列表(list)

列表是一个有序且可变的容器,在里面可存放多个类型的元素。

1.1定义

use_list = ["天","地","人"]
number_list = [98,66,55]
data_list = [1,True,"Alex"]
use_list = []
use_list.append("铁锤")
use_list.append(123)
use_list.append(True)
print(use_list)#["铁锤",123,True]

不可变类型:字符串,布尔,整形(内部数据无法进行修改)

可变类型:列表(内部数据可以进行修改)

1.2独有功能

  • 字符串,不可变,即:创建好之后内部就无法修改。[独有功能都是新创建一份数据]
name = "alex"
data = name.upper()
print(name)
prinat(data)
  • 列表,可变,即:创建好之后内部元素可以修改。[独有功能基本上都是直接操作列表内部,不会创建新一份数据]
user_list = ["天","地"]
user_list.append("人")
print(user_list)

列表中的常见独有功能如下:

1.追加,在原列表中尾部追加值。

data_list = []   
v1 = input("请输入名字")
data_list.append(v1)
v2 = input("请输入名字")
data_list.append(v2)

#案例2
welcome = "欢迎使用修仙世界".center(30,'*')
print(welcome)
user_count = 0
while True:
	count = input("请输入游戏人数:")
	if count.isdecimal():
		user_count = int(count)
		break
	else:
		print("输入格式错误,人数必须是数字。")
message = "欢迎{}人加入修仙世界".format(user_count)
print(message)
user_name_list = []
for i in range(1,user_count+1):
	tips = "请输入玩家名字({}/{}):".format(i,user_count)
	name = input(tips)
	user_name_list.append(name)
print(user_name_list)

2.批量追加,将一个列表中的元素逐一添加另一个列表

tools = ["搬砖","菜刀","斧头"]
tools.extend([11,22,33])
print(tools)#['搬砖', '菜刀', '斧头', 11, 22, 33]
tools = ["搬砖","菜刀","斧头"]
weapon = [11,22,33]
tools.extend(weapon)
print(tools)#['搬砖', '菜刀', '斧头', 11, 22, 33]
#等价于
tools = ["搬砖","菜刀","斧头"]
weapon = [11,22,33]
for item in weapon:
	tools.append(item)
print(tools)#['搬砖', '菜刀', '斧头', 11, 22, 33]

3.插入,在原列表的指定索引位置插入值

user_list = ["搬砖","菜刀","斧头"]
user_list.insert(0,"外卖")
user_list.insert(2,"苹果")
print(user_list)#['外卖', '搬砖', '苹果', '菜刀', '斧头']

4.在原列表中根据值删除(从左到右找到第一个删除)

user_list = ["搬砖","菜刀","斧头","Alex"]
while True:
	if "Alex" in user_list:
		user_list.remove("Alex")
	else:
		break
print(user_list)
#案例:自动抽奖程序
import random
data_list = ["iphone12","mate60","1000元","暖水壶"]
while data_list:
	name = input("自动抽奖程序,请输入自己的名字:")
	value = random.choice(data_list)
	print("恭喜{},抽中{}".format(name,value))
	data_list.remove(value)

5.在原列表中根据索引踢出某个元素(根据索引位置删除)

user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
#               0       1       2      3       4
user_list.pop(1)
print(user_list)#['王宝强', 'Alex', '贾乃亮', 'Alex']
user_list.pop()
print(user_list)#['王宝强', '陈羽凡', 'Alex', '贾乃亮']
item = user_list.pop(1)
ele = user_list.pop()#在user-list删除最后一个,并讲删除值赋给ele
item = user_list.pop(2)#在user_list中删除索引为2的值,并将删除值赋给item
#案例:排队买火车票
user_queue = []
while True:
	name = input("北京—上海火车票,购买请输入名字排队(Q退出):")
	if name == "Q":
		break
	user_queue.append(name)
ticket_count = 3
for i in range(ticket_count):
	username = user_queue.pop(0)
	message = "恭喜{},购买火车票成功。".format(username)
	print(message)
faild_user = ",".join(user_queue)
faild_message = "非常抱歉,票已经售完了,以下用户请选择其他的出行方式,名单:{}".format(faild_user)
print(faild_message)

6.清空原列表

user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
user_list.clear()
print(user_list)#[]

7.根据值获取索引(从左到右找到第一个删除)[慎用,找不到报错]

user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
if "Alex" in user_list:
	index = user_list.index("Alex")
	print(index)#2
else:
	print("不存在")

8.列表元素排序

#数字排序
num_list = [11,22,4,5,11,99,88]
print(num_list)
num_list.sort()#让num_list从小到大排序
num_list.sort(reverse=True)#让num_list从大到小排序
print(num_list)
#字符串排序
user_list = ["王宝强","Ab陈羽凡","Alex","贾乃亮","贾乃","1"]
#[29579, 23453, 24378]
 [65, 98, 38472, 32701, 20961]
 [65, 108, 101, 120] 
 [49]
print(user_list)
user_list.sort()
print(user_list)
data = "Ab陈羽凡"
data_list = []
for char in data:
	v1 = ord(char)
	data_list.append(v1)
print(data)
print(data_list)

注意:排序时内部元素无法进行比较时,程序会报错(尽量数据类型统一)。

9.反转原列表

user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
user_list.reverse()
print(user_list)

1.3公共功能

1.相加,两个列表相加获取生成一个新的列表。

data = ["赵四","刘能"] + ["宋晓锋","范德彪"]
print(data)
v1 = ["赵四","刘能"]
v2 = ["宋晓锋","范德彪"]
v3 = v1 + v2
print(v3)

2.相乘,列表*整形将列表中的元素在创建N份并生成一个新的列表。

data = ["赵四","刘能"] * 2
print(data)
v1 = ["赵四","刘能"]
v2 = v1 *2
print(v2)

3.运算符in包含

由于列表内部是由多个元素组成,可以通过in来判断元素是否在列表中。

user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
result = "alex" in user_list
result = "alex" not in user_list
print(result)

注意:列表检查元素是否存在时,效率会很低。

4.获取长度

user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
print(len(user_list))

5.索引

#读
user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
print(user_list[0])
print(user_list[1])
print(user_list[2])
#改
user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
print(user_list[0]) = "吴佩琦"
print(user_list)
#删
user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
del user_list[1]

注意:超出索引范围会报错。

提示:由于字符串是不可变类型所以他只有索引读的功能,而列表可以进行读,改,删

6.切片

#读
user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
print(user_list[0:2])
print(user_list[1:])
print(user_list[:-1])
#改
user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
print(user_list[0:2]) = [11,22,33,44]
print(user_list)
user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
print(user_list[1000:]) = [11,22,33,44]
print(user_list)
#删
user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
del user_list[1:]
print(user_list)

7.步长

user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
#               1       2       3       4       5
print(user_list[1:4:2])
#案例:实现列表翻转
user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
new_data = user_list[::-1]
print(new_data)
user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
user_list.reverse()
print(data_list)

8.for循环

user_list = ["王宝强","陈羽凡","Alex","贾乃亮","Alex"]
for item in user_list:
	print(item)
user_list = ["王宝强","陈羽凡","刘Alex","刘贾乃亮","Alex"]
for index in range(len(user_list))-1,-1,-1):
	item = user_list(index)
	if item.startswith("刘"):
		user_list.remove(item)
print(user_list)

1.4转换

  • int,bool无法转换成列表
  • str
name = "吴佩琦"
data = list(name)
print(data)
  • 超前
v1 = (11,22,33,44)#元组
vv1 = list(v1)#列表[11,22,33,44]
v2 = {"alex","eric","dsb"}#集合
vv2 = list(v2)#列表['dsb', 'eric', 'alex']

1.5.1嵌套

列表属于容器,内部可以存放各种数据,所以他也支持列表的嵌套,如:

data = ["谢广坤",["海燕","赵本山"],True,[11,22,33,44],"宋小宝"]

对于嵌套的值,可以根据之前学习的索引知识点来进行学习,例如:

data = ["谢广坤",["海燕","赵本山"],True,[11,22,33,44],"宋小宝"]
print(data[0])#谢广坤
print(data[1])#['海燕', '赵本山']
print(data[0][2])#坤
print(data[1][-1])#赵本山
data.append(666)
print(data)['谢广坤', ['海燕', '赵本山'], True, [11, 22, 33, 44], '宋小宝', 666]
data[1].append("谢大脚")
print(data)['谢广坤', ['海燕', '赵本山', '谢大脚'], True, [11, 22, 33, 44], '宋小宝', 666]
del data[-2]
print(data)#['谢广坤', ['海燕', '赵本山', '谢大脚'], True, [11, 22, 33, 44], 666]
user_list = []
while True:
	user = input("请输入用户名(Q退出):")
	if user == "Q"
		break
	pwd = input("请输入密码:")
	data = [user,pwd]
	user_list.append(data)
print(user_list)

2.元组

列表(list),是一个有序且可变的容器,在里面可以存放多个不同类型的元素。

元组(tuple),是一个有序且不可变的容器,在里面可以存放多个不同类型的元素。

2.1定义

v1 = (11,22,33)
v2 = ("李杰","Alex")
v3 = (True,123,"Alex",[11,22,33])
#建议在元组的最后多加一个逗号,用于标识他是一个元组
v2 = ("李杰","Alex",)

2.3公共功能

1.相加,两个列表相加获取生成一个新的列表

data = ("赵四","刘能") + ("宋晓锋","范德彪")
print(data)
v1 = ("赵四","刘能")
v2 = ("宋晓锋","范德彪")
v3 = v1 + v2
print(v3)

2.相乘,列表*整形将列表中的元素再创建N份并生成一个新列表。

data = ("赵四","刘能") * 2
print(data)
v1 = ("赵四","刘能")
v2 = v1 * 2
print(v1)
print(v2)

3.获取长度

user_list = ("赵四","刘能")
print(len(user_list))

4.索引

user_list = ("赵四","刘能")
print(user_list[0])
print(user_list[1])
print(user_list[2])

5.切片

user_list = ("赵四","刘能",)
print(user_list[0:2])
print(user_list[1:])
print(user_list[:-1])

6.步长

user_list = ("赵四","刘能",)
print(user_list[1:4:2])
print(user_list[0::2])
print(user_list[1::2])
print(user_list[4:1:-1])
#字符串&元组
user_list = ("赵四","刘能")
data = user_list[::-1]
#列表
user_list = ("赵四","刘能")
data = user_list[::-1]
user_list.reverse()
print(user_list)

7.for循环

user_list = ("赵四","刘能")
for item in user_list:
	print(item)
user_list = ("赵四","刘能")
for item in user_list:
	if item == "刘能":
		continue
	print(name)

2.4转换

其他类型转换为元组,使用tuple(其他类型),目前只有字符串和列表可以转换为元组。

data = tuple(其他)
#str/list
name = "武沛齐"
data = tuple(name)
print(data)#('武', '沛', '齐')
name = ("武沛齐",18,"pythonav")
data = tuple(name)
print(data)#('武沛齐', 18, 'pythonav')

2.5嵌套

由于元组和列表都可以充当容器,他们内部可以放很多元素,并且也支持元素内的各种嵌套。

tu = ('赵四', '刘能', '宋晓锋',("武沛齐",18,"pythonav") '范德彪')
tu1 = tu[0]
tu2 = tu[1]
tu3 = tu[2][0]
tu4 = tu[2][1]

标签:Alex,python12,list,列表,user,print,data
From: https://www.cnblogs.com/zhangyj213/p/17770859.html

相关文章

  • python123判断闰年函数
    判断闰年的原理和问题在格里高利历中,闰年是指能被4整除但不能被100整除的年份,或者能被400整除的年份。判断一个年份是否为闰年是一个常见的问题,本文将介绍如何使用Python编写一个判断闰年的函数。在编写判断闰年函数之前,我们需要了解一个问题,即公元纪年法在历史上的改革。人们最......
  • python12:函数参数引用传值
    概念:在python中参数的赋值是一种引用关系。例如:a=1 a=2这里a中存储的是1存储的实际地址。其后将2赋予a实际上是修改了a中引用的地址,二将2赋予了新地址的空间里。这里并不是同一片空间的覆盖关系。而是属于不同空间不同的地址,只是通过a这样同一个标签来进行引用。注意:1.在pyt......
  • Python12类内置函数大汇总
    内置函数就是python给你提供的,拿来直接用的函数,比如print.,input等。截止到python版本3.6.2python一共提供了68个内置函数,我将它们分成12类,方便你学习。#68个内置函数abs()dict()help()min()setattr()all()dir()hex()next()slice()any()......
  • python12
    Python 基础语法Python语言与Perl,C和Java等语言有许多相似之处。但是,也存在一些差异。在本章中我们将来学习Python的基础语法,让你快速学会Python编程。第一......
  • Python12-eg
    实例01(使用wx.StaticText输出Python之禅)1#-*-coding:utf-8-*-2importwx3classMyFrame(wx.Frame):4def__init__(self,parent,id):5......
  • python123_第七周练习_程序设计题
    羽毛球计分规则:1.21分制,3局2胜为佳2.每球得分制3.每回合中,取胜的一方加1分4.当双方均为20分时,领先对方2分的一方赢得该局比赛5.当双方均为29分时,先取得30......