鱼C论坛

 找回密码
 立即注册
查看: 2541|回复: 3

[已解决]如何将列表的元素挨个输入到函数中?

[复制链接]
发表于 2020-2-20 12:41:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#零基础学python第19课的作业
#定义函数
def count(*str1):
    "检索字符串各种字符的数量"
    times = 0
    for i in str1:
        eng,num,space,other = 0,0,0,0
        for k in i:
            if k.isalpha():
                eng += 1
            elif k.isdecimal():
                num += 1
            elif k == " ":
                space += 1
            else:
                other += 1
        times += 1
        print('第%d个字符串共有:英文字母%d个,数字%d个,空格%d个,其他字符%d个。' % (times,eng,num,space,other))
#运行
temp = 0
list1 = []
while temp != "over":
    temp = input('请输入要检索的字符串(输入over结束):')
    if temp != 'over':
        list1.append(temp)
count((a for a in list1))




我估计最后一句是不对的?请问如何书写?
最佳答案
2020-2-20 12:42:44
用星号解包。

  1. #零基础学python第19课的作业
  2. #定义函数
  3. def count(*str1):
  4.     "检索字符串各种字符的数量"
  5.     times = 0
  6.     for i in str1:
  7.         eng,num,space,other = 0,0,0,0
  8.         for k in i:
  9.             if k.isalpha():
  10.                 eng += 1
  11.             elif k.isdecimal():
  12.                 num += 1
  13.             elif k == " ":
  14.                 space += 1
  15.             else:
  16.                 other += 1
  17.         times += 1
  18.         print('第%d个字符串共有:英文字母%d个,数字%d个,空格%d个,其他字符%d个。' % (times,eng,num,space,other))
  19. #运行
  20. temp = 0
  21. list1 = []
  22. while temp != "over":
  23.     temp = input('请输入要检索的字符串(输入over结束):')
  24.     if temp != 'over':
  25.         list1.append(temp)
  26. count(*list1)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-20 12:42:19 | 显示全部楼层
萌新第一次发帖,如果违规请马上删除!!!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-20 12:42:44 | 显示全部楼层    本楼为最佳答案   
用星号解包。

  1. #零基础学python第19课的作业
  2. #定义函数
  3. def count(*str1):
  4.     "检索字符串各种字符的数量"
  5.     times = 0
  6.     for i in str1:
  7.         eng,num,space,other = 0,0,0,0
  8.         for k in i:
  9.             if k.isalpha():
  10.                 eng += 1
  11.             elif k.isdecimal():
  12.                 num += 1
  13.             elif k == " ":
  14.                 space += 1
  15.             else:
  16.                 other += 1
  17.         times += 1
  18.         print('第%d个字符串共有:英文字母%d个,数字%d个,空格%d个,其他字符%d个。' % (times,eng,num,space,other))
  19. #运行
  20. temp = 0
  21. list1 = []
  22. while temp != "over":
  23.     temp = input('请输入要检索的字符串(输入over结束):')
  24.     if temp != 'over':
  25.         list1.append(temp)
  26. count(*list1)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-20 12:54:59 | 显示全部楼层

这也太迅速了!!~!~!~!!!谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-2 15:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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