Re: [問題] Python新手 for迴圈問題
※ 引述《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://www.ptt.cc/bbs/Python/M.1560355447.A.A62.html
推
06/13 00:34,
5年前
, 1F
06/13 00:34, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 4 之 5 篇):