你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

DAY 5

2021/12/29 19:12:30

day 5

列表

增加、修改、删除、统计、排序
name_list = ["zhangsan", "lisi", "wangwu"]
# 1. 取值和取索引
print(name_list[0])
# name_list[3]  list index out of range

# 知道数据的内容,想确定数据在列表中的内容
print(name_list.index("lisi"))
# 若传递的数据不在列表中,则会报错
# 2.修改
name_list[1] = "李四"
# 列表指定的索引超出范围,程序会报错 list assignment index out of range
# 3.增加
# append 可以向列表的末尾追加数据
name_list.append("王小二")
# insert 可以在列表的指定索引位置插入数据
name_list.insert(1, "美女")
# extend 可以把其他列表中的完整内容,追加到当前列表的末尾
temp_list = ["sunwukong", "zhuerge", "shashidi"]
name_list.extend(temp_list)
# 4.删除
# remove 方法可以从列表中删除指定的数据
name_list.remove("wangwu")
# pop 方法默认可以把列表中最后一个元素删掉
name_list.pop()
# pop 方法可以指定要删除元素的索引
name_list.pop(3)
name_list.clear()
print(name_list)
name_list = ["张三", "李四", "王五"]
# 使用del关键字删除列表元素
del name_list[1]
# del 本质上是用来将一个变量从内存中删除的
name = "xiaoming"
del name  # 后续的代码不能再使用这个变量
print(name_list)
name_list = ["张三", "李四", "王五", "张三"]
# len函数可以统计列表中元素的总数
list_len = len(name_list)
print(list_len)
# count 方法可以统计列表中某一个数据出现的次数
count = name_list.count("张三")
print(count)
# remove删除列表中第一次出现的数据
name_list.remove("张三")
print(name_list)
name_list = ["zhangsan", "lisi", "wangwu", "wangxiaoer"]
num_list = [6, 8, 4, 1, 10]
# 升序 ctrl+q 查看函数
# name_list.sort()
# num_list.sort()
# 降序 ctrl+/ 快速多行注释
# name_list.sort(reverse=True)
# num_list.sort(reverse=True)
# 逆序(反序)
name_list.reverse()
num_list.reverse()
print(name_list)
print(num_list)
关键字

例如 True 后面不需要使用括号

函数

封装了独立功能,可以直接调用

函数名(参数)

方法

对象.方法(参数)

遍历循环

name_list = ["zhangsan", "lisi", "wangwu", "wangxiaoer"]

for my_name in name_list:
     print(my_name)

元组

元素不能修改,可以保存不同类型的数据

tuple=()

info_tuple = ("zhangsan", 18, 1.75,"zhangsan")
# 1.取值和取索引
print(info_tuple[0])
print(info_tuple.index("zhangsan"))
# 2.统计计数
print(info_tuple.count("zhangsan"))
print(len(info_tuple))
info_tuple = ("zhangsan", 18, 1.75)
print("%s年龄是%d 身高是%2.f" % info_tuple)
info_str = "%s年龄是%d 身高是%2.f" % info_tuple
print(info_str)
list(info_tuple)  # 可以将元组转化为列表

字典

列表是有序的对象集合

字典是无序的对象集合,使用键值对存储数据

xiaoming = {"name": "小明",
            "age": 18,
            "gender": True,
            "height": 1.75,
            "weight": 75.5}
# 1.取值
print(xiaoming["name"])
# 2.增加/修改
xiaoming["shoes"] = 18
xiaoming["name"] = "xiaoming"
# 3.删除
xiaoming.pop("name")
print(xiaoming)
xiaoming_dict = {"name": "小明",
                 "age": 18}
# 1.统计键值对数量
print(len(xiaoming_dict))
# 2.合并字典
temp_dict = {"height": 1.75,
             "age": 20}

xiaoming_dict.update(temp_dict)
# 3.清空字典
xiaoming_dict.clear()
print(xiaoming_dict)
xiaoming_dict = {"name": "小明",
                 "qq": "1234",
                 "phone": "10086"}
# 迭代遍历字典
# 变量k是每一次循环中,获取到的键值对的key
for k in xiaoming_dict:
    print("%s - %s" % (k, xiaoming_dict[k]))
card_list = [
    {"name": "zhangsan",
     "qq": "123",
     "phone": "110"},
    {"name": "lisi",
     "qq": "245",
     "phone": "220"}
]
for card_info in card_list:
    print(card_info)
str1 = "hello python"
str2 = '我的外号是"大西瓜"'
print(str2)
print(str1[6])
for chars in str1:
    print(chars)
hello_str = "hello hello"
# 1.统计字符串长度
print(len(hello_str))
# 2.统计某一个小字符串出现的次数
print(hello_str.count("llo"))
# 3.某一个子字符串出现的位置
print(hello_str.index("llo"))

判断空白字符、判断数字

# 1.判断空白字符
space_str = " \t\n"
print(space_str.isspace())
# 2.判断字符串中是否只包含数字
# 1>都不能判断小数
#num_str = "1.1"
# 2>unicode 字符串
# num_str="\u00b2"
# 3>中文数字
num_str = "一千零一"

print(num_str)
print(num_str.isdecimal())
print(num_str.isdigit())
print(num_str.isnumeric())

字符串的查找和替换

hello_str = "hello world"
# 1.判断是否以指定字符串开始
print(hello_str.startswith("hello"))
# 2.判断是否以指定字符串结束
print(hello_str.endswith("ld"))
# 3.查找指定字符串
# index同样可以查找指定的字符串在大字符串中的索引
# index如果指定的字符串不存在,会报错
# find如果指定的字符串不存在,会返回-1
print(hello_str.find("llo"))
# 4.替换字符串
# replace执行完成之后,会返回一个新的字符串     注意:不会修改原有字符串的内容
print(hello_str.replace("world", "python"))
print(hello_str)

文本对齐和去除空白字符

poem = ["\t\n登鹤雀楼",
        "王之涣",
        "白日依山尽\n",
        "黄河入海流",
        "欲穷千里目",
        "更上一层楼"]
for i in poem:
        # 先使用strip方法去除字符串中的文本
        print("|%s|" % i.strip().rjust(10, " "))

拆分和连接

poem_str = "间接\t 确定 \t\n djklajl \t\n"
print(poem_str)

# 1.拆分字符串
poem_list = poem_str.split()
print(poem_list)

# 2.合并字符串
result = " ".join(poem_list)
print(result)

切片

字符串[开始索引:结束索引:步长]

公共方法

函数
切片
算术运算符