单词[数]=星星是什么意思

2024-03-29 15:11:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个初学者,我在Codecademy上做了一个练习,解决方法是:

    def censor(text, word):

       words = text.split()
       result = ''
       stars = '*' * len(word)
       count = 0
       for i in words:
         if i == word:
            words[count] = stars
         count += 1
       result =' '.join(words)

       return result

所以我的问题是第8行的单词[count]=stars是什么意思?你知道吗


Tags: 方法textinforlendefcountresult
2条回答

它将解引用局部变量stars的结果指定为在通过解引用局部变量count获得的索引处由局部变量words引用的列表的元素。你知道吗

words[count] = stars行是一个赋值。你知道吗

在等号的右边,你可以找到你想要分配给某个东西的值。在本例中,它是******形式的字符串或字符序列。你知道吗

等号左边是赋值的目标。它是您要存储字符序列的地方。在这种情况下,目标是列表words中的一个位置。位置由count指定。你知道吗

因此,如果你有当前的状态

words = ['Hello', 'World']
count = 1
stars = '*****'

那么第8行中的赋值将导致以下状态:

words = ['Hello', '*****']

它已将stars的新值*****赋给列表words中的count位置,并将其替换为World。你知道吗

相关问题 更多 >