简体   繁体   English

如何在 Python 中使用另一个 Class 中的字典进行循环

[英]How Do I For loop With A Dict in Python That Is In Another Class

For example if I were using a list i would say例如,如果我使用的是列表,我会说

for i in taskList:
   Print('item')

I was wondering how to do this with a dict also this dict is in another class.我想知道如何用一个字典来做到这一点,这个字典也在另一个 class 中。 I have a class set up for the user to input tasks which adds it to a dict (this works) however now I want another class that pulls from the dict in the first class.我为用户设置了一个 class 来输入将其添加到字典中的任务(这可行)但是现在我想要另一个 class 从第一个 class 中的字典中提取。 is this possible?这可能吗? Here is a short example of what I've tried这是我尝试过的一个简短示例

class TaskList():
   itemsToDoDic = {'Cleaning' : 0, 'Cook' :0}


classItems = TaskList()

class Importance():
   classItems.itemsToDoDic
   def levelOfImportance(self):
      for i in self.ClassItems.itemsToDoDic:
         importanceValue = int(input('Enter a 
         number 1 - 5')

I got the following errors:我收到以下错误:

  1. Instance of 'Importance' has no 'ClassItems' member “Importance”实例没有“ClassItems”成员
  2. Unused variable 'i'未使用的变量“我”
  3. Unused variable 'importanceValue'未使用的变量“importanceValue”

Say you have two classes, needless to say, that if your source class (the one you get the dict from) is in another file, you will have to import it.假设你有两个类,不用说,如果你的源 class (你从中获取字典的那个)在另一个文件中,你将不得不导入它。

Check this code:检查此代码:

# this is the source class
class DictContainingClass():
  itemsToDoDic = {'Cleaning' : 0, 'Cook' :0}

# creating an instance of the source class
instance = DictContainingClass()

# this is the taking class
class DictNeedingClass():

  
  # creating your class method:
  def myClassMethod(self):

    # accessing the dict inside it
    getDict = instance.itemsToDoDic
  
    # now looping through its items
    for item in getDict.items():
      print(item)
    # or if you want to access only the keys
    for key in getDict.keys():
      print(key)
    # or if you want to access only values
    for value in getDict.values():
      print(value)

# calling for your method
DictNeedingClass().myClassMethod()

Hope this answered your question, please refer to this dictionary methods page for more information.希望这回答了您的问题,请参阅此字典方法页面以获取更多信息。

Here is the solution to your error.这是您的错误的解决方案。

Here in for loop for i in self.ClassItems.itemsToDoDic: you are calling self.ClassItems.itemsToDoDic which is not defined in Importance class rather defined as a global variable. for i in self.ClassItems.itemsToDoDic:您正在调用self.ClassItems.itemsToDoDic ,这未在Importance class 中定义,而是定义为全局变量。

class TaskList():
   itemsToDoDic = {'Cleaning' : 0, 'Cook' :0}

classItems = TaskList()

class Importance():
    def levelOfImportance(self):
        for key,value in classItems.itemsToDoDic.items():
            print(key,value)
            #Rest of your code here

#testing
Importance().levelOfImportance() 

Check this page if you want to know more about for loop over the dictionary variable.如果您想了解更多关于字典变量的 for 循环,请查看此页面。 https://www.geeksforgeeks.org/iterate-over-a-dictionary-in-python/ https://www.geeksforgeeks.org/iterate-over-a-dictionary-in-python/

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

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