-1

我要疯了吗?

在我的测试中,如果字符串在列表 [0] 中,python list.index('') 无法在列表中找到字符串!!!

为什么?我怎么找到它?

这是我的示例代码:

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]

这给了我以下结果:

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?
I did not find 1 in list 2 even thought it is definitely there (twice infact)... WTF?
I can find it like this but I want to search by string >>> 1
Or like this like this but I want to search by string >>> 1

我想我一定遗漏了一些非常明显的东西......但无法弄清楚或找到答案......请通过搜索字符串帮助我在 list1 中找到“WTF”或在 list2 中找到“1”.....

4

1 回答 1

3

当您这样做时if list1.index('1'),您正在测试该列表中的索引是否'1'为布尔真。它的索引为零,这是布尔假。所以你的 if 块不会运行。

如果您想知道它是否在其中,只需执行if '1' in list1.

于 2013-05-10T07:32:13.383 回答