鱼C论坛

 找回密码
 立即注册
查看: 2871|回复: 2

[已解决]求助大佬

[复制链接]
发表于 2023-1-8 16:19:42 | 显示全部楼层 |阅读模式

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

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

x
红鲤鱼与绿鲤鱼与驴,到底几条鱼几头驴?计算出列表word = ["红","鲤","鱼","与","绿","鲤","鱼","与","驴"]有几条鱼几头驴。

新建一个字典输出结果,格式为:{'鱼': 2, '驴': 1}。

# 定义红鲤鱼与绿鲤鱼与驴列表
word = ["红","鲤","鱼","与","绿","鲤","鱼","与","驴"]
dic={}
num1 = 0
num2 = 0
item1 ="鱼"
item2 ="驴"
for item1 in word:
    num1 = num1 + item1
   
for item2 in word:
    num2 = num2 + item2

print(dic)
请问如果按照我这种思路写
应该怎么补全
最佳答案
2023-1-8 16:59:32
你写的不对没法通过补全实现

改动最少的写法
word = ["红","鲤","鱼","与","绿","鲤","鱼","与","驴"]
dic={}
num1 = 0
num2 = 0
item1 ="鱼"
item2 ="驴"

for item in word:
    if item == item1:
        num1 = num1 + 1
dic[item1] = num1

for item in word:
    if item == item2:
        num2 = num2 + 1
dic[item2] = num2

print(dic)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-1-8 16:59:32 | 显示全部楼层    本楼为最佳答案   
你写的不对没法通过补全实现

改动最少的写法
word = ["红","鲤","鱼","与","绿","鲤","鱼","与","驴"]
dic={}
num1 = 0
num2 = 0
item1 ="鱼"
item2 ="驴"

for item in word:
    if item == item1:
        num1 = num1 + 1
dic[item1] = num1

for item in word:
    if item == item2:
        num2 = num2 + 1
dic[item2] = num2

print(dic)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-8 17:01:01 | 显示全部楼层
word = ["红","鲤","鱼","与","绿","鲤","鱼","与","驴"]
dic={}
num1 = 0
num2 = 0
item1 ="鱼"
item2 ="驴"
for item in word:
    if item==item1:
        num1+=1
    elif item==item2:
        num2+=1
dic={item1:num1, item2:num2}
print(dic)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-16 03:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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