Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.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/2/node.js/37.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中将列表传递给函数时,如何使其不区分大小写_Python - Fatal编程技术网

在Python中将列表传递给函数时,如何使其不区分大小写

在Python中将列表传递给函数时,如何使其不区分大小写,python,Python,现在开发一个名为count_this_word_in_all_tweets的函数,该函数将tweets列表和单词作为参数,它将查找该单词在列表中所有tweets中出现的次数。函数应该使用前面定义的列表理解和函数count_this_word_in_this_tweet来完成任务 所以我在tweet函数中使用了这个作为我的count\u this\u word\u,它的工作原理也和它的假设一样 def count_this_word_in_this_tweet(data, word): d

现在开发一个名为count_this_word_in_all_tweets的函数,该函数将tweets列表和单词作为参数,它将查找该单词在列表中所有tweets中出现的次数。函数应该使用前面定义的列表理解和函数count_this_word_in_this_tweet来完成任务

所以我在tweet函数中使用了这个作为我的count\u this\u word\u,它的工作原理也和它的假设一样

def count_this_word_in_this_tweet(data, word):
    data.islower()
    word.islower()
    splitwords = data.split()
    count = splitwords.count(word)
    return count
我的另一个功能是

def count_this_word_in_all_tweets(data, word):
    count = [count_this_word_in_this_tweet(s,word) for s in  data]
    total = sum(count)
    return total
但第二个不是不区分大小写的。我试着搜索电影,我得到140个结果,我搜索电影,我得到14个结果。我不确定为什么第二个函数不区分大小写,因为它调用的是一个不区分大小写的函数

任何帮助都将不胜感激。

islower()
仅检查字符串的值是否全部为小写

伊斯洛街()

如果字符串中的所有大小写字符4都是小写且至少有一个大小写字符,则返回True,否则返回False

您正在寻找
lower()

下大街()

返回字符串的副本,其中所有大小写字符4都转换为小写

Unicode标准第3.13节描述了使用的小写算法

您需要将数据设置为
lower()

基于你的例子

data = ['DawnofthePlanetoftheApes - Movie Reviews, Movie Rating, Trailers, Posters | MovieMagik http:t.co/lYSGCB8jXR via @moviemagikin Saw the new Planet of the Apes. Best one since the original with way better effects! #DawnofthePlanetoftheApes I could say Im watching Planet of the Apes (1968) as a countdown to #DawnofthePlanetoftheApes - but really, I could watch it any time.']

word = 'movie'

def count_me(data, word):
    data = data.lower()
    word = word.lower()
    splitwords = data.split()
    count = splitwords.count(word)
    return count

def lets_roll(data, word):
    count = [count_me(s, word) for s in data]
    total = sum(count)
    return total

>>> lets_roll(data, word)
#2


好的,但即使我将tweet(data,word)中的def count\u this\u word\u更改为.lower,也无法解决问题。我是否也必须将另一个列表中的列表和单词更改为lower?能否提供一个示例,说明您对
数据
单词
的输入?数据是一个已编辑的推文列表。这个词就是电影。如果数据是一个列表,我的print语句就是print(在所有tweets(tweets[:],“Movie”)(tweets[:],“Movie”)中计算这个单词。试试这个,
data=[word.lower()表示数据中的word]
如果我在第二个函数中这样做的话,它确实有效。第一次我不能这样做,因为我正在分割数据
data = data.lower()
word = word.lower()
data = ['DawnofthePlanetoftheApes - Movie Reviews, Movie Rating, Trailers, Posters | MovieMagik http:t.co/lYSGCB8jXR via @moviemagikin Saw the new Planet of the Apes. Best one since the original with way better effects! #DawnofthePlanetoftheApes I could say Im watching Planet of the Apes (1968) as a countdown to #DawnofthePlanetoftheApes - but really, I could watch it any time.']

word = 'movie'

def count_me(data, word):
    data = data.lower()
    word = word.lower()
    splitwords = data.split()
    count = splitwords.count(word)
    return count

def lets_roll(data, word):
    count = [count_me(s, word) for s in data]
    total = sum(count)
    return total

>>> lets_roll(data, word)
#2