字符串、字符串列表,倒序生成字典。

156 篇文章 3 订阅

带数字的字符串以数字为key倒序生成字典,字符串列表按其元素索引为key倒序生成字典。


【学习的细节是欢悦的历程】


  自学并不是什么神秘的东西,一个人一辈子自学的时间总是比在学校学习的时间长,没有老师的时候总是比有老师的时候多。
            —— 华罗庚


等风来,不如追风去……


带数字的字符串以数字为key倒序生成字典
字符串按规则生成字典
(字符串列表按其元素索引为key倒序生成字典)


本文质量分:

91
本文地址: https://blog.csdn.net/m0_57158496/article/details/130809903

CSDN质量分查询入口:http://www.csdn.net/qc


目 录


◆ 字符串&字符串列表按规则生成字典


1、题目描述

(题目来源于 CSDN 问答社区提问“ Python 实现一个倒排索引”)。
在这里插入图片描述


  题目没有代码文本,我用我一个没写完的“工程”代码文本来试炼——



回页目录

2、解题思路

2.1 题目拆解


  列表字符串倒序索引为key生成字典,相对简单。题目没作要求,可先倒序列表,再以其长度n-其元素索引为key,字符串元素为value。方法多种,不一而足,可以拾取喜欢的搞豆是。😄关于倒序序列,我有做过学习笔记“Python列表(list)反序(降序)的7种实现方式”,可以点击蓝色文字跳转翻阅。
  带序号字符串,我以目前的Python功底,感觉有些棘手。能想到的方法就是用re提取数字,以数字为标识用str.split()遍历从后往前截取数字后接的字符串,用数字为key字符串为value依次追加到字典。由于是从后往前操作,其结果就是倒序。

  用我没完成的代码文本 file1.py 的内容,拆解题目,理清思路——


  • file1.py 内容
#!/sur/bin/nve python
# coding: utf-8
from re import findall
my_path = '/sdcard/Documents/'
names_text_line = open(f"{my_path}三国演义人物姓名列表.txt").readlines()
texts = open(f"{my_path}三国演义.txt").read()
names = []
name_sort = {}

for i in names_text_line:
    print(i)
    name = findall(r'([^A-Z][\u4E00-\u9FA5(,) \(\)]+)[、 \n]+', i)
    name = [i if '、' not in i else i.split('、') for i in name]
    
    input(f"Local_name: {name}")

for i in names:
    if len(i)==1:
        print(i)
input(999)

for k,name in enumerate(names):
    print(' '*50, end='\r')
    print(f"{' '*(k%39)}finding ...", end='\r')
    name_sort[name] = texts.count(name)

print(' '*50, end='\r')
print(f"\n曹操:{'曹操' in names},{name_sort.get('曹操', None)}")
names = [(name, times) for name,times in name_sort.items()]
names.sort(key=lambda x: x[1], reverse=True)

for i in range(20):
    print(f"{names[i][0]:>18}:{names[i][1]}")

print(len(names), len(name_sort), len(texts))

  • 由代码文本生成字符串和字符串列表

lis = open('/sdcard/qpython/file1.py').readlines()[:17] # 读取代码文本,以每行字符串为元素生成字符串列表。
mystr = ' '.join([f"{k}: {i[:-1]}" for k,i in enumerate(lis)]) # 将代码文本拼接成带行号的一个字符串。

2.2 代码运行效果截屏图片

在这里插入图片描述






回页目录

3、解题操作

3.1 字符串列表


  • 对于字符串列表,直接倒序解析就好

lis_dict = {str(i): lis[i] for i in range(len(lis)-1, -1, -1)} # 字典解析式生成结果字典。

3.2 带数字的代码文本


  • 提取字符串中的数字

mystr_indexs = findall(r'\d+: ', mystr) # re.findall() 方法提取行号列表。

  • 循环拆分追加到字典

for i in range(len(mystr_indexs)-1, -1, -1): # 循环倒序拆分字符串行并追加到字典中。
    temp = mystr2.split(f"{i}:")
    mystr_dict[f"{i}"] = temp[1]
    mystr2 = temp[0]



