数据类型转换
# 1.哪些类型可以转化为数字 res = int('10') print(res) res = int('-3') print(res) res = float('.15') print(res) res = float('-.15') print(res) res = float('-3.15') print(res) # 作业:判断所有能被转换为数字类型的字符串,并转化 # 2.数字转化字符串 print(str(10)) # 3.字符串与列表相互转换 ****** s = 'abc123呵呵' print(list(s)) # ['a', 'b', 'c', '1', '2', '3', '呵', '呵'] 没有对应的 str(ls) ls = ['a', 'b', 'c', '1', '2', '3', '呵', '呵'] n_s = ''.join(ls) print(n_s) # 'abc123呵呵' # s1 = 'a b c 1 2 3 呵 呵' # res = s1.split() # 默认按空格拆 s1 = 'a b c 1 2 3 呵 呵' res = s1.split() print(res) # 必须掌握 s2 = 'ie=UTF-8&wd=你好帅' res = s2.split('&') print(res) # ['ie=UTF-8', 'wd=你好帅'] ls2 = ['ie=UTF-8', 'wd=你好帅'] n_s2 = '@'.join(ls2) print(n_s2) # ie=UTF-8@wd=你好帅 # 4.需求:"ie=UTF-8&wd=你好帅" => [('ie', 'UTF-8'), ('wd', '你好帅')] res = [] s4 = "ie=UTF-8&wd=你好帅" ls4 = s4.split('&') # ['ie=UTF-8', 'wd=你好帅'] for ele in ls4: # v = ie=UTF-8 | wd=你好帅 k, v = ele.split('=') # k: ie v: UTF-8 res.append((k, v)) print(res) # 5.需求:"ie=UTF-8&wd=你好帅" => {'ie': 'UTF-8', 'wd': '你好帅'} res = {} s5 = "ie=UTF-8&wd=你好帅" ls5 = s5.split('&') # ['ie=UTF-8', 'wd=你好帅'] for ele in ls5: # v = ie=UTF-8 | wd=你好帅 k, v = ele.split('=') # k: ie v: UTF-8 res[k] = v print(res) # 6.需求:[('ie', 'UTF-8'), ('wd', '你好帅')] => {'ie': 'UTF-8', 'wd': '你好帅'} res = {} ls6 = [('ie', 'UTF-8'), ('wd', '你好帅')] for k, v in ls6: res[k] = v print(res) # 7.list与tuple、set直接相互转化 - 直接 类型() # 8.需求:将汉字转化为数字 # 将 壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟 # 转化为 1、2、3、4、5、6、7、8、9、10、100、100 # 作业:壹仟捌佰玖拾叁 => 1893 num_map = { '壹': 1, '贰': 2, '仟': 1000 }