我疯了吗?在
在我的测试中python列表.索引如果字符串在列表[0]中,('')在列表中找不到字符串!!!在
为什么?我怎么找到它?在
下面是我的示例代码:
list1 = ['WTF', '2.09', '\xc2\x80document.write(CurrencyFormat(Currency.convert((209/100),"GBP","EUR")));','0.00', 'Feminised', '6.88', '\xc2\x80document.write(CurrencyFormat(Currency.convert((688/100),"GBP","EUR")));', 'Regular', 'x10', '20.90', '\xc2\x80document.write(CurrencyFormat(Currency.convert((2090/100),"GBP","EUR")));', 'Feminised', 'x12', '82.56', '\xc2\x80document.write(CurrencyFormat(Currency.convert((8256/100),"GBP","EUR")));']
list2 = ['1','2','3','4','5', '1']
if list1.index('0.00'):
print "I found 0.00 in list 1 but if its in position[0], I cannot find it using index('0.00') - even it appears twice what gives?"
if list2.index('1'):
print 'weird'
else:
print 'I did not find 1 in list 2 even thought it is definitely there (twice infact)... WTF?'
print 'I can find it like this but I want to search by string >>> ' + list2[0]
print 'Or like this like this but I want to search by string >>> ' + list2[-1]
结果如下:
^{pr2}$我想我一定错过了一些很明显的东西。。。但无法找到答案。。。请帮助我通过搜索字符串在列表1中查找“WTF”或在列表2中查找“1”。。。。。在
当您执行
if list1.index('1')
操作时,您正在测试该列表中'1'
的索引是否为布尔真。它的索引为零,这是布尔值false。所以你的if块不运行。在如果你想知道它是否在里面,只要做
if '1' in list1
。在相关问题 更多 >
编程相关推荐