简体   繁体   English

Python:即使它肯定存在,也不会在列表中找到字符串

[英]Python: will not find a string in a list even though it definitely is there

This should be such a simple issue but it's driving me nuts.这应该是一个如此简单的问题,但它让我发疯。 When I search for the keyword 'gift' in a list of strings, python will find it as long as I manually set the string 'gift'.当我在字符串列表中搜索关键字'gift'时,只要我手动设置字符串'gift',python就会找到它。 When I derive the string from a dictionary key, python will not find it in the list in question even though the dict key is an identical string object with no typos.当我从字典键派生字符串时,即使字典键是没有拼写错误的相同字符串对象,python 也不会在相关列表中找到它。 Here's the exact code:这是确切的代码:

This WORKS:这有效:

subtopic_keys = list(copy.deepcopy(subtopic_map).keys())

for element in subtopic_keys: 
    try:
        for dict_object in subtopic_map[element]:
            for key2 in dict_object.keys():
                for index, entry in enumerate(dict_object[key2]['tweets']):
                    count = 0
                    if 'gift' in clean(entry).split():
                        pass
                    if 'gift' not in clean(entry).split():
                        dict_object[key2]['tweets'][index] = 'removed'
    except KeyError:
        pass

This does NOT work.这不起作用。 Note: the only change is 'gift' has been replaced by element from the first for loop, which is an identical string object.注意:唯一的变化是 'gift' 已被第一个 for 循环中的元素替换,这是一个相同的字符串对象。 I verified this by printing type(element) and it is of the string class.我通过打印 type(element) 验证了这一点,它是字符串类。

subtopic_keys = list(copy.deepcopy(subtopic_map).keys())

for element in subtopic_keys: 
    try:
        for dict_object in subtopic_map[element]:
            for key2 in dict_object.keys():
                for index, entry in enumerate(dict_object[key2]['tweets']):
                    count = 0
                    if element in clean(entry).split():
                        pass
                    if element not in clean(entry).split():
                        dict_object[key2]['tweets'][index] = 'removed'
    except KeyError:
        pass

The last bit of code replaces every entry with 'removed', implying python does not recognize the string in any entry, so long as it's derived from a dict key.代码的最后一点用“removed”替换每个条目,这意味着 python 不能识别任何条目中的字符串,只要它是从 dict 键派生的。 Why would that be the case?为什么会是这样? The dict key is an identical string class object. dict 键是一个相同的字符串类对象。

I had the same problem.我有同样的问题。 Turns out my list had "\\n" at the end of all the element strings.原来我的列表在所有元素字符串的末尾都有“\\n”。 This is how i worked it out.我就是这样解决的。

openList=open("list.txt","r")
list = b.readlines() # this returned \n in each element of the list
list2=[x.replace("\n","") for x in list] #this removes all the \n in all elements in list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 venv 找不到 python 库,即使我确实安装了它 - venv cannot find python library even though I definitely have it installed Pandas 在 Visual Studio 代码中找不到,即使它肯定在环境中 - Pandas not found in Visual Studio Code even though it is definitely in the env KeyError虽然肯定在词典中 - KeyError though definitely in the Dictionary Python 找不到 LibTIFF 库,即使已经安装了 LibTIFF - Python cannot find LibTIFF library, even though LibTIFF is already installed 即使正确安装,Python3仍找不到模块 - Python3 won't find a module even though it is properly installed 即使安装了Python,也找不到PyGObject / gi - Python can't find PyGObject/gi even though it is installed 即使导入成功,Python也找不到类 - Python can't find class even though the import was successful 为什么 python tkinter 模块即使有文件也找不到文件? - why is python tkinter module unable to find file even though there is one? 即使打印功能正在打印列表,Python中也会读取文件错误 - Read file error in Python, even though print function is printing the list Python:即使列表中的项目存在,如果列表中的项目仍为False - Python: if item in list evaluates False even though the item exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM