diff --git "a/05_\346\225\260\346\215\256\345\256\271\345\231\250/02_list\345\210\227\350\241\250\347\232\204\345\270\270\347\224\250\346\223\215\344\275\234.py" "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/02_list\345\210\227\350\241\250\347\232\204\345\270\270\347\224\250\346\223\215\344\275\234.py" index c8595b1b90b7d7e1df186270939e8611ee1e445d..0a0a6ac732d3c7885cfd0f705bc4401de4758b2d 100644 --- "a/05_\346\225\260\346\215\256\345\256\271\345\231\250/02_list\345\210\227\350\241\250\347\232\204\345\270\270\347\224\250\346\223\215\344\275\234.py" +++ "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/02_list\345\210\227\350\241\250\347\232\204\345\270\270\347\224\250\346\223\215\344\275\234.py" @@ -51,7 +51,15 @@ count = len(mylist) print(f"列表的元素数量总共有:{count}个") +# 7. 删除某元素在列表中的第一个匹配项 +mylist = ["itcast", "itheima", "itcast", "itheima", "python"] +mylist.reverse() +print(f"通过reverse方法翻转后,列表的结果是:{mylist}") +# 7. 删除某元素在列表中的第一个匹配项 +mylist = ["itcast", "itheima", "itcast", "itheima", "python"] +mylist.remove("itheima") +print(f"通过remove方法移除元素后,列表的结果是:{mylist}") diff --git "a/05_\346\225\260\346\215\256\345\256\271\345\231\250/04_list\345\210\227\350\241\250\347\232\204\345\276\252\347\216\257.py" "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/04_list\345\210\227\350\241\250\347\232\204\345\276\252\347\216\257.py" index bd5867127277bba08070a16359d07ab22d778d17..485aa2f4be8b1b2f5c5910a6f74534a0b72dcd78 100644 --- "a/05_\346\225\260\346\215\256\345\256\271\345\231\250/04_list\345\210\227\350\241\250\347\232\204\345\276\252\347\216\257.py" +++ "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/04_list\345\210\227\350\241\250\347\232\204\345\276\252\347\216\257.py" @@ -11,13 +11,21 @@ def list_while_func(): mylist = ["传智教育", "黑马程序员", "Python"] # 循环控制变量:通过下标索引来控制,默认0 # 每一次循环将下标苏姚 + index = 0 + while index < len(mylist): + print(mylist[index]) + index += 1 - - +# list_while_func() def list_for_func(): """ 使用for循环遍历列表的演示函数 :return: """ + mylist = ["传智教育", "黑马程序员", "Python"] + for e in mylist: + print(e) + +list_for_func() diff --git "a/05_\346\225\260\346\215\256\345\256\271\345\231\250/06_\345\255\227\347\254\246\344\270\262.py" "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/06_\345\255\227\347\254\246\344\270\262.py" index aa8e4297ccece691c11ac211675364e70cac6b60..18aa00afa4addabc9113036f5720d20e4a594e5c 100644 --- "a/05_\346\225\260\346\215\256\345\256\271\345\231\250/06_\345\255\227\347\254\246\344\270\262.py" +++ "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/06_\345\255\227\347\254\246\344\270\262.py" @@ -38,3 +38,8 @@ print(f"字符串{my_str}中it出现的次数是:{count}") # 统计字符串的长度, len() num = len(my_str) print(f"字符串{my_str}的长度是:{num}") + + +my_str = "12itheima and itcast21" +new_my_str = my_str.strip("231") +print(f"字符串{my_str}被strip('12')后,结果:{new_my_str}") \ No newline at end of file diff --git "a/05_\346\225\260\346\215\256\345\256\271\345\231\250/08_\345\272\217\345\210\227\345\222\214\345\210\207\347\211\207.py" "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/08_\345\272\217\345\210\227\345\222\214\345\210\207\347\211\207.py" index fb047f2b944d1d1ea56f7ec2107f222becdfabf2..c7197b017eb59ec1444519f77e1626aea90dc449 100644 --- "a/05_\346\225\260\346\215\256\345\256\271\345\231\250/08_\345\272\217\345\210\227\345\222\214\345\210\207\347\211\207.py" +++ "b/05_\346\225\260\346\215\256\345\256\271\345\231\250/08_\345\272\217\345\210\227\345\222\214\345\210\207\347\211\207.py" @@ -1,37 +1,27 @@ """ 演示对序列进行切片操作 """ - # 对list进行切片,从1开始,4结束,步长1 my_list = [0, 1, 2, 3, 4, 5, 6] -result1 = my_list[1:4] # 步长默认是1,所以可以省略不写 +result1 = my_list[1:4] # 步长默认是1,所以可以省略不写 print(f"结果1:{result1}") - # 对tuple进行切片,从头开始,到最后结束,步长1 my_tuple = (0, 1, 2, 3, 4, 5, 6) -result2 = my_tuple[:] # 起始和结束不写表示从头到尾,步长为1可以省略 +result2 = my_tuple[:] # 起始和结束不写表示从头到尾,步长为1可以省略 print(f"结果2:{result2}") - # 对str进行切片,从头开始,到最后结束,步长2 my_str = "01234567" result3 = my_str[::2] print(f"结果3:{result3}") - - # 对str进行切片,从头开始,到最后结束,步长-1 my_str = "01234567" -result4 = my_str[::-1] # 等同于将序列反转了 +result4 = my_str[::-1] # 等同于将序列反转了 print(f"结果4:{result4}") - - # 对列表进行切片,从3开始,到1结束,步长-1 my_list = [0, 1, 2, 3, 4, 5, 6] result5 = my_list[3:1:-1] print(f"结果5:{result5}") - - # 对元组进行切片,从头开始,到尾结束,步长-2 my_tuple = (0, 1, 2, 3, 4, 5, 6) result6 = my_tuple[::-2] print(f"结果6:{result6}") -