鱼C论坛

 找回密码
 立即注册
查看: 3311|回复: 4

[已解决]如果输出判断类型

[复制链接]
发表于 2021-9-13 13:21:44 | 显示全部楼层
本帖最后由 傻眼貓咪 于 2021-9-13 17:06 编辑
你的 alpha = 0,space = 0,digit = 0,others = 0 必須放在 for 循環外面,不然每次更新結果後又恢復 0

我的代碼:(供參考)
  1. def strInfo(*strs: str) -> dict():
  2.     res = {}.fromkeys(['alpha', 'space', 'digit', 'others'], 0) # 創建字典,增加輸出可讀性
  3.     for char in ''.join(strs): # 直接遍歷,無需再創建新列表
  4.         if char.isalpha(): res['alpha'] += 1
  5.         elif char.isdigit(): res['digit'] += 1
  6.         elif char == ' ': res['space'] += 1
  7.         else: res['others'] += 1
  8.     return res.items() # 返回字典裡的元素


  9. print(*strInfo('6576455','asd aeedf sdfs')) # 打印
复制代码
  1. ('alpha', 12) ('space', 2) ('digit', 7) ('others', 0)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-4-2 01:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表