Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在python中搜索2D列表中的引用_Python 3.x - Fatal编程技术网

Python 3.x 在python中搜索2D列表中的引用

Python 3.x 在python中搜索2D列表中的引用,python-3.x,Python 3.x,我有一个2D列表,我正在尝试查找列表中单词“the”的出现情况 我正在自学python并尝试以下代码 data = [["the cat is fed", "the bar is barred", "cat is now a bar"], ["the cat was fed", "the bar was barred", "cat was now a bar"]] whatIsTheSum = sum('the' in s for s in data) print(whatI

我有一个2D列表,我正在尝试查找列表中单词“the”的出现情况

我正在自学python并尝试以下代码

data = [["the cat is fed", "the bar is barred", "cat is now a bar"],
        ["the cat was fed", "the bar was barred", "cat was now a bar"]]

whatIsTheSum = sum('the' in s for s in data)

print(whatIsTheSum)

我希望结果为4,但程序返回0。

因为您没有迭代嵌套数组

whatIsTheSum = sum('the' in s for nested in data for s in nested)

因为您没有迭代嵌套数组

whatIsTheSum = sum('the' in s for nested in data for s in nested)

查找两个不同数组的元素。试试这个:

whatIsTheSum = sum('the' in s for s in data[0]+data[1])
print(whatIsTheSum)

查找两个不同数组的元素。试试这个:

whatIsTheSum = sum('the' in s for s in data[0]+data[1])
print(whatIsTheSum)

欢迎来到SO!如果其中一个字符串多次包含“the”,您的预期结果是什么<代码>在中只检查是否至少存在一个事件。欢迎使用SO!如果其中一个字符串多次包含“the”,您的预期结果是什么<在中的code>仅检查是否存在至少一个事件。