Python測試開發預習課6/18

注意:
切片是可以越界的
遍歷不要改原字符串

1、abcxxx,請統計一下x有多少個?用函數實現

知識點

>>> s="abcaxxx"
>>> s.count("x")
3

在這裏插入圖片描述
count函數的算法
算法:
1 定義一個函數,參數傳遞一個字符串
2 聲明一個變量letter_count存儲某個字符出現的個數
3 遍歷字符串,逐一拿出來,判斷是否是你想要統計的那個
4 如果是,則letter_count+1
5 如果不是,則什麼都不做
6 把函數中的統計結果變量返回回來 return letter_count

def count(s,target_letter):
    letter_count = 0
    for i in s:
        if i == target_letter:
            letter_count+=1
    return letter_count

print(count("abcxxx","x"))

在這裏插入圖片描述

2、abcxabcyabc,請統計一下abc有多少個?用函數實現

算法:
例如:0位置的s[0:3]==“xxx” 當前i是0,
滿足的條件下1和2不需要做if判斷了,直接跳過去
把當前的i+1,i+2這兩個位置,放入到filter_position
代碼:

def count(s,target_letters):
    string_count = 0
    length=len(target_letters)
    filter_position = []
    for i in range(len(s)):
        if i in filter_position:
            continue
        if s[i:i+length] == target_letters:
            string_count+=1
            for j in range(1,length):
                filter_position.append(i+j)
    return string_count

print(count("xxxxabcxxxx","xxx"))

3、列表的增刪改查

>>> arr=[]
>>> type(arr)
<class 'list'>
>>> arr.append(1)
>>> arr.append("1")
>>> arr.append([])
>>> arr.append((1,2))
>>> arr.append(1.24)
>>> arr
[1, '1', [], (1, 2), 1.24]
>>> len(arr)
5
>>> arr.insert(0,"xyz")
>>> arr[0]
'xyz'
>>> arr[2]#列表是一個序列,基於座標查看
'1'
>>> for i in range(10):
...     arr.append(i)
...
>>> arr
['xyz', 1, '1', [], (1, 2), 1.24, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del arr[0]
>>> del arr[0]
>>> arr
['1', [], (1, 2), 1.24, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del arr[2:5]
>>> arr
['1', [], 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[0]="aaa"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> arr[0]="xxx"
>>> arr
['xxx', [], 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> arr[0:4]="111"
>>> arr
['1', '1', '1', 3, 4, 5, 6, 7, 8, 9]
>>> for i in arr:
...     print(i)
...
1
1
1
3
4
5
6
7
8
9
>>> for i in range(len(arr)):
...     print(arr[i])
...
1
1
1
3
4
5
6
7
8
9
>>> a=[1,2,3]
>>> arr=["a","b"]
>>> arr.extend(a)
>>> arr
['a', 'b', 1, 2, 3]

在這裏插入圖片描述
在這裏插入圖片描述

4、元組的增刪改查–元組:它所有子元素的地址,是不能改變的

>>> a=()
>>> type(a)
<class 'tuple'>
>>> a=(1)
>>> type(a)
<class 'int'>
>>> a=(1,)
>>> type(a)
<class 'tuple'>
>>> a=(1,"a",[],{})
>>> a[0]="x"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> del a[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion
>>> a[0]
1
>>> a[1]
'a'
>>> a[2]
[]
>>> a[3]
{}
>>> a[4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>>
>>>
>>>
>>> a
(1, 'a', [], {})
>>> a[2]
[]
>>> a[2].append(100)
>>> a[2].append(233)
>>> a
(1, 'a', [100, 233], {})
>>> a
(1, 'a', [100, 233], {})

在這裏插入圖片描述

5、字典的增刪改查–字典的key不能重複,如果賦值重複了,會把value替換掉

>>> d={}
>>> type(d)
<class 'dict'>
>>> d["1"]=100
>>> d
{'1': 100}
>>> d[[1]]=100
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> d
{'1': 100}
>>> d[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 1
>>> d["1"]
100
>>> d[10000]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 10000
>>> d["2"]=9000
>>> d["2"]=9000
>>> d
{'1': 100, '2': 9000}
>>> del d["2"]
>>> d
{'1': 100}
>>>
>>>
>>>
>>>
>>>
>>> d={1:2,3:4,5:6}
>>> d
{1: 2, 3: 4, 5: 6}
>>> d.keys()
dict_keys([1, 3, 5])
>>> list(d.keys())
[1, 3, 5]
>>> for i in d.keys():
...     print(i)
...
1
3
5
>>> for i in d.values():
...     print(i)
...
2
4
6
>>> for k,v in d.items():
...     print(k,"=",value)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'value' is not defined
>>> for k,v in d.items():
...     print(k,"=",v)
...
1 = 2
3 = 4
5 = 6
>>> d
{1: 2, 3: 4, 5: 6}
>>> d.clear()
>>> d
{}
>>> d={1:2,3:4,5:6}
>>> for i in d.keys():
...     d[i]=1190
...
>>> d
{1: 1190, 3: 1190, 5: 1190}
>>> new_d={}
>>> for i in d.keys():
...     if i%2==1:
...         continue
...         new_d[i]=d[i]
...
>>> new_d[i]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 5
>>> new_d
{}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章