Short cuts to useful Python tutorials from other web pages.
Snippets of code from the other pages is included in this blog.
Attribution is given int he form of a link to the original reference.
Lamda and multiple args
Python lists explained
The list type is a container that holds a number of other objects, in a given order. The list type implements the sequence protocol, and also allows you to add and remove objects from the sequence.
Python List len() Method
The Python yield keyword explained
3,400+ votes on Stack Overflow
calling the function str.lower and assigning the return value which is definitely not iterable
Python sets
NameError: name 're' is not defined
-import the re module
Python - Using regex to find multiple matches and print them out
Python Dictionary (hashes)
Python : List of dict, if exists increment a dict value, if not append a new dict
Pretty printing a dictionary
Use the module name to reference the pprint function
Python sets
Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.
Here is a brief demonstration:
Similarly to list comprehensions, set comprehensions
are also supported:
Snippets of code from the other pages is included in this blog.
Attribution is given int he form of a link to the original reference.
Lamda and multiple args
Python lists explained
The list type is a container that holds a number of other objects, in a given order. The list type implements the sequence protocol, and also allows you to add and remove objects from the sequence.
Python List len() Method
list1, list2 = [123, 'xyz', 'zara'], [456, 'abc'] print "First list length : ", len(list1); print "Second list length : ", len(list2);
The Python yield keyword explained
3,400+ votes on Stack Overflow
calling the function str.lower and assigning the return value which is definitely not iterable
Change the line
content = inputFile.read().lower
to
content = inputFile.read().lower()
Your original line assigns the built-in function lower to your variable content instead of calling the function
str.lower
and assigning the return value which is definitely not iterable.Python sets
> > > y = [1, 1, 6, 6, 6, 6, 6, 8, 8]
> > >; sorted(set(y))
[1, 6, 8]
> > > s = set([1,6,8])
> > > print(s)
{8, 1, 6}
> > > s.update(range(10,100000))
> > > for v in range(10, 100000):
s.remove(v> > >> > > print(s){1, 6, 8}
Python regexs
Python regexs 2
NameError: name 're' is not defined
-import the re module
Python - Using regex to find multiple matches and print them out
line = 'bla bla bla
some text...
more text?'
matches = re.findall('
', line, re.S)
print matches
Python Dictionary (hashes)
Python : List of dict, if exists increment a dict value, if not append a new dict
urls = {'http://www.google.fr/' : 1 }
for url in list_of_urls:
if not url in urls:
urls[url] = 1
else:
urls[url] += 1
http://stackoverflow.com/questions/13757835/make-python-list-unique-in-functional-way-map-reduce-filter?rq=1
Is there a way in Python of making a List unique through functional paradigm ?
Input :
[1,2,2,3,3,3,4]
Output:
[1,2,3,4]
(In order preserving manner)In [29]: a = [1,2,2,3,3,3,4]
In [30]: reduce(lambda ac, v: ac + [v] if v not in ac else ac, a, [])
Out[30]: [1, 2, 3, 4]
Pretty printing a dictionary
Use the module name to reference the pprint function
import pprint
pprint.pprint(...)
Python sets
Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.
Here is a brief demonstration:
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> fruit = set(basket) # create a set without duplicates
>>> fruit
set(['orange', 'pear', 'apple', 'banana'])
>>> 'orange' in fruit # fast membership testing
True
>>> 'crabgrass' in fruit
False
>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a # unique letters in a
set(['a', 'r', 'b', 'c', 'd'])
>>> a - b # letters in a but not in b
set(['r', 'd', 'b'])
>>> a | b # letters in either a or b
set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
>>> a & b # letters in both a and b
set(['a', 'c'])
>>> a ^ b # letters in a or b but not both
set(['r', 'd', 'b', 'm', 'z', 'l'])
>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
set(['r', 'd'])
python remove whitespaces in string
Looping through python regex matches
import re
s = "ABC12DEF3G56HIJ7"
pattern = re.compile(r'([A-Z]+)([0-9]+)')
for (letters, numbers) in re.findall(pattern, s):
print numbers, '*', letters
Why does comparing strings in Python using either '==' or 'is' sometimes produce a different result?
is
is identity testing, ==
is equality testing. what happens in your code would be emulated in the interpreter like this:
>>> a = 'pub'
>>> b = ''.join(['p', 'u', 'b'])
>>> a == b
True
>>> a is b
False
How to test if a dictionary contains a specific key? [duplicate]
x = {'a' : 1, 'b' : 2}
if (x.contains_key('a')):
....
How to check a string for specific characters?
How to return more than one value from a function in Python? [duplicate]