Python 3.x 在python中搜索2D列表中的引用
我有一个2D列表,我正在尝试查找列表中单词“the”的出现情况 我正在自学python并尝试以下代码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
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>仅检查是否存在至少一个事件。