lines = [line for line in file(filename)] for in word_list:是什么意思

一句话:
列表推导式,将for循环的结果存储到lines中。

下面是具体解释:
python中使用lines = [line for line in file (file name)]的格式是列表推导式,这个等式是将for循环的结果存储到列表lines中。

列表推导式(又称列表解析式)提供了一种简明扼要的方法来创建列表,它是利用其创建新列表list的一个简单方法。列表推导式比较像for循环语句,必要时也可以加入if条件语句完善推导式。

它的结构是在一个中括号里包含一个表达式,然后是一个for语句,然后是 0 个或多个 for 或者 if
语句。表达式可以是任意的,意思是你可以在列表中放入任意类型的对象。

语法格式:[表达式 for 循环计数器 in 可迭代对象]
扩展资料

1、列表推导式标准格式

[Expression for Variable in  list]
也就是:[ 表达式  for  变量 in 列表]

如果需要加入if条件语句则是:[表达式 for 变量 in 列表 if 条件]

2、python推导式操作方法

(1)列表推导式标准操作方法:

>>> a = [1,2,3,4,5,6,7,8,9,10]

>>> [3*x for x in a]

输出:[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]

(2)如果没有给定列表,也可以用range()方法:

>>> [3*x for x in range(1,11)]

输出:[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]

(3)加入if条件判断语句的列表推导式:

比如要取列表a中的偶数

>>> a = [1,2,3,4,5,6,7,8,9,10]

>>> [x for x in a if x % 2 == 0]

输出:[2, 4, 6, 8, 10]
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将下列代码改为对上海证券交易所网站公告爬取from concurrent.futures import ThreadPoolExecutor import requests headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.159 Safari/537.36" } def download_pdf(url, code, num, date): print(f'开始下载 data/{code}_{date}_{num}.pdf') resp = requests.get(url, headers=headers) with open(f'E:/深交所pdf/{code}_{date}_{num}.pdf', 'wb') as f: f.write(resp.content) resp.close() print(f'E:/深交所pdf/{code}_{date}_{num}.pdf 下载完毕!') if __name__ == '__main__': domain = 'http://www.sse.cn' with ThreadPoolExecutor(30) as t: with open('target.csv', 'r') as f: lines = f.readlines() for line in lines: param = list(line.split()) form = { 'seDate': [param[3], param[3]], 'stock': [param[0]], 'channelCode': ['listedNotice_disc'], 'pageSize': '50', 'pageNum': '1' } # 获取文件列表的url get_file_list_url = 'http://www.sse.com.cn/disclosure/listedinfo/announcement/json/announce_type.json?v=0.9715488799747511' resp = requests.post(get_file_list_url, headers=headers, json=form) # resp.encoding = 'utf-8' # print(resp.json()) js = resp.json() resp.close() tot = 0 for data in js['data']: tot += 1 download_url = domain + f'/api/disc/info/download?id={data["id"]}' t.submit(download_pdf, url=download_url, code=param[0], num=tot, date=param[3]) print("下载完毕!!!") # doc_id = '' # download_url = domain + f'/api/disc/info/download?id={"c998875f-9097-403e-a682-cd0147ce10ae"}' # resp = requests.get(download_url, headers=headers) # with open(f'{"c998875f-9097-403e-a682-cd0147ce10ae"}.pdf', 'wb') as f: # f.write(resp.content) # resp.close()
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值