(3) 参考利用下面的程序代码,完成代码注释中要求的两项任务。 import re """ 下面ref是2020年CVPR的最佳论文的pdf格式直接另存为文本文件后, 截取的参考文献前6篇的文本部分。 请利用该科研文献的这部分文本,利用正则表达式、字符串处理等方法, 编程实现对这6篇参考文献按下面的方式进行排序输出。 a.按参考文献标题排序 b.按出版年份排序 """ ref = """[1] Panos Achlioptas, Olga Diamanti, Ioannis Mitliagkas, and Leonidas Guibas. Learning representations and generative models for 3D point clouds. In Proc. ICML, 2018 [2] Pulkit Agrawal, Joao Carreira, and Jitendra Malik. Learning to see by moving. In Proc. ICCV, 2015 [3] Peter N. Belhumeur, David J. Kriegman, and Alan L. Yuille. The bas-relief ambiguity. IJCV, 1999 [4] Christoph Bregler, Aaron Hertzmann, and Henning Biermann. Recovering non-rigid 3D shape from image streams. In Proc. CVPR, 2000 [5] Angel X. Chang, Thomas Funkhouser, Leonidas Guibas. Shapenet: An information-rich 3d model reposi-tory. arXiv preprint arXiv:1512.03012, 2015 [6] Ching-Hang Chen, Ambrish Tyagi, Amit Agrawal, Dy-lan Drover, Rohith MV, Stefan Stojanov, and James M. Rehg. Unsupervised 3d pose estimation with geometric self-supervision. In Proc. CVPR, 2019""" ref_str = re.sub(r'\[([0-9]{1})\]', r'$[\1]', ref) # 添加分隔$ print(ref_str) #脚手架代码 ref_str_2 = re.sub(r'([a-zA-Z]{2})\.', r'\1.#', ref_str) # 添加分隔# print(ref_str_2) #脚手架代码 ref_str2 = ref_str_2.replace("\n", "") ref_list = ref_str2.split("$") print(ref_list) #脚手架代码 [提示: 排序可以采用内置函数sorted(),语法如下: sorted(iterable, /, *, key=None, reverse=False), 注意掌握形式参数中带“/”和“*”的用途]
时间: 2024-01-11 19:03:07 浏览: 223
代码注释如下:
```python
import re
# 定义参考文献字符串
ref = """[1] Panos Achlioptas, Olga Diamanti, Ioannis Mitliagkas, and Leonidas Guibas. Learning representations and generative models for 3D point clouds. In Proc. ICML, 2018
[2] Pulkit Agrawal, Joao Carreira, and Jitendra Malik. Learning to see by moving. In Proc. ICCV, 2015
[3] Peter N. Belhumeur, David J. Kriegman, and Alan L. Yuille. The bas-relief ambiguity. IJCV, 1999
[4] Christoph Bregler, Aaron Hertzmann, and Henning Biermann. Recovering non-rigid 3D shape from image streams. In Proc. CVPR, 2000
[5] Angel X. Chang, Thomas Funkhouser, Leonidas Guibas. Shapenet: An information-rich 3d model reposi-tory. arXiv preprint arXiv:1512.03012, 2015
[6] Ching-Hang Chen, Ambrish Tyagi, Amit Agrawal, Dy-lan Drover, Rohith MV, Stefan Stojanov, and James M. Rehg. Unsupervised 3d pose estimation with geometric self-supervision. In Proc. CVPR, 2019"""
# 用正则表达式将参考文献编号中的中括号[]改为$,以便后续分割
ref_str = re.sub(r'\[([0-9]{1})\]', r'$[\1]', ref)
# 用正则表达式将参考文献中的作者缩写后面的句号.改为#,以便后续分割
ref_str_2 = re.sub(r'([a-zA-Z]{2})\.', r'\1.#', ref_str)
# 去掉参考文献中的换行符
ref_str2 = ref_str_2.replace("\n", "")
# 用$分割参考文献字符串,将每个参考文献作为一个元素存入列表中
ref_list = ref_str2.split("$")
# 按参考文献标题排序
ref_list_title = sorted(ref_list, key=lambda x: x.split(". ")[-1])
# 打印按参考文献标题排序后的结果
print("按参考文献标题排序:")
for i in ref_list_title:
print(i)
# 按出版年份排序
ref_list_year = sorted(ref_list, key=lambda x: int(re.findall(r'\d{4}', x)[-1]))
# 打印按出版年份排序后的结果
print("按出版年份排序:")
for i in ref_list_year:
print(i)
```
阅读全文