Skip to content

Instantly share code, notes, and snippets.

@infinite-Joy
Created December 17, 2017 14:39
Show Gist options
  • Save infinite-Joy/aaaf2fdbff7f73393b43ce4b911e9a4d to your computer and use it in GitHub Desktop.
Save infinite-Joy/aaaf2fdbff7f73393b43ce4b911e9a4d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 7.29 s, sys: 51.6 ms, total: 7.34 s\n",
"Wall time: 7.34 s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"import gzip\n",
"\n",
"def compute_user_subreddit_ids():\n",
" user_ids = []\n",
" subreddit_ids = []\n",
" subreddit_to_id = {}\n",
" i = 0\n",
" with gzip.open('reddituserpostingbehavior.csv.gz', 'rb') as f:\n",
" for line in f:\n",
" this_line = line.decode(\"utf-8\")\n",
" for sr in this_line.rstrip().split(',')[1:]:\n",
" if sr not in subreddit_to_id:\n",
" subreddit_to_id[sr] = len(subreddit_to_id)\n",
" user_ids.append(i)\n",
" subreddit_ids.append(subreddit_to_id[sr])\n",
" i += 1\n",
" return user_ids, subreddit_ids\n",
" \n",
"user_ids, subreddit_ids = compute_user_subreddit_ids()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (venv)",
"language": "python",
"name": "venv"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment