Python

Python 3:

- Working with iterators (like range generators or map functions) may throw errors. Convert to list:

print (sheet.columns[1]) CHANGE TO print (list(sheet.columns)[1])

SEE: http://www-personal.umich.edu/~mejn/cp/chapters/AppendixB.pdf

----------------------------------------------------

openpyxl

openpyxl documentation

 

 

********************************************
Sum a List of Numbers:
print (sum(range(1,1001)))


********************************************
Multiply Each Item in a List by 2:
print (list(map(lambda x: x * 2, range(1,11))))


********************************************
Verify if Exists in a String:
wordlist = ["scala", "akka", "play framework", "sbt", "typesafe"]
tweet = "This is an example tweet talking about scala and sbt."
print (list(map(lambda x: x in tweet.split(),wordlist)))


********************************************
Read in a File (Windows backslashes must be handled):
    (https://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%E2%80%94-backslashes-in-windows-filenames/)

print (open('D:/wwwroot/Python/fido/test.txt','r').readlines())

print (open(r'D:\wwwroot\Python\fido\test.txt','r').readlines())

import os
mypath = os.path.normpath("D:/wwwroot/Python/fido/test.txt")
print (mypath)
print (open(mypath).readlines())

 

********************************************
Sort:
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda pair: pair[1])
print (pairs)

 

********************************************
Compute prime numbers:

nums = range(2, 50)
for i in list(range(2, 8)):
     nums = list(filter(lambda x: x == i or x % i, nums))
print (nums)

* Note use of "list()" for Python 3. Iterator objects must be converted to lists.


********************************************