简体   繁体   English

从具有多个返回值的函数中仅访问一个返回值

[英]access just one return value from function with multiple return values

I am having trouble figuring out how I would access a value returned from a function where I am returning multiple values. 我在弄清楚如何访问从我返回多个值的函数返回的值时遇到了麻烦。 I have a dictionary object that contains a tuple for the values like this. 我有一个包含这样的值的元组的字典对象。

import random

payments = {'12345678121365489': ('11', '2022', '666'),
            '5136982546823120': ('03', '2021', '523')}

def pick_card_combo():
    rand_dict, info = random.choice(list(practice_dict.items()))
    return(rand_dict, info)

Of course, I can index the "info" tuple and pick out certain elements but I was wondering how I could get each individual index for my generator expression in this function here. 当然,我可以为“ info”元组建立索引并挑选出某些元素,但是我想知道如何在此函数中为生成器表达式获取每个单独的索引。

def create_accounts(accs_gend):
    my_gen = ([first_name(), last_name(), country, pick_card_combo()
               <rand_dict>, <info[0]>, <info[1]>, <info[2]>] for i in range(accs_gend))
    account = tuple(my_gen)
    print(account)

I have different items in that dictionary so I would like for the various items to be used with the different tuples I generate and not just one item. 我在该词典中有不同的项目,因此我希望将各种项目与我生成的不同元组一起使用,而不仅仅是一个项目。 Is there a way to access each respective return value here why calling this function each time a new element is generated? 这里有一种方法可以访问每个返回值,为什么每次生成新元素时都要调用此函数?

I prefer to keep my generator objects inside the tuple as is too since that is the format which xlsxwriter requires it. 我也更喜欢将生成器对象也保留在元组中,因为这是xlsxwriter要求的格式。

break it down more 分解更多

def create_account():
    """ create one single account """
    rand_dict, info = pick_card_combo()
    return ([first_name(), last_name(), country,]+ [rand_dict,] + info )

def create_accounts(accs_gend):
    """ Create some number of random accounts """
    for i in range(accs_gend):
        yield create_account() # by using yield this is a "generator" 

five_random_accounts = tuple(create_accounts(5)) # calling tuple will evaluate the generator and create a tuple of all the elements

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

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