簡體   English   中英

遍歷類中的列表

[英]Iterate over a list in a class

我正在嘗試遍歷類中的列表,但僅將列表的第一個成員打印到控制台。 如何打印每個元素?

class CodeManager(object):
    """Separates the input string into individual characters in a list"""

    characters = []

    def __init__(self, stringCode):
        self.stringCode = stringCode

    def LoopThroughList(self):

        self.characters = list(self.stringCode.upper())
        for chars in self.characters:
            return chars

然后在主Python文件中創建一個類對象:

code = CodeManager.CodeManager("Hello my name is Callum")
print (code.LoopThroughList())

您將在第一次迭代后返回:

for chars in self.characters:
        return chars # ends loop

如果要查看所有字符,請使用print或yield並遍歷code.LoopThroughList()

for chars in self.characters:
        print(chars)

yield並使該方法成為生成器

 for chars in self.characters:
        yield chars

然后:

for ch in code.LoopThroughList():
    print(ch)

實際上,使用yield可以使您使用返回的每個字符,這可能更接近您在自己的代碼中嘗試執行的操作。

如果只想查看換行符上輸出的每個字符,則可以使用str.join:

self.characters = list(self.stringCode.upper())
        return "\n".join(self.characters)

您也不需要在self.stringCode.upper()上調用list,可以直接在字符串上進行迭代。

您的循環return第一個字符。 return語句將阻止循環其余迭代的執行。

您可能想使用print而不是return

def loop_through_list(self):
    self.characters = list(self.stringCode.upper())
    for char in self.characters:
        print(char)

用作:

code = CodeManager.CodeManager("Hello my name is Callum")
code.loop_through_list()

而且,您在classcharacters = []定義非常沒用。 您在方法調用中隱藏了class屬性。

您要返回,因此是列表中的第一個元素。

class CodeManager(object):
    """Separates the input string into individual characters in a list"""

    characters = []

    def __init__(self, stringCode):
        self.stringCode = stringCode

    def LoopThroughList(self):
        self.characters = list(self.stringCode.upper())
        for chars in self.characters:
            print chars

code = CodeManager("Hello my name is Callum")
code.LoopThroughList()

您可以使用列表理解

def LoopThroughList(self):
    return [chars for chars in self.stringCode.upper()]

這將返回stringCode變量中所有字符的列表

是啊。 列表理解僅需稍作修改即可:

class CodeManager(object):
"""Separates the input string into individual characters in a list"""

    characters = []

    def __init__(self, stringCode):
        self.stringCode = stringCode
        self.characters = stringCode.upper()

    def LoopThroughList(self):
        return [chars for chars in self.stringCode.upper().split()]


code = CodeManager("Hello my name is Callum")
print (code.LoopThroughList())

[ 'HELLO', 'MY', 'NAME', 'IS', 'CALUM']

或者,如果您想要這樣:

class CodeManager(object):

characters = []

def __init__(self, stringCode):
    self.stringCode = stringCode
    self.characters = stringCode.upper()

def LoopThroughList(self):
    return [chars for chars in self.stringCode.upper()]


code = CodeManager("Hello my name is Callum")
print (code.LoopThroughList())

['你好我的名字' ,“”,“我”,“ S”,“”,“ C”,“ A”,“ L”,“ L”,“ U”,“ M”]

您的角色變量是類變量,而不是實例變量(有區別)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM