Skip to main content
 首页 » 编程设计

python之从 itertools 组合列表结果中删除括号

2024年11月01日7rubylouvre

我有这个代码

import itertools 
import random 
 
def gen_reset_key(): 
    key = random.randint(100, 199) 
    return key 
 
key_combinations = list(itertools.combinations(str(gen_reset_key()), 2)) 
 
key_one = key_combinations[0] 
key_two = key_combinations[1] 
key_three = key_combinations[2] 
 
print("Key one is: {}".format(key_one)) 
print("Key two is: {}".format(key_two)) 
print("Key three is: {}".format(key_three)) 

输出是:

Key one is: ('1', '9') 
Key two is: ('1', '9') 
Key three is: ('9', '9') 

我想从输出中删除括号和语音标记。我怎样才能做到这一点?

这样我的输出就是:

Key one is 19 
Key two is 19 
Key three is 99 

请您参考如下方法:

这些是元组。您可以使用 ''.join(..) 加入元组中的字符:

print('Key one is: {}'.format(<b>''.join(</b>key_one<b>)</b>)) 
print('Key two is: {}'.format(<b>''.join(</b>key_two<b>)</b>)) 
print('Key three is: {}'.format(<b>''.join(</b>key_three<b>)</b>))