简体   繁体   English

局部变量“ item”值未使用“未使用的范围”-Python

[英]Local variable “item' value not used ”unused scope" - Python

Trying to iterate through a list: ['AEM', 'ATD.B', 'ARX', 'BMO', 'BNS'...] Checking for "." 尝试遍历以下列表: ['AEM', 'ATD.B', 'ARX', 'BMO', 'BNS'...]检查“。” (periods) and want to replace them with "-" (dashed lines) (句号),并希望将其替换为“-”(虚线)

Code: 码:

            for ticker in sptsx60:
                if '.' in ticker:
                    ticker = ticker.replace(".", "-")
                else:
                    pass
            print(sptsx60)

The third line ticker = ticker.replace(".", "-") reads a tooltip "This inspection highlights local variables,parameters or local functions unused in scope" in my PyCharm IDE. 第三行ticker = ticker.replace(".", "-")在我的PyCharm IDE中读取工具提示“此检查突出显示了作用域中未使用的局部变量,参数或局部函数”。

The printed list remains unchanged after the if statement. if语句后,打印列表保持不变。 Was wondering if anyone had more insight regarding the tooltip and how to fix the code. 想知道是否有人对工具提示以及如何修复代码有更深入的了解。 Thanks :) 谢谢 :)

Using a list comprehension. 使用列表理解。

Ex: 例如:

sptsx60 = ['AEM', 'ATD.B', 'ARX', 'BMO', 'BNS']
sptsx60 = [ticker.replace(".", "-") if '.' in ticker else ticker for ticker in sptsx60 ]

print(sptsx60 )

Output: 输出:

['AEM', 'ATD-B', 'ARX', 'BMO', 'BNS']
  • Note: It is not a good idea to modify an element in a list while iterating over it. 注意:在迭代列表时修改列表中的元素不是一个好主意。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM