简体   繁体   English

在函数之间传递具有其他值格式的字典

[英]Passing dictionary with other values formatted between functions

I want to pass the entire dictionaries's key, 2 more values which are being called in a loop hence will be combined and printed line by line. 我想传递整个字典的键,另外2个在循环中被调用的值将被合并并逐行打印。 Values are being passed to another function but only 1st value of the entire dictionary is passed. 值将传递给另一个函数,但仅传递整个字典的第一个值。 I want all values get passed as shown below along with other variables. 我希望所有值连同其他变量一起传递,如下所示。 What would I do so that all values are passed not just one. 我该怎么做,以使所有值都传递而不仅仅是一个。

Here is my code: 这是我的代码:

class scoring:

   def values(self):
      dicts = {}
      inner_dict = {}
      with open("1.txt","r") as f:
         for line, score in zip(sys.stdin, f):
            d2  = ast.literal_eval(score)
            for key,v in d2.items():
               inner_dict = dicts.setdefault(key, {})
               inner_dict['s'] = v.get('s')
               sent = dicts[key]['s']
               binary = re.split("\s+", line.strip())[0]
               return key, sent, binary

   def score(self,key,sent,binary):
      <something>

if __name__ == "__main__":

   call = scoring()
   key,sent,binary = call.values()
   call.score(key,sent,binary)

I have not done real coding in python, but I guess you are returning from the first iteration of the inner loop, probably that's why the other values never gets returned. 我还没有在python中进行真正的编码,但是我想您是从内部循环的第一次迭代返回的,这也许就是为什么其他值从未返回的原因。 You can save all your values list, and then can return list and access values from the list. 您可以保存所有值列表,然后可以返回列表并从列表中访问值。

class scoring:

   def values(self):
      dicts = {}
      list = []
      inner_dict = {}
      with open("1.txt","r") as f:
         for line, score in zip(sys.stdin, f):
            d2  = ast.literal_eval(score)
            for key,v in d2.items():
               inner_dict = dicts.setdefault(key, {})
               inner_dict['s'] = v.get('s')
               sent = dicts[key]['s']
               binary = re.split("\s+", line.strip())[0]
               list.append((key, sent, binary))
      return key, sent, binary

   def score(self,key,sent,binary):
      <something>

if __name__ == "__main__":

   call = scoring()
   lst = call.values()
   for l in lst :
        call.score(l[0],l[1],l[2])

The basic idea is to do something like this: 基本的想法是做这样的事情:

lst = []
lst.append((1,2,3))
lst.append((4,5,6))
for l in lst :
   print l[0], l[1], l[2]

it will print 它将打印

1 2 3
4 5 6

Your return statement is inside two for loops. 您的return语句位于两个for循环内。 This means that you are only going to go through the loops once before the return statement would leave the function. 这意味着在return语句离开函数之前,您只需要经历一次循环。 This is as much help that I can give without more information as you might want to use yield or to create lists or define class attributes that can be referenced by the entire class. 我可以在没有更多信息的情况下提供尽可能多的帮助,因为您可能想使用yield或创建列表或定义可以被整个类引用的类属性。 Phase provide a more detailed desired output. 阶段提供了更详细的所需输出。

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

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