python正则表达式、列表

正则表达式:

正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。

正则表达式的处理函数:

re match函数:尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。

re.search函数:扫描整个字符串并返回第一个成功的匹配。

re.findall函数:匹配字符串中所有符合的表达式

正则表达式中的符号定义:

. : 匹配任意字符,除了换行符

re*:匹配0个或多个表达式

re+:匹配1个或多个表达式

^:匹配字符串的开头

$ :   匹配字符串的结尾

re{n}:匹配n个前面的表达式。例如,"o{2}"不能匹配"Bob"中的"o",但是能匹配"food"中的两个o。

re{n,} :精确匹配n个前面表达式。例如,"o{2,}"不能匹配"Bob"中的"o",但能匹配"foooood"中的所有o。"o{1,}"等价于"o+"。"o{0,}"则等价于"o*"。

a|b:匹配a或b

\w:匹配数字字母下划线

\W:匹配非数字字母下划线

\s:匹配任意空白字符,等价于 [\t\n\r\f]。

\S:匹配非空字符

\d:匹配任意数字[0-9]

列表

 

列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现。列表的数据项不需要具有相同的类型。

创建列表使用“[ ]”列如:list = [ ]

访问列表中的元素,用该元素对应的索引来获取

向列表末尾添加数据用append():list.append()

向列表中某个位置添加数据用insert():list.insert(索引,数据)

修改列表中的数据:list[索引]=修改的数据

删除列表数据:del列表名[要删除元素的索引]

                        list.pop():删除列表中最后一个元素,也可以在括号中写要删除的元素的索引

                        list.remove("元素"):删除列表中第一个该元素

某元素在列表中出现的次数:list.count("元素")

del 列表名[要删除的数据的下标],列表名.pop()删除列表最后一个数据,括号里也可以指定要删除的数据的下标,列表名.remove(“数据”)删除括号里的内容,只删除一次。

冒泡排序

 

 list1 = [8, 1, 56, 23, 67, 12, 24, 45]
# 循环比较的次数
for i in range(len(list1) - 1):
    # 循环列表索引,
    for j in range(len(list1) - 1 - i):
        if list1[j] > list1[j + 1]:
            list1[j], list1[j + 1] = list1[j + 1], list1[j]

通讯录管理系统:

 

 dict1 = {}
while True:
    print("=" * 20, "通讯录管理系统", "=" * 20)
    print("1.增加姓名和手机")
    print("2.删除姓名")
    print("3.修改手机")
    print("4.查询所有用户")
    print("5.根据姓名查找手机号")
    print("6.退出")
    print("=" * 50)
    num = input("请输入您需要的操作:")
    if num in ["1", "2", "3", "4", "5"]:
        if num == "1":
            print("增加姓名和手机")
            print("-" * 20)
            name = input("请输入姓名:")
            phone = input("请输入手机号码:")
            if phone.isdigit():
                dict1[name] = phone
            else:
                print("您输入的格式不正确,请重新输入")
            print(dict1)
        elif num == "2":
            print("请删除姓名")
            print("-" * 30)
            name1 = input("请输入你要删除的姓名:")
            if name1 in dict1:
                del dict1[name1]
                print("删除成功")
                print("-" * 30)
                print(dict1)
            else:
                print("-" * 30)
                print("没有此人")
        elif num == "3":
            print("修改手机")
            print("-" * 30)
            name2 = input("请输入您要修改手机的姓名:")
            phone1 = input("请输入您要修改的号码:")
            if phone1.isdigit():
                if name2 in dict1:
                    dict1[name2] = phone1
                    print("修改成功")
                else:
                    print("抱歉,没有此人信息")
            else:
                print("您输入的格式不正确,请重新输入")
            print(dict1)
        elif num == "4":
            print("查询所有用户")
            print("-" * 30)
            for key in dict1:
                print(key, dict1[key])
        elif num == "5":
            print("根据姓名查找手机号")
            print("-" * 30)
            name3 = input("请输入您要查找的姓名")

            if name3 in dict1:
                print("%s的手机号为%s" % (name3, dict1[name3]))
            else:
                print("没有此人")

    elif num == "6":
        print("欢迎下次使用")
        break
    else:
        print("您的输入有误,请重新输入")

练习:

 

 score = []
# 2
score.append(68)
score.append(87)
score.append(92)
score.append(100)
score.append(76)
score.append(88)
score.append(54)
score.append(89)
score.append(76)
score.append(61)
print(score)
# 3
print(score[2])
# 4
print(score[0:6])
# 5
score.insert(2, 59)
print(score)
# 6
num = 76
print("num这个变量出现了%d次" % score.count(num))
# 7
print(num in score)
# 8
print("满分成绩的学生学号为%d" % score.index(100))
# 9
score[2] += 1
print(score)
# 10
del score[0]
print(score)
# 11
print("列表中元素的个数为%d个" % len(score))
# 12
score.sort()
print(score)
print("最高分:", max(score))
print("最低分:", min(score))
# 13
score.reverse()
print(score)
# 14
print("删除的元素是", score.pop())
print(score)
# 15
score.append(88)
print(score)
score.remove(88)
print(score)
# 16
score1 = [80, 61]
score2 = [71, 95, 82]
score1.extend(score2)
print(score1)
# 17
score1 = [80, 61]
score2 = list.copy(score1) * 5
print(score2)

 

 

 

 

           

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值