对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
我哪里出错了?也许是串在一起?你知道吗
if语句中缺少下划线(\)。应该是这样的。你知道吗
搜索中没有包含完整字符串:
您在查找中忘记了下划线。你知道吗
似乎您忘记了连接中的
_
。你知道吗尝试更改:
至
相同,不包括:
应该是
现在你应该得到想要的输出了。你知道吗
相关问题 更多 >
编程相关推荐