回页目录

4、完整源码

(源码较长,点此跳过源码)

#!sur/bin/nve python
# coding: utf-8
from re import findall

lis = open('/sdcard/qpython/file1.py').readlines()[:18] # 读取代码文本,以每行字符串为元素生成字符串列表。
mystr = ' '.join([f"{k}: {i[:-1]}" for k,i in enumerate(lis)]) # 将代码文本拼接成带行号的一个字符串。

mystr_indexs = findall(r'\d+: ', mystr) # re.findall() 方法提取行号列表。
mystr_dict = {} # 代码行字符串字典初值。
mystr2 = mystr

for i in range(len(mystr_indexs)-1, -1, -1): # 循环倒序拆分字符串行并追加到字典中。
    temp = mystr2.split(f"{i}:")
    mystr_dict[f"{i}"] = temp[1]
    mystr2 = temp[0]

print(f"\n字符串:\n'{mystr}'\n\n生成的字典:\n{mystr_dict}") # 结果输出。

lis_dict = {str(i): lis[i] for i in range(len(lis)-1, -1, -1)} # 字典解析式生成结果字典。
print(f"\n\n字符串列表:\n'{lis}'\n\n生成的字典:\n{lis_dict}") # 结果输出。


回页首

上一篇:  代码模拟春节“集五福”(根据近两年流行的春节集五福活动,编写一个模块,实现模拟春节集五福的过程)
下一篇: 

我的HOT博:

  本次共计收集 210 篇博文笔记信息,总阅读量 33.91w,平均阅读量 1614。已生成 22 篇阅读量不小于 3000 的博文笔记索引链接。数据采集于 2023-05-22 05:29:13 完成,用时 4 分 49.29 秒。


  1. 让QQ群昵称色变的神奇代码
    ( 54591 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122566500
    点赞:24   踩 :0  收藏:80  打赏:0  评论:17
    本篇博文笔记于 2022-01-18 19:15:08 首发,最晚于 2022-01-20 07:56:47 修改。
  2. ChatGPT国内镜像站初体验:聊天、Python代码生成等
    ( 52131 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/129035387
    点赞:123   踩 :0  收藏:788  打赏:0  评论:75
    本篇博文笔记于 2023-02-14 23:46:33 首发,最晚于 2023-03-22 00:03:44 修改。
  3. pandas 数据类型之 DataFrame
    ( 8313 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124525814
    点赞:6   踩 :0  收藏:25  打赏:0  评论:0
    本篇博文笔记于 2022-05-01 13:20:17 首发,最晚于 2022-05-08 08:46:13 修改。
  4. 罗马数字转换器|罗马数字生成器
    ( 6457 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122592047
    点赞:0   踩 :0  收藏:1  打赏:0  评论:0
    本篇博文笔记于 2022-01-19 23:26:42 首发,最晚于 2022-01-21 18:37:46 修改。
  5. Python字符串居中显示
    ( 6277 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122163023
    点赞:1   踩 :0  收藏:5  打赏:0  评论:1
    本篇博文笔记于 2021-12-26 23:35:29 发布。
  6. 个人信息提取(字符串)
    ( 5886 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124244618
    点赞:0   踩 :0  收藏:9  打赏:0  评论:0
    本篇博文笔记于 2022-04-18 11:07:12 首发,最晚于 2022-04-20 13:17:54 修改。
  7. 斐波那契数列的递归实现和for实现
    ( 5308 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122355295
    点赞:4   踩 :0  收藏:2  打赏:0  评论:8
    本篇博文笔记于 2022-01-06 23:27:40 发布。
  8. 练习:字符串统计(坑:f‘string‘报错)
    ( 4892 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121723096
    点赞:0   踩 :0  收藏:1  打赏:0  评论:0
    本篇博文笔记于 2021-12-04 22:54:29 发布。
  9. 练习:尼姆游戏(聪明版/傻瓜式•人机对战)
    ( 4632 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121645399
    点赞:14   踩 :0  收藏:42  打赏:0  评论:0
    本篇博文笔记于 2021-11-30 23:43:17 发布。
  10. 回车符、换行符和回车换行符
    ( 4427 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/123109488
    点赞:0   踩 :0  收藏:2  打赏:0  评论:0
    本篇博文笔记于 2022-02-24 13:10:02 首发,最晚于 2022-02-25 20:07:40 修改。
  11. python清屏
    ( 4406 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/120762101
    点赞:0   踩 :0  收藏:5  打赏:0  评论:0
    本篇博文笔记于 2021-10-14 13:47:21 发布。
  12. Python列表(list)反序(降序)的7种实现方式
    ( 4268 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/128271700
    点赞:4   踩 :0  收藏:14  打赏:0  评论:8
    本篇博文笔记于 2022-12-11 23:54:15 首发,最晚于 2023-03-20 18:13:55 修改。
  13. 密码强度检测器
    ( 3924 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121739694
    点赞:1   踩 :0  收藏:4  打赏:0  评论:0
    本篇博文笔记于 2021-12-06 09:08:25 首发,最晚于 2022-11-27 09:39:39 修改。
  14. 罗马数字转换器(用罗马数字构造元素的值取模实现)
    ( 3840 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122608526
    点赞:0   踩 :0  收藏:0  打赏:0  评论:0
    本篇博文笔记于 2022-01-20 19:38:12 首发,最晚于 2022-01-21 18:32:02 修改。
  15. 练习:生成100个随机正整数
    ( 3740 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122558220
    点赞:1   踩 :0  收藏:4  打赏:0  评论:0
    本篇博文笔记于 2022-01-18 13:31:36 首发,最晚于 2022-01-20 07:58:12 修改。
  16. 练习:班里有人和我同生日难吗?(概率probability、蒙特卡洛随机模拟法)
    ( 3588 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124424935
    点赞:1   踩 :0  收藏:2  打赏:0  评论:0
    本篇博文笔记于 2022-04-26 12:46:25 首发,最晚于 2022-04-27 21:22:07 修改。
  17. 我的 Python.color() (Python 色彩打印控制)
    ( 3470 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/123194259
    点赞:2   踩 :0  收藏:7  打赏:0  评论:0
    本篇博文笔记于 2022-02-28 22:46:21 首发,最晚于 2022-03-03 10:30:03 修改。
  18. 练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。
    ( 3258 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/125415626
    点赞:3   踩 :0  收藏:4  打赏:0  评论:3
    本篇博文笔记于 2022-06-22 19:54:20 首发,最晚于 2022-06-23 22:41:33 修改。
  19. 聊天消息敏感词屏蔽系统(字符串替换 str.replace(str1, *) )
    ( 3125 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124539589
    点赞:3   踩 :0  收藏:2  打赏:0  评论:3
    本篇博文笔记于 2022-05-02 13:02:39 首发,最晚于 2022-05-21 06:10:42 修改。
  20. Linux 脚本文件第一行的特殊注释符(井号和感叹号组合)的含义
    ( 3089 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/123087606
    点赞:0   踩 :0  收藏:4  打赏:0  评论:3
    本篇博文笔记于 2022-02-23 13:08:07 首发,最晚于 2022-04-04 23:52:38 修改。
  21. 练习:求列表(整数列表)平衡点
    ( 3034 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121737612
    点赞:0   踩 :0  收藏:0  打赏:0  评论:0
    本篇博文笔记于 2021-12-05 23:28:10 发布。
  22. random.sample()将在python 3.9x后续版本中被弃用
    ( 3000 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/120657230
    点赞:0   踩 :0  收藏:0  打赏:0  评论:0
    本篇博文笔记于 2021-10-08 18:35:09 发布。
推荐条件 阅读量突破三千
(更多热博,请点击蓝色文字跳转翻阅)

回页首


老齐漫画头像

精品文章:

来源:老齐教室


回页首

Python 入门指南【Python 3.6.3】


好文力荐:


CSDN实用技巧博文:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦幻精灵_cq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值