镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 1 code name=['燃灯古佛','释迦摩尼佛','弥勒佛'] for item in name: print(item) 2 show ------------------------------------------博文的精髓,在技术部分,更在镇场一诗.Python是优秀的语言,值得努
一般Python for语句前不加语句,但我在机器学习实战中看到了这两条语句: featList = [example[i] for example in dataSet] classList = [example[-1] for example in dataSet] 多方研究和询问,得到如下解释: 语句featList = [example[i] for example in dataSet]作用为: 将dataSet中的数据按行依次放入example中,然后取得example中的examp
#-*- coding:UTF-8 -*- squares=[] for x in range(10): squares.append(x**2) print squares #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] #等价于 squares=[x**2 for x in range(10)] print squares #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] combs=[] for x in [1,2,3]: for y
对于Python, 一切事物都是对象,对象基于类创建!! 注:查看对象相关成员var,type, dir 一.整数 如: 18.73.84 每一个整数都具备如下需要知道的功能: def bit_length(self): """ 返回表示该数字的时占用的最少位数 """ """ int.bit_length() -> int Number of bits necessary to represent self
替换 fnr, fnr_source, fnw = 'my.py.html', '产品清单.txt', 'my.py.res.html'd_source = {}with open(fnr_source, 'r', encoding='utf-8') as fr: for i in fr: ii = i.replace('\n', '') if len(ii.replace(' ', '')) == 0: continue if '<p>' not in ii: k = ii d_source
def read_data(file_name): if not re.findall(".txt", file_name): file_name += ".txt" L = [[] )]] with open(file_name) as r: for d in r: j = d.split("|") for i in range(len(L)): L[i].append(j[i].strip()) return L
应用python random标准库做一个随机生成密码的程序,可以随机生成任意多个字符.(基于python2.7,如果是python3需要修改下) 案例: #-*-coding:utf-8 -*-#author:wangxing import randomimport stringimport sys #存储大小写字母和数字,特殊字符列表STR = [chr(i) for i in range(65,91)] #65-91对应字符A-Zstr = [chr(i) for i in range(9
内容概要 for循环 range(start,end,step)函数 生成随机数列表 list()函数 将range()的结果整合到某个列表 列表的操作 切片(start: end :step) 元组 for循环 用for循环可以遍历列表中的每一个元素 demo1: people = ['alice' , 'bob' , 'david'] for person in people: print(person) # 缩进和冒号很重要,是python语法中的一部分 > alice bob david