python的新事物非常裸露。
我正在尝试找到一种方法来查找带有字符串和变量的列表是否是另一个列表的子集。请参阅以下的代码和结果
y = ['test_sam_20190624.csv', 'test_phil_20190624.csv', 'test_bill_20190624.csv', 'test_jess_20190624.csv', 'test_issy_20190624.csv', 'test_clinton_20190624.csv']
x = ['sam', 'jack', 'bill', 'rodry', 'clinton']
print('nFile list is ')
print(*y, sep="n")
print('nNeeded names are ')
print(*x, sep="n")
datetoday = '20190624'
incl = [p for p in x if 'test'+p+datetoday+'.csv' in y]
not_incl = [p for p in x if 'test'+p+datetoday+'.csv' not in y]
print("n Included")
print(*incl, sep="m")
print("n Not included")
print(*not_incl, sep="n")
和下面给出的输出:
File list is
test_sam_20190624.csv
test_phil_20190624.csv
test_bill_20190624.csv
test_jess_20190624.csv
test_issy_20190624.csv
test_clinton__20190624.csv
Needed names are
sam
jack
bill
rodry
clinton
Included
Not included
sam
jack
bill
rodry
clinton
Process finished with exit code 0
但是我希望incl = ['sam' 'bill 'clinton']
肯定是输出吗?输出为:
Included
sam
bill
clinton
Not included
jack
rodry
我要去哪里?也许在字符串的串联中?
您没有插入搜索中的完整字符串:
incl = [p for p in x if 'test'+'_'+p+'_'+datetoday+'.csv' in y]
not_incl = [p for p in x if 'test'+'_'+p+'_'+datetoday+'.csv' not in y]
您忘记了查找中的下划线。
[dkennetz@nodecn203 fun]$ python3.5 fun.py
File list is
test_sam_20190624.csv
test_phil_20190624.csv
test_bill_20190624.csv
test_jess_20190624.csv
test_issy_20190624.csv
test_clinton_20190624.csv
Needed names are
sam
jack
bill
rodry
clinton
Included
sammbillmclinton
Not included
jack
rodry
似乎您忘记了串联中的 _
s。
尝试更改:
incl = [p for p in x if 'test'+p+datetoday+'.csv' in y]
to
incl = [p for p in x if 'test_'+p+'_'+datetoday+'.csv']
not_incl相同:
not_incl = [p for p in x if 'test'+p+datetoday+'.csv' not in y]
应该是
not_incl = [p for p in x if 'test_'+p+'_'+datetoday+'.csv' not in y]
现在您应该获得想要的输出。
您在if语句中缺少下划线(_(。应该如下。
incl = [p for p in x if 'test_'+p+'_'+datetoday+'.csv' in y]
not_incl = [p for p in x if 'test_'+p+'_'+datetoday+'.csv' not in y]
正如所有先前的ANSER所建议的那样,缺少的是字符串中的下划线(_(。如果我们取出这些下划线,它将不会获取正确的字符串。
incl = [p for p in x if 'test_'+p+'_'+datetoday+'.csv' in y]
not_incl = [p for p in x if 'test_'+p+'_'+datetoday+'.csv' not in y]