Reputation: 63
I want to generate emojis in a random way. I tried two different methods but still not habing an output mixed
import random
soleillever ="\U0001f305"
Soleildodo = "\U0001f634"
nuage = '\u2601'
soleilnuage = "\U0001f326"
nuageeclair = "\U0001f329"
rainbow = '\U0001f308'
termo = '\U0001F321'
soleil = '\U0001f31e'
soleil2 = "\U0001f31d"
nuageeclairpluie = "\u26C8"
flocon = "\u2744"
print(soleil,soleil2,termo,rainbow,nuageeclair,soleilnuage,nuage,flocon)
a =[rainbow + nuageeclair + soleilnuage + nuage + soleil2 + nuageeclairpluie + flocon + rainbow]
b = random.sample(a, k=len(a))
print(b)
a1 = [rainbow + nuageeclair + soleilnuage + nuage + soleil2 + nuageeclairpluie + flocon + rainbow]
b1 = random.shuffle(a1)
print(b1)
Here is the output with the type
['🌈🌩🌦☁🌝⛈❄🌈']
Here is b <class 'list'>
B1 gives me None
Here is b1<class 'NoneType'>
If someone have an advice, I will appreciate ! B.
Upvotes: 0
Views: 685
Reputation: 4135
There appear to be two mistakes in your code. The first is that random.shuffle
applies the shuffling in-place.
As per my comments, random.shuffle
does not return anything. As such, when you assigned b1
to the value returned by random.shuffle
, it is set to nothing, i.e. None
You can imagine the function as looking like this:
def shuffle(input):
# do something to input
instead of
def shuffle(input):
# do something to input
return input
In your case, it should work if you do the following.
# normal, un-shuffled list
print(a1)
random.shuffle(a1)
print(a1) # same array, but now it has been shuffled
The second problem is that you create a1
, as list of strings by using the + operation. As a result, this essentially creates a single string, contained within a list instead of a list of strings. The difference is as a follows:
lst0 = ['hello']
random.shuffle(lst0) # returns ['hello']
lst1 = ['h'+'e'+'l'+'l'+'o'] # this is identical to lst0
random.shuffle(lst1) # returns ['hello']
lst2 = ['h','e','l','l','o']
random.shuffle(lst2) # returns ['o', 'h', 'l', 'e', 'l']
In your case, try to use commas instead of +. That is:
a1 = [rainbow , nuageeclair , soleilnuage , nuage , soleil2 , nuageeclairpluie , flocon , rainbow]
or directly
a1 = ["\U0001f305","\U0001f634",'\u2601',"\U0001f326","\U0001f329",'\U0001f308','\U0001F321','\U0001f31e',"\U0001f31d","\u26C8","\u2744"]
Upvotes: 1