1. 程式人生 > 其它 >day12模組和包

day12模組和包

技術標籤:python基礎python

模組和包

1. map函式和reduce函式:

map函式

  • 用法一:
    map(函式,序列) - 將原序列中的元素,按照函式給定的標準轉換成新的序列
    函式的要求:
    a. 是一個函式
    b. 有一個引數(指向後面的這個序列中的元素)
    c。需要一個返回值(返回值就是新序列中的元素)

  • 用法二:
    map(函式, 序列1, 序列2)
    函式的要求:
    a. 是一個函式
    b. 有兩個引數(分別指向後面兩個序列中的元素)
    c. 需要一個返回值(返回值就是新序列中的元素)

練習1:獲取nums中的所有元素的個位數,產生一個新的列表

nums = [19, 78, 66
, 51, 34, 98, 22] result = map(lambda element: element % 10, nums) print(type(result), id(result)) print(result) print(list(result)) # [9, 8, 6, 1, 4, 8, 2]

練習2:產生一個新的序列,序列中的每個元素是math和Chinese的分數和: scores = [29, 43, 64, 96, 84]

math = [19, 23, 34, 56, 34]
chinese = [10, 20, 30, 40, 50]
result = map(lambda
element, element1: element+element1, math, chinese) print(list(result)) # [29, 43, 64, 96, 84]

練習3:合併兩個列表形成一個字典

names = ['蘋果', '小愛同學', '耳機', '天貓精靈']
nums = [1823, 22221, 891, 78]
# {'蘋果':1823, '小愛同學':22221, '耳機':891, '天貓精靈':78}
result = map(lambda element, element1: (element, element1), names, nums)
# result = map(lambda element, element1: [element, element1], names, nums) print(dict(result)) # {'蘋果': 1823, '小愛同學': 22221, '耳機': 891, '天貓精靈': 78}

練習4:將三個列表數字拼成一個列表的數字

nums1 = [19, 78, 778, 90]
nums2 = [1, 2, 3, 4]
nums3 = [111, 222, 333, 444, 555]

result = map(lambda x, y, z: int(f'{x}{y}{z}'), nums1, nums2, nums3)
print(list(result))     # [191111, 782222, 7783333, 904444]  注意:555沒拼上

reduce函式

  • 用法一:
    reduce(函式, 序列,初始值)
    函式的要求:
    a.函式
    b.有且只有兩個引數(第一個引數第一次指向初始值;從第二次開始指向上一次的計算結果);
    第二個引數指向序列中的每個元素
    c.有一個返回值(返回值決定合併規則)

    nums = [23, 34, 56, 67, 8]
    result = reduce(lambda x, y: x+y, nums, 0)
    print(result)
    

練習1:求所有的數的個位的和

nums = [23, 34, 56, 67, 8]
result = reduce(lambda x, y: x + y % 10, nums, 0)
print(result)

練習2:求所有的元素的數值的和

nums = [23, '78.9', 9, '10']
result = reduce(lambda x, y: x + float(y), nums, 0)
print(result)

練習3:求所有數字的乘積

nums = [23, 8.9, 'abc', True, '2']
result = reduce(lambda x, y: x * (y if type(y) in [int, float] else 1), nums, 1)
print(result)

2. 模組的使用

①什麼是模組
一個py檔案就是一個模組,檔名就是模組名
Python中可以在一個模組中去使用另外一個模組(命名要規範)中所有的全域性變數。

②匯入模組

1)直接匯入模組
import 模組名 - 匯入指定模組,匯入後需要通過’模組名.xxx’的形式使用模組中內容

2)直接匯入模組中的變數
from 模組名 import 變數名1,變數名2,… - 匯入模組中的指定變數

3)重新命名
import 模組名 as 新模組名 - 給模組重新命名,重新命名後使用新的名字
from 模組名 import 變數名1 as 新變數名1,變數名2 as 新變數名2,… - 匯入時給指定變數重新命名

4)萬用字元
from 模組名 import * - 匯入指定模組中所有的全域性變數

# ====================== import 模組名=============
# import 模組名
# import test
# 
# print(test.a)
# 
# test.fun1()
# 
# ==================== from - import ===========
# from test import a, name, fun1
# print(a)
# print(name)
# fun1()
# 
# 
# ===================給模組重新命名==============
# import test as test2
# test = 'haha'
# print(test)
# 
# print(test2.a, test2.name)
# 
# ============= ====給變數重新命名===============
# from test import a, name as name1
# 
# name = '小明1'
# print(name, name1)
# 
# 
# =============萬用字元========================
# from test import *
# print(a, name)
# fun1()

③匯入模組的原理
不管以什麼樣的方式匯入模組,在匯入模組的時候系統會自動進入被匯入的模組,將模組中的程式碼全部執行一遍
Python匯入模組的時候會自動檢測被匯入的模組之前是否已經匯入過,如果已經匯入過將不再重複匯入

import test
from test import a

import test
import test

# 這個if外面的程式碼才是會被其他模組執行的程式碼
if __name__ == '__main__':     # name可看做檔名  main可看做當前執行的檔案主函式名
    # 這個if裡面的程式碼不會被其他模組執行,只能被自己執行
    pass

2.包的使用

①什麼是包
包就是用來管理多個模組的一種特殊的資料夾(包含有____init____.py檔案的資料夾)

②怎麼使用包中模組的內容
通過包匯入內容的時候,會先執行____init____.py檔案中的內容

=========方法一:======
import files.jsonFile
files.jsonFile.json_read()

=========方法二:======
import files.jsonFile as jf
jf.json_read()

=========方法三:======
from files import jsonFile
jasonFile.json_read()

=========方法四:=======
from files.jsonFile import json_read  # json_read是函式名
json_read()

3. __init__
=====用法一:批量匯入======
from files import jsonFile
from files import otherFile
from files import testFile

=====用法二:提取高頻使用的部分內容====
from files import otherFile
from files import textFile

json_read = jsonFile.json_read
text_write = textFile.text_write


======用法三:封裝通用功能=======
def open_file():
    print('開啟檔案')