Python Forum
Sample random, unique string pairs from a list without repetitions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sample random, unique string pairs from a list without repetitions
#1
Hi everyone,

This is my first post on this forum so please bear with me. At the very beginning I'd like to let you know that I'm a complete rookie and before you start hammering me that I haven't tried to solve the problem myself, that it's basics etc. please know that I did some research into this kind of a problem, mainly on how to extract unique pairs from a list however most of the solutions I've come across are a bit too complex for me to follow and had a bit different application as they were strictly integer based rather than string based. I literally just started my adventure with coding.

Problem:
I want to write a program which will select two random names from a list and by doing so will create a unique pair of names. The two people selected will be nominated to buy Christmas presents for each other. We always have a problem in my family when it comes to buying Christmas presents so I thought it'd be nice to randomize things a little.

import random

names = ["Adam","Anna","Jessica","Stephen","Claude","Gary"]


for i in names:
    k = random.sample(names, 2)
    j =k
    if j == k:
        k = random.sample(names,2)
    print(k) 
The main problem now is that I don't know how to create a proper check loop which would compare the newly generated pairs with the names stored in the original list so I can avoid having the same or repetitive pairs. For that reason I used the if statement to generate another set of pairs but I realise it is not the proper way of doing it, therefore any guidance on how proceed would be much appreciated.

Thanks in advance.
Reply
#2
Two approaches to solving:

Shuffle the names like a deck of cards and remove names two at a time. This is the easiest solution. You can use random.shuffle(names) or names = random.sample(names, len(names)) to do the shuffling.

Randomly pick names from the list like picking cards from a fan. Remove the picked name from the list just like you remove the card from the fan. This is closer to what you were doing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star Pairs Trading Simulation Kiitoos 0 246 Feb-19-2024, 08:27 PM
Last Post: Kiitoos
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,255 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,724 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,529 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  random.choices and random.sample azizrasul 5 2,269 Sep-16-2022, 08:10 PM
Last Post: deanhystad
  How to get unique entries in a list and the count of occurrence james2009 5 2,995 May-08-2022, 04:34 AM
Last Post: ndc85430
  Pairs of multiplied prime number--->N Frankduc 13 3,558 Jan-16-2022, 01:52 PM
Last Post: Frankduc
  generating random string unique forever Skaperen 5 2,335 Aug-20-2021, 07:15 AM
Last Post: Gribouillis
  Unable to use random.choice(list) in async method spacedog 4 3,450 Apr-29-2021, 04:08 PM
Last Post: spacedog
  Extracting unique pairs from a data set based on another value rybina 2 2,309 Feb-12-2021, 08:36 AM
Last Post: rybina

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020