Skip to content

Instantly share code, notes, and snippets.

@decisionstats
Created December 24, 2016 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save decisionstats/42b3fc90ae6fa537a19a08017e0336cb to your computer and use it in GitHub Desktop.
Save decisionstats/42b3fc90ae6fa537a19a08017e0336cb to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import re\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"numlist=[\"$10000\",\"$20,000\",\"30,000\",40000,\"50000 \"]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function sub in module re:\n",
"\n",
"sub(pattern, repl, string, count=0, flags=0)\n",
" Return the string obtained by replacing the leftmost\n",
" non-overlapping occurrences of the pattern in string by the\n",
" replacement repl. repl can be either a string or a callable;\n",
" if a string, backslash escapes in it are processed. If it is\n",
" a callable, it's passed the match object and must return\n",
" a replacement string to be used.\n",
"\n"
]
}
],
"source": [
"help(re.sub)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"for i,value in enumerate(numlist):\n",
" \n",
" numlist[i]=re.sub(r\"([$,])\",\"\",str(value))\n",
" numlist[i]=int(numlist[i])\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here re.sub replaces the $ and , patterns with nothing (\"\") from each value of the list just like gsub does in R. In python str converts an object to string just like paste does in R. In the next step, int converts the object to numeric values (integer) just as as.numeric does in R"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10000, 20000, 30000, 40000, 50000]\n"
]
}
],
"source": [
"print(numlist)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"30000.0"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.mean(numlist)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on class enumerate in module builtins:\n",
"\n",
"class enumerate(object)\n",
" | enumerate(iterable[, start]) -> iterator for index, value of iterable\n",
" | \n",
" | Return an enumerate object. iterable must be another object that supports\n",
" | iteration. The enumerate object yields pairs containing a count (from\n",
" | start, which defaults to zero) and a value yielded by the iterable argument.\n",
" | enumerate is useful for obtaining an indexed list:\n",
" | (0, seq[0]), (1, seq[1]), (2, seq[2]), ...\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __getattribute__(self, name, /)\n",
" | Return getattr(self, name).\n",
" | \n",
" | __iter__(self, /)\n",
" | Implement iter(self).\n",
" | \n",
" | __new__(*args, **kwargs) from builtins.type\n",
" | Create and return a new object. See help(type) for accurate signature.\n",
" | \n",
" | __next__(self, /)\n",
" | Implement next(self).\n",
" | \n",
" | __reduce__(...)\n",
" | Return state information for pickling.\n",
"\n"
]
}
],
"source": [
"help(enumerate)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we used enumerate to replace and convert each value of the list one by one. This is a powerful method unique to Python as for loops are not computationally efficient in R."
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [Root]",
"language": "python",
"name": "Python [Root]"
},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment