在python列表中查找变量

时间:2015-03-02 20:42:08

标签: python

我试图在函数返回的列表中搜索变量

        hid = '282'
if __name__ == '__main__':


    filename = "foo.txt"

    z = get_history_ids2(sid)
    with open(filename) as f:
        data = f.readlines()

    for r in z: 
        if int('4216') in data is False:
            print 'lose'
        else:
            print 'win'
    print data


    print (hid)
    print z

我变得虚假......或者换句话说它看不到数字“282”,尽管它确实存在于列表中

  

[281,282,283,284,285,286,294,303,304,305,306,307,308,309,310,312,313,314,318,319,320,321,322, 323,414,415,416,417,418]

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您的程序正确报告字符串'282'不在列表中。你可以在同一个列表中找到int 282:

In [1]: x = [281, 282, 283, 284, 285, 286, 294, 303, 304, 305, 306, 307, 308, 309, 310, 312, 313, 314, 318, 319, 320, 321, 322, 323, 414, 415, 416, 417, 418]

In [2]: '282' in x
Out[2]: False

In [3]: 282 in x
Out[3]: True

In [4]: int('282') in x
Out[4]: True