Creating a Randomized Emoji String in Python

TS 2016 Report.png

You might have noticed this part of the 2016 Emoji ReportΒ – part of the graphic features an emoji for each of our subscribers. It’s a randomized ordering of all the different skin tones of the πŸ‘©β€πŸ’» and πŸ‘¨β€πŸ’» emoji, repeated [number of subscribers] / 12. Our subscriber count wasn’t exactly divisible by 12, so I deleted a few blond πŸ‘¨β€πŸ’» because usually they are over-represented.

A note on inclusion: I nearly used the πŸ—£ emoji, but I decided that was too anonymous, and anonymous defaults are usually male. We think that at least half our subscribers are women, so this seemed like a nice way to show that off. I also debated whether to use the default yellow emoji, but opted to in part because of my colleague John’s article Did I Grow Up And Become The Yellow Hand?Β 

We needed so many emoji, that I knew I would have to script it. I tend to script things in Python, and figured I could use iPython and that would be cool. In the end I ran into a couple of things and opted toΒ make a file instead. Those things were: emoji support in the terminal was terrible (come on Apple!) and I couldn’t see what I was doing (emoji support in XCode is great), and writing to a file from iPython seemed a bit annoying (of course I go to look that up, and find that it’s actually straightforwardΒ – is there a word for the StackOverflow answers you find after you’ve already solved your problem?).

The only other gotcha was that I needed the elements in a list to shuffle and random them, which makes sense. Here’s the entire four lines of code! Notice that at the top the encoding is specified.

# -*- coding: UTF-8 -*-

import numpy
import random

# A list of the emoji we want to randomize.
emojilist =  'πŸ‘©β€πŸ’»','πŸ‘©πŸ»β€πŸ’»','πŸ‘©πŸ½β€πŸ’»','πŸ‘©πŸΌβ€πŸ’»','πŸ‘©πŸΎβ€πŸ’»','πŸ‘©πŸΏβ€πŸ’»','πŸ‘¨β€πŸ’»','πŸ‘¨πŸ»β€πŸ’»','πŸ‘¨πŸΌβ€πŸ’»','πŸ‘¨πŸ½β€πŸ’»','πŸ‘¨πŸΎβ€πŸ’»','πŸ‘¨πŸΏβ€πŸ’»'

# Repeat it X number of times (I'm using 10 here as an example).
repeated = numpy.repeat(emojilist, 10)

# Randomize the list (in place).
random.shuffle(repeated)

# Join the elements together with nothing in between, and print it out.
# It's easy to pipe the output to a file using ">".
print ''.join(repeated)

Leave a Reply