Logged in as: guest Log in
Review session - Python list features & extra credit jbrian / Version 2

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************
   
IDLE 1.2.4     
# demonstrating the * operator: a sequence * an int n results in n copies of the sequence concatenated
>>> [1, 2, 3] * 10
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
# strings are sequences
>>> "Cat" * 5
'CatCatCatCatCat'
>>> "cat ate rat ate" * 4
'cat ate rat atecat ate rat atecat ate rat atecat ate rat ate'
# the index function: find the location of some value in a list
>>> [1, 2, 3].index()

Traceback (most recent call last):
  File "", line 1, in
    [1, 2, 3].index()
TypeError: index() takes at least 1 argument (0 given)
>>> [1, 2, 3].index(5)

Traceback (most recent call last):
  File "", line 1, in
    [1, 2, 3].index(5)
ValueError: list.index(x): x not in list
>>> [1, 2, 3].index(2)
1
# the append function: add to the end of a list. If given a list, adds it as a single item rather than concatenating.
>>> x = []
>>> x.append(5)
>>> x
[5]
>>> x.append([6 7])
SyntaxError: invalid syntax
>>> x.append([6, 7])
>>> x
[5, [6, 7]]
# the extend function: concatenate two lists
>>> x.extend([8, 9])
>>> x
[5, [6, 7], 8, 9]
# operator +: concatenate two lists (but not destructively, that would be +=)
>>> x + 10

Traceback (most recent call last):
  File "", line 1, in
    x + 10
TypeError: can only concatenate list (not "int") to list
>>> x + [10]
[5, [6, 7], 8, 9, 10]
# method remove
>>> x.remove([6, 7])
# list comprehensions: a concise way of doing the same thing to every element of a list, rather than using a loop.
>>> x
[5, 8, 9]
>>> ['a' * num for num in list]

Traceback (most recent call last):
  File "", line 1, in
    ['a' * num for num in list]
TypeError: 'type' object is not iterable
>>> ['a' * num for num in x]
['aaaaa', 'aaaaaaaa', 'aaaaaaaaa']
>>> xrange(10)
xrange(10)
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [0 for x in range(10)]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# can transform and filter a list at the same time.
>>> ['a' * num for num in range(10) if num % 2 == 0]
['', 'aa', 'aaaa', 'aaaaaa', 'aaaaaaaa']
# equivalent loop would be:
# result = []
# for num in range(10):
#    if num % 2 == 0:
#        result.append('a' * num)

##############################
# Note:
# list comprehensions are structurally quite similar to SQL: the python implementors could easily change the syntax to something like
# [SELECT 'a' * num FROM range(10) AS num WHERE num % 2 == 0]
# C# actually has something like the above: they call it
# "Language INtegrated Query" (LINQ).  It lets you write a SQL-style query
# against either a database or just a plain in-memory collection, and it's
# part of the language syntax rather than parsed from a string (so compile-time syntax and type checking, etc.)
##############################

##############################
# Challenge for advanced students (8 points extra credit on HW4):
# Since we are so close to the PSet 4 due date, you may email me your solution by 12:00AM Sunday 11/21.
# I will not provide any help with this, seeing as it's extra credit,
# but I may give partial credit if your solution is mostly right.
#
# Suppose you have a list of tuples (id, first, last, sex, dob)
# stored in the variable ourList, and you want to query it like an in-memory
# database table.  Can you write a single python expression
# (using one or more list comprehensions) equivalent to this SQL query?
#
#   SELECT last, count(*), MAX(dob)
#   FROM ourList WHERE sex='F'
#   GROUP BY last
#
# You will probably need to use Python's sets to make this work, and note that you should
# NEVER write a processing task this complicated as a single expression in serious code.
##############################

# zip: list of a, list of b --> list of tuples (a, b)
>>> x = range(10)
>>> y = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
>>> zip

>>> zip(x, y)
[(0, 3), (1, 1), (2, 4), (3, 1), (4, 5), (5, 9), (6, 2), (7, 6), (8, 5), (9, 3)]
>>> z = zip(x, y)
# multiple assignment, tuple unpacking, also works in list comprehensions.
>>> first, second = z[0]
>>> first
0
>>> second
3
>>> [a + b for (a, b) in z]
[3, 2, 6, 4, 9, 14, 8, 13, 13, 12]
>>> x = range(10)
# indexing and slicing
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x[5]
5
>>> x[3:5]
[3, 4]
>>> x[3:]
[3, 4, 5, 6, 7, 8, 9]
>>> x[:3]
[0, 1, 2]
>>> x[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# slicing with negative offsets: shorthand for offsetting from the end of the list.
>>> x[1:-1]
[1, 2, 3, 4, 5, 6, 7, 8]
>>> myString = '''"Hello, World!"'''
>>> myString
'"Hello, World!"'
>>> myString[1:-1]
'Hello, World!'
# slices can be replaced or deleted to splice pieced into or out of a list.
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x[2:5] = ['a']
>>> x
[0, 1, 'a', 5, 6, 7, 8, 9]
>>> del x[2:5]
>>> x
[0, 1, 7, 8, 9]
>>> x.append(6)
>>> x
[0, 1, 7, 8, 9, 6]
>>> x = [5, [6, 7], 8]
>>> x
[5, [6, 7], 8]
>>> x[1].append(8, 9)

Traceback (most recent call last):
  File "", line 1, in
    x[1].append(8, 9)
TypeError: append() takes exactly one argument (2 given)
>>> x[1].append(8)
>>> x
[5, [6, 7, 8], 8]
>>> x[1].extend([9, 10, 11])
>>> x
[5, [6, 7, 8, 9, 10, 11], 8]
# the max function.  related: min, sum, len.
>>> max([9, 8, 7])
9
# using list comprehensions to help find the longest inner list in a list of lists - may be useful for your homework.
>>> x = [[1], [1, 4, 5], [2, 3]]
>>> [len(sublist) for sublist in list]

Traceback (most recent call last):
  File "", line 1, in
    [len(sublist) for sublist in list]
TypeError: 'type' object is not iterable
>>> [len(sublist) for sublist in x]
[1, 3, 2]
>>>



Site built using pyWeb version 1.10
© 2010 Leonard McMillan, Alex Jackson and UNC Computational Genetics