import mod_1 from pac_2.mod_2 import func as func2 from pac_3.mod_3 import func as func3 from pac_3.pac_4.mod_4 import func as func4 from pac_3.pac_5 import *
if __name__ == '__main__': mod_1.func() func2() func3() func4() mod_5_1.func() mod_5_2.func()
执行结果为:
1 2 3 4 5 6
This is mod_1 This is pac_2.mod_2 This is pac_3.mod_3 This is pac_3.pac_4.mod_4 This is pac_3.pac_5.mod_5_1 This is pac_3.pac_5.mod_5_2
l = [1,3,4,2,5] print ('l is %s' % l) sl = sorted(l) print ('sorted(l) is %s' % sl) print ('now l is %s' % l) l.sort() print ('after l.sorl() l is %s' % l)
输出结果为:
1 2 3 4
l is [1, 3, 4, 2, 5] sorted(l) is [1, 2, 3, 4, 5] now l is [1, 3, 4, 2, 5] after l.sorl() l is [1, 2, 3, 4, 5]
# 过滤出数组中的偶数 list_1 = [1,2,3,4,5] filter_l = filter(lambda x : x%2 == 0,list_1) list_2 = [i for i in filter_l] print('list is %s' % list_1) print('after filter list is %s' % list_2) # [2, 4]
# 删除字符串中的空格 str_1 = 'hello world !' filter_s= filter(lambda c : c != ' ',str_1) str_2 = ''.join(filter_s) print('str is %s' % str_1) print('after filter str is %s' % str_2) # helloworld!
# 将所有的元素求平方 list_1 = [1,2,3,4,5] map_l = map(lambda x : x ** 2,list_1) list_2 = [i for i in map_l] print('list is %s' % list_1) print('after map list is %s' % list_2) # [1, 4, 9, 16, 25]
# 将所有的字符转为大写 str_1 = 'hello world !' map_s= map(lambda c : c.upper(),str_1) str_2 = ''.join(map_s) print('str is %s' % str_1) print('after map str is %s' % str_2) # HELLO WORLD !
from struct import * a, b, c = 1, 2, 3 print (a, b, c) p = pack('hhl', a, b, c) # b'\x01\x00\x02\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00' print (p) a, b, c = unpack('hhl', p) print (a, b, c) # 1 2 3
# 屏蔽hell 替换为**** inStr = "open the shell oh what the hell i mean hello" outStr = re.sub(r"\bhell\b", "****", inStr) # 注意使用raw字符串否则\b会失效 print(outStr) # open the shell oh what the **** i mean hello
使用捕获组(capture group),
1 2 3 4 5 6 7 8 9 10
import re
# 使用捕获组 将A4替换为A-4 ... inStr = "A4 is ready but B8 and B9 are not" outStr = re.sub("(\w)(\d+)", "\g<1>-\g<2>", inStr) ''' outStr = re.sub(r"(?P<letter>\w)(?P<figure>\d)", "\g<letter>-\g<figure>", inStr) '''
print(outStr) # A-4 is ready but B-8 and B-9 are not