Xiaobai ask for help: python displays the remaining elements after random

I want to use python to implement the following requirement. Ask God to help me:
A sequence:
team= ("1 "r=random.sample (team,3)
)
r=random.sample (team,3)
I also want to randomly select 2 of the remaining elements after random. How can this be realized?
Please help me. Thank you very much

Feb.28,2021

strip out the selected elements and do random.sample () again. When a sequence (collection) contains repeating elements, you should use the sequence subscript as a parameter to random.sample () , rather than the sequence itself.

Please refer to the following code

-sharp -*- coding: utf-8 -*-
import random


team = ('1', '2', '3', '4', '5', '6', '7')


def f1():
    -sharp :
    r1 = random.sample(team, 3)
    r2 = []
    for item in team:
        if item not in r1:
            r2.append(item)
    print('result1: %s' % random.sample(r2, 2))


def f2():
    -sharp : set
    r1 = random.sample(team, 3)
    r2 = tuple(set(team) - set(r1))
    print('result2: %s' % random.sample(r2, 2))


def f3():
    -sharp :
    index_r1 = random.sample(range(len(team)), 3)
    -sharp r1 = [team[i] for i in index_r1]
    remain_index = tuple(set(range(len(team))) - set(index_r1))
    index_r2 = random.sample(remain_index, 2)
    r2 = [team[i] for i in index_r2]
    print('result3: %s' % r2)


if __name__ == '__main__':
    f1()
    f2()
    f3()
The question of

is actually a pseudo-requirement. First 3 are random and then 2 are random in the remaining elements. In fact, it is equivalent to random 5. Take 3 first and then 2

.
Menu