Python函數--按字母查找

目錄

a

b

c

d

e

f

g

h

i

j

l

m

n

0

p

q

r

s

t

u

v

w

x

yz


a

1:append():列表結尾追加數據,列表追加數據的時候,直接在原列表裏面追加了指定數據,即修改了原列表,故列表爲可變型數據

    如果append()追加的數據是一個序列,則追加整個序列到列表

   列表序列.append(數據)

list = ['a', 'b', 'c', 'd']
list.append('hello')
print(list)
list.append(['hello', 'world'])
print(list)
 
['a', 'b', 'c', 'd', 'hello']
['a', 'b', 'c', 'd', 'hello', ['hello', 'world']]

2:add():增加,不能增加序列,因爲集合有去重功能,所以,當向集合內追加的數據是當前集合已有的數據的話,則不會進行任何操作

  集合.add(要增加的數據)

s1=set('abcdefg')
s1.add('10')
print(s1)
 
{'d', 'b', '10', 'f', 'e', 'c', 'g', 'a'}

3:abs() :可以完成對數字求絕對值計算。

print(abs(-10))
    
10

 

 

b

c

1:count():  返回某個⼦串在字符串中出現的次數

   字符串序列.count(子串, 開始位置下標, 結束位置下標)

   列表序列.count(數據)

str = "01234567"
print(str.count('3',0,1))
print(str.count('3',0,4))
 
0
1

2:capitalize():將字符串第一個字符轉換成大寫

      字符串序列.capitalize()

str="hello world"
print(str.capitalize())
 
Hello world

3:center():返回一個原字符串居中對齊,並使用指定字符(默認空格)填充至對應長度的新字符串

        字符串序列.center(長度,填充字符)  

str = "Hello World"
print(str.center(20, '*'))
 
****Hello World*****

4:clear():清空列表

  列表序列\集合\字典.clear()

list = ['a', 'b', 'a', 'c']
list.clear()
print(list)
 
[]

5:copy():複製

 列表序列\集合\字典.copy()

list = [1, 3, 4, 5, 2]
temp = list.copy()
print(temp)
 
[1, 3, 4, 5, 2]

6:close:關閉打開的文件

  文件對象.close()

f = open('test.txt', 'r')
print(f.read())
f.close()

 

d

1:del 刪除,可以刪除列表(數據類型都可以,但要注意有的數據類型不支持下標,但是可以整個數據類型註銷),或者刪除指定數據

  del 目標/del()

list = ['a', 'b', 'c', 'd']
del list[1]
print(list)
del list
print(list)
 
['a', 'c', 'd']
<class 'list'>

 2:discard():刪除集合中的指定數據,如果數據不存在也不會報錯,語法和remove()一樣

e

1:endswith():檢查字符串是否以指定子串結尾,是則返回True,否則返回False.如果設置開始和結束位置下標,則在指定範圍內檢查,和startswith()用法一樣

2:extend():列表結尾追加數據,如果數據是一個序列,則將這個序列的數據逐一添加到列表

  列表序列.extend(數據)

list = ['a', 'b', 'c', 'd']
list.extend('hello')
print(list)
list.extend(['hello', 'world'])
print(list)
 
['a', 'b', 'c', 'd', 'h', 'e', 'l', 'l', 'o']
['a', 'b', 'c', 'd', 'h', 'e', 'l', 'l', 'o', 'hello', 'world']

3:enumerate(可遍歷對象, start=0):函數⽤於將⼀個可遍歷的數據對象(如列表、元組或字符串)組合爲⼀個索引序 列,同時列出數據和數據下標,⼀般⽤在 for 循環當中。start 用來設置遍歷數據的下標起始值,默認爲0

list1 = ['a', 'b', 'c', 'd']
for i in enumerate(list1, 3):
    print(i, end='')
 
(3, 'a')(4, 'b')(5, 'c')(6, 'd')

 

f

1:find():檢測某個⼦串是否包含在這個字符串中,如果在返回這個⼦串開始的位置下標,否則則返 -1。

     字符串序列.find(子串,開始位置下標, 結束位置下標)

str = "01234567"
print(str.find('3',0,4))
 
3

2:fifilter(func, lst) 函數⽤於過濾序列, 過濾掉不符合條件的元素, 返回⼀個 fifilter 對象,。如果要轉換爲列表,可以使⽤ list() 來轉換。

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
fuc = lambda x: x % 2 ==0
result = filter(fuc, list1)
print(result)
print(list(result))

<filter object at 0x0000020588DE2B20>
[2, 4, 6, 8, 10]

 

 

g

1:get():查找

  字典序列.get(key,默認值)

  如果當前查找的key不存在則返回第二個參數(默認值),如果省略第二個參數,則返回None

dict1 = {'name': 'tom', 'age': 18, 'gender': 'male'}
print(dict1.get('name', 'NO'))
print(dict1.get('family', 'NO'))
 
tom
NO

h

i

1:input("提示信息")

  • 當程序執⾏到 input ,等待⽤戶輸⼊,輸⼊完成之後才繼續向下執⾏。
  • 在Python中, input 接收⽤戶輸⼊後,⼀般存儲到變量,⽅便使⽤。
  • 在Python中, input 會把接收到的任意⽤戶輸⼊的數據都當做字符串處理。
num = input("請輸入數字")
print(f'您輸入的數字是{num}')
 
請輸入數字1
您輸入的數字是1

 2:index():檢測某個⼦串是否包含在這個字符串中,如果在返回這個⼦串開始的位置下標,否則報異常。

     字符串序列.index(子串,開始位置下標, 結束位置下標)

     列表序列.index(數據,開始位置下標,結束位置下標)

str = "01234567"
print(str.index('3',0,1))
 
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: substring not found

 3:isalpha():如果字符串至少有一個字符並且所有的字符都是字母則返回True,否則返回False

      字符串序列.isalpha()

str = "HelloWorld"
print(str.isalpha())
 
True

4:isdigit():如果字符串只包含數字則返回True否則返回False,語法與isalpha()一樣

5:isalnum():如果字符串至少有一個字符並且所有字符都是字母或數字則返回True,否則返回False,語法與isalpha()一樣

6:isspace():如果字符串中只包含空格,則返回True,否則返回False

7:in:判斷指定數據是否在某個列表序列,如果在返回True,否則返回False

  數據 in 列表序列\ 字符串\ 元組\ 集合\ 字典

list = ['a', 'b', 'c', 'd']
print('a' in list)
 
True

8:insert():指定位置新增數據

  列表序列.insert(位置下標,數據)

list = ['a', 'b', 'c', 'd']
list.insert(1, 'A')
print(list)
 
['a', 'A', 'b', 'c', 'd']

9:items():獲取字典裏的數據(包括鍵值)

   字典.items()

dict1 = {'name': 'tom', 'age': 18, 'gender': 'male'}
print(dict1.items())
 
dict_items([('name', 'tom'), ('age', 18), ('gender', 'male')])

10:id(): 獲取變量所在內存地址編號(十進制)

a = [1, 2, 3]
print(id(a))
    
2927484854400  

 

j

1:join():⽤⼀個字符或⼦串合併字符串,即是將多個字符串合併爲⼀個新的字符串。

     字符或子串.join(多字符串組成的序列)

str=['aa', 'bb', 'cc', 'dd']
print('*'.join(str))
 
aa*bb*cc*dd

1:keys():獲取字典內的鍵值

  字典.keys()

dict1 = {'name': 'tom', 'age': 18, 'gender': 'male'}
print(dict1.keys())
 
dict_keys(['name', 'age', 'gender'])

l

1:lower():將字符串中大寫轉小寫

 字符串序列.lower()

str = "Hello World"
print(str.lower())
 
hello world

2:lstrip():刪除字符串左側空白字符

    字符串序列.lstrip()

str = "  Hello World  "
print(str.lstrip())
 
Hello World  

3:ljust():返回一個原字符串左對齊,並使用指定字符(默認空格)填充至對應長度的新字符串

        字符串序列.ljust(長度,填充字符)

str = "Hello World"
print(str.ljust(20, '*'))
 
Hello World*********

4:len():訪問列表長度,即列表中數據的個數

  len(列表序列\ 字符串\ 元組\ 集合\ 字典)

list = ['a', 'b', 'c', 'd']
print(len(list))
 
4

5:list():將某個序列轉換成列表

tu1 = ('a', 'b', 'c', 'd')
set1 = {0, 1, 2, 3, 4}
l1 = list(tu1)
l2 = list(set1)
print(type(l1))
print(type(l2))
print(l1)
print(l2)
 
<class 'list'>
<class 'list'>
['a', 'b', 'c', 'd']
[0, 1, 2, 3, 4]

6:lambda語法:如果⼀個函數有⼀個返回值,並且只有⼀句代碼,可以使⽤ lambda簡化。

  lambda 參數列表:表達式`*

  •     lambda表達式的參數可有可⽆,函數的參數在lambda表達式中完全適⽤。
  •  lambda函數能接收任何數量的參數但只能返回⼀個表達式的值
  •  lambda也可用來sort函數中給字典進行排序
fun = lambda a,b : a+b
print(fun(1, 2))

stu = [
    {'name': 'ebc', 'age': 18},
    {'name': 'buf', 'age': 17},
    {'name': 'cha', 'age': 19}
]

stu.sort(key = lambda x: x['name'], reverse = True)
print(stu)

3
[{'name': 'ebc', 'age': 18}, {'name': 'cha', 'age': 19}, {'name': 'buf', 'age': 17}]

 

m

1:max():返回容器中元素最大值

list1 = ['a', 'b', 'c', 'd']
tuple1 = (0, 1, 2, 3, 4, 5)
print(max(list1))
print(max(tuple1))
 
d
5

2:min():返回容器中元素的最小值

list1 = ['a', 'b', 'c', 'd']
tuple1 = (0, 1, 2, 3, 4, 5)
print(min(list1))
print(min(tuple1))
 
a
0

3:map():map(func, lst),將傳⼊的函數變量func作⽤到lst變量的每個元素中,並將結果組成新的列表(Python2)/迭代器(Python3)返回

list1 = [1, 2, 3, 4, 5]
fuc = lambda x : x**2
tem = map(fuc, list1)
print(tem)
print(list(tem))

<map object at 0x0000019A423AD100>
[1, 4, 9, 16, 25]

 

n

1:not in :判斷指定數據是否不在某個列表序列,如果不在返回True,否則返回False,語法與 in 相同  

0

open(name, mode):,使⽤open函數,可以打開⼀個已經存在的⽂件,或者創建⼀個新⽂件

  name:是要打開的⽬標⽂件名的字符串(可以包含⽂件所在的具體路徑)。

  mode:設置打開⽂件的模式(訪問模式):只讀、寫⼊、追加等

f = open('test.txt', 'r')
f.close()

 

p

1:pop():刪除指定下標的數據(默認爲最後一個),並返回該數據

  列表序列\集合\字典.pop(下標)

list = ['a', 'b', 'c', 'd']
temp = list.pop(1)
print(temp)
print(list)
 
b
['a', 'c', 'd']

q

r

1:導出random模塊,使用random模塊中的隨機整數功能

     random.randint(開始,結束)

import random
num=random.randint(0,2)
print(num)
 
0

2:rfind():    和find()功能相同,但查找⽅向爲右側開始.

3:rindex(): 和index()功能相同,但查找⽅向爲右側開始。

4:replace():替換

      字符串序列.replace(舊子串,新字串,替換次數)

str = "01234567"
print(str.replace('0','m',10))
 
m1234567

 5:rstrip():刪除字符串右側空白字符,和lstrip()用法相同

6:rjust():返回一個原字符串右對齊,並使用指定字符(默認空格)填充至對應長度的新字符串,語法和ljust()相同

7:remove():移除列表中某個數據的第一個匹配項

  列表序列\集合.remove

list = ['a', 'b', 'a', 'c']
list.remove('a')
print(list)
 
['b', 'a', 'c']

8:reverse():逆置

  列表序列.reverse()

list = ['a', 'b', 'a', 'c']
list.reverse()
print(list)
 
['c', 'a', 'b', 'a']

9:range(start, end, step):生成從start(默認爲0)到end的數字,步長爲step(默認爲1),供for循環使用

for i in range(0, 10, 2):
    print(i, end='')
 
02468

 10:round()函數可以完成對數字四捨五入的計算

print(round(1.1))
print(round(2.55))

1
3

11:reduce(func(x,y),lst),其中func必須有兩個參數。每次func計算的結果繼續和序列的下⼀個元素做累積計算。

import functools    #需要調用functools模塊
list1 = [1,2, 3, 4, 5]

fuc = lambda a, b : a + b
result = functools.reduce(fuc, list1)
print(result)

15

12:read:⽂件對象.read(num)

  num表示要從⽂件中讀取的數據的⻓度(單位是字節),如果沒有傳⼊num,那麼就表示讀取⽂件中所有的數據

f = open('test.txt', 'r')
print(f.read())
f.close()

13:readlines(): readlines可以按照⾏的⽅式把整個⽂件中的內容進⾏⼀次性讀取,並且返回的是⼀個列表,其中每⼀⾏的數據爲⼀個元素。

f = open('test.txt', 'r')
print(f.readlines())
f.close()

['sdaddasdlilo\n', 'dsadda\n', 'dsadsad']

 14:readline():⼀次讀取⼀⾏內容。

f = open('test.txt', 'r')
print(f.readline())
print(f.readline())
f.close()

sdaddasdli
dsadda

s

1:split():按照指定字符分割字符串,如果分割字符是原有字符串中的字符,分割後則會丟失該字符

     字符串序列.split(分割字符,num)

str = "a b c d a b"
print(str.split('b',2))
print(str.split())
 
['a ', ' c d a ', '']
['a', 'b', 'c', 'd', 'a', 'b']

2:strip(): 刪除字符串兩側空白字符,和lstrip()用法相同

3:startswith():檢查字符串是否是以指定子串開頭,是則返回True,否則返回False.如果設置開始和結束位置下標,則在指定範圍內檢查

     字符串序列.startswith(子串,開始位置下標,結束位置下標)

str = "Hello World"
print(str.startswith('He', 0, 10))
 
True

4:sort():排序

  列表序列.sort(key=None, reverse = False(默認))   reverse表示排序規則,reverse =True 降序, reverse False 升序(默認)

list = [1, 3, 4, 5, 2]
list.sort(reverse=True)
print(list)
list.sort()
print(list)
 
[5, 4, 3, 2, 1]
[1, 2, 3, 4, 5]

5:set():創建集合使用{}或set(),但是如果要創建空集合只能用set(),因爲{}用來創建空字典

s1=set('abcdefg')
print(s1)
 
{'a', 'g', 'c', 'f', 'e', 'd', 'b'}

6:set():將某個序列轉換成集合

tu1 = ('a', 'b', 'c', 'd')
l1 = [0, 1, 2, 3, 4]
s1 = set(tu1)
s2 = set(l1)
print(type(s1))
print(type(s2))
print(s1)
print(s2)
 
<class 'set'>
<class 'set'>
{'b', 'c', 'd', 'a'}
{0, 1, 2, 3, 4}

 

t

1:title():將字符串每個單詞首字母轉換成大寫

  字符串序列.tiltle()

str="hello world"
print(str.title())
 
Hello World

 2:tuple():將某個序列轉換爲元組

list1 = ['a', 'b', 'c', 'd']
set1 = {0, 1, 2, 3, 4}
tu1 = tuple(list1)
tu2 = tuple(set1)
print(type(tu1))
print(type(tu2))
print(tu1)
print(tu2)
 
<class 'tuple'>
<class 'tuple'>
('a', 'b', 'c', 'd')
(0, 1, 2, 3, 4)

u

1:upper():將字符串中小寫轉大寫

        字符串序列.upper()

str = "Hello World"
print(str.upper())
 
HELLO WORLD

 2:update():追加的數據是序列(列表或字符串)

  集合.update(要追加的數據)

s1=set('abcdefg')
s1.update('10')
print(s1)
s1.update(['100', '200'])
print(s1)
 
{'f', '1', 'g', 'e', '0', 'c', 'd', 'b', 'a'}
{'f', '100', '1', 'g', 'e', '0', 'c', '200', 'd', 'b', 'a'}

v

1:values():獲取數據(不要鍵值)

dict1 = {'name': 'tom', 'age': 18, 'gender': 'male'}
print(dict1.values())
 
dict_values(['tom', 18, 'male'])

 

w

1:write:文件對象.write('內容')

f = open('test.txt', 'w')
f.write('hello')
f.close()

 

x

y

z

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