所以这里我有两个列表
cons=['qwe','wer','ert','rty','tyu','yui','uio','iop','asd','sdf','dfg',
'fgh','ghj','hjk','jkl','zxc','xcv','cvb','vbn','bnm']
print([i for e in alphabet for i in cons if e in i])
字母表是用户输入。
例如,如果用户要键入qwe,则shell将打印'qwe','wer','ert','rty'
(所有至少有一个字母相似的项),但如果输入的所有项都与此类似,则只希望它从con打印项目。例如打印“qwe”
我该怎么做?
编辑:为了更清楚地说明这一点,如果我要输入“qwer”,我希望输出为“qwe”和“wer”。
发布于 2017-10-04 06:40:36
尝试使用set
cons=['qwe','wer','ert','rty','tyu','yui','uio','iop','asd','sdf','dfg',
'fgh','ghj','hjk','jkl','zxc','xcv','cvb','vbn','bnm']
alphabet = "weq"
print([c for c in cons if set(alphabet).issubset(set(c))]) # output will be ['qwe']
https://stackoverflow.com/questions/46558153
复制相似问题