+ 1
How i print not duplicate data
I am try to build python code x =[a,.....] y=[t,......] a= random x b = random y Z = a+b Print (z) x here gives me random data. #the problem is here But i need only one unique data Thank you all
23 odpowiedzi
+ 6
'random.sample' is your friend here.
https://www.geeksforgeeks.org/JUMP_LINK__&&__python__&&__JUMP_LINK-random-sample-function/
https://www.geeksforgeeks.org/randomly-select-elements-from-list-without-repetition-in-python/
Here is an example:
https://code.sololearn.com/czks2wCwdotw/?ref=app
+ 5
Hamza A Foji ,
not quite clear what you mean exactly.
> can you please rework the sample code and the description so that we can get an idea what your issue is.
thanks!
+ 3
I'm confused 🤔
You have a loop with 20 iterations but 2 lists with 3 values each. Could you give an example of expected output please?
+ 3
Now I'm more confused because you are saying you don't want duplicate data, but you just give an example of expected output that contains duplicate data.
+ 3
I realize the first code meets your needs but if we wanted all combinations we could append the results in opposite order and have all 18 combinations like StuartH mentioned.
https://code.sololearn.com/cI2bZvQU4SR0/?ref=app
+ 2
Hamza A Foji when you want to contact someone personally, just click on the @ symbol and a list of users will pop up from which you can select the one you need, then the user will receive a personal notification.
+ 2
I think this may help you 😉
https://code.sololearn.com/czks2wCwdotw/?ref=app
+ 2
Keith
Thank you for the help
I appreciate that
This is what i want exactly
Great job bro
+ 2
Welcome! Glad it helped 👍
+ 2
Keith i am in the first step you give me a great help bro . I think i need more practice and imagination....have a good day bro
+ 1
Thank you Keith
I will try with that
+ 1
If I understood your idea correctly, then this can be done without the help of "random"...😎
x = ['a','b','c']
y = ['x','y','z']
a = list(set(x))[0]
b = list(set(y))[0]
z = a+b
print(z)
+ 1
thank you solo
I will try this one too
I am wrong because I don't bring the code from my pc so sorry you help me with imagination.thank you
+ 1
Hamza A Foji ,
# You don't need variables.
import random
print(
random.choice(["happy", "sad"]),
random.choice(["cat", "dog"])
)
"'" Outputs one of these phrases:
sad dog
sad cat
happy dog
happy cat
"""
+ 1
Hamza A Foji ,
A Python set automatically removes duplicates, so what you could do is, in a loop, keep adding random combinations to a set until the set size is what you want (it won't increase when you add a duplicate) then break out of the loop and print the set.
But be careful that the desired set size is not greater than the number of possible combinations, or the set will never reach that size, and the loop will keep generating duplicate combinations forever.
+ 1
Do you want every possible combination returned or a set number of pairs? Two possibilities I can see are:
1. For a set number, enter each combination that is returned into a list. After each pair is generated, check if its in the list and if so, generate again until you get a unique value.
2. To simply output every possible combination, iterate through the lists outputting each in turn.
I (or anyone else) can go into more detail on either depending on whether they are what you mean?
+ 1
Thank you StuartH
0
Ok i will try to grab all the code soon. Thank you
0
@solo like that ok thank you bro