在转换元组时将元组扩展为参数? [英] Expand tuple into arguments while casting them?

查看:27
本文介绍了在转换元组时将元组扩展为参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

blah = random.randint(int(minmax[0]), int(minmax[1]))

我知道这是可能的:

minimum, maximum = int(minmax[0]), int(minmax[1])
blah = random.randint(minimum, maximum)

我可以使用元组参数扩展在一行中完成第二个吗?例如,如果 minmax 是一个整数元组,我可以这样做:

Can I do this second one in a single line using tuple-argument expansion? For example, if minmax was a tuple of integers to begin with, I could do:

blah = random.randint(*minmax)

但是我没有整数元组,我有一个字符串元组.显然,无论哪种方式,这都不是什么大问题.我只是好奇.

But I don't have a tuple of ints, I have a tuple of strs. Obviously it's not a big deal one way or the other. I'm just curious.

推荐答案

是的,这是可行的:

blah = random.randint(*map(int, minmax))

使用 map(int, ...) 执行类型转换.

Use map(int, ...) to perform the type conversion.

这篇关于在转换元组时将元组扩展为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