Удаление скобок из результатов комбинированного списка itertools - PullRequest
2 голосов
/ 17 октября 2019

У меня есть этот код

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

Ответы [ 2 ]

1 голос
/ 17 октября 2019

Это кортежи. Вы можете объединить персонажей в кортежах с помощью ''.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>))
0 голосов
/ 17 октября 2019

Вы можете сделать это следующим образом (без использования str.join):

print('Key one is: {}{}'.format(*key_one))
print('Key two is: {}{}'.format(*key_two))
print('Key three is: {}{}'.format(*key_three))

Это должно иметь дополнительное преимущество - избегать создания промежуточных строк в join.

...