作者safyrose (三十飞)
看板Python
标题Re: [问题] Python新手 for回圈问题
时间Thu Jun 13 00:04:05 2019
※ 引述《a172545056 (Leon)》之铭言:
: 各位前辈大家好,我是刚接触python不久的新人,目前练习到for回圈时有点卡关,想请
: 教一下各位前辈
: 我有三个List
: ListA=[“Apple”,“food”,“Iron”]
: ListB=[“x”,“z”,“on”]
: ListC=[]
: 今天我想知道ListA中的字串是否有包含ListB的字串,有的话ListC.append(“YES”),没
: 有的话ListC.append(“No”),
: 我想得到的结果是ListC[“No”,“No”,“Yes”]
: 小弟目前的做法是
: for a_str in ListA:
: for b_str in ListB:
: if b_str in a_str:
: ListC.append(“Yes”)
: continue
: else:
: ListC.append(“No”)
: 这样子ListC就增加了很多“No”,
: 跟我想要的结果不太一样,
: 想了很久不知道该怎麽做
: 还请各位前辈指点一下,谢谢!
帮你做个小修改就搞定了
for a_str in ListA:
xxx = 'No'
for b_str in ListB:
if b_str in a_str:
xxx = 'yes'
continue
ListC.append(xxx)
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.133.128.12 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1560355447.A.A62.html
1F:推 TuCH: continue 应该换成break 06/13 00:34