-
Notifications
You must be signed in to change notification settings - Fork 1
Week 4 Solutions
DanRutz edited this page Jul 18, 2012
·
2 revisions
In [1]: l1 = ["Apples", "Pears", "Oranges", "Peaches"]
In [2]: l1
Out[2]: ['Apples', 'Pears', 'Oranges', 'Peaches']
In [3]: i1 = raw_input("What's another fruit: ")
What's another fruit: Bananas
In [4]: l1.append(i1)
In [5]: l1
Out[5]: ['Apples', 'Pears', 'Oranges', 'Peaches', 'Bananas']
In [6]: n1 = input("Which fruit number to show (1 is first): ")
Which fruit number to show (1 is first): 3
In [7]: print "Fruit number %s is %s."%(str(n1), l1[n1 - 1])
Fruit number 3 is Oranges.
In [8]: l1 = ['Mangos'] + l1
In [9]: l1
Out[9]: ['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches', 'Bananas']
In [10]: for x in l1:
....: if x[0] == "P":
....: print x
....:
Pears
Peaches
In [11]: print [x for x in l1 if x[0] == "P"]
['Pears', 'Peaches']
In [1]: l2 = ['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches', 'Bananas']
In [2]: l2
Out[2]: ['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches', 'Bananas']
In [3]: l2.pop()
Out[3]: 'Bananas'
In [4]: l2
Out[4]: ['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches']
In [5]: i2 = raw_input("Which fruit to delete: ")
Which fruit to delete: Oranges
In [6]: l2.remove(i2)
In [7]: l2
Out[7]: ['Mangos', 'Apples', 'Pears', 'Peaches']
In [1]: l2 = ['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches', 'Bananas']
In [2]: l2
Out[2]: ['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches', 'Bananas']
In [3]: l2.pop()
Out[3]: 'Bananas'
In [4]: l2
Out[4]: ['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches']
In [5]: l2 = l2 * 2
In [6]: l2
Out[6]: ['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches', 'Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches']
In [7]: while True:
...: i2 = raw_input("Which fruit to delete: ")
...: if i2 in l2:
...: while i2 in l2:
...: l2.remove(i2)
...: break
...:
Which fruit to delete: Tomatos
Which fruit to delete: Beans
Which fruit to delete: Apples
In [8]: l2
Out[8]: ['Mangos', 'Pears', 'Oranges', 'Peaches', 'Mangos', 'Pears', 'Oranges', 'Peaches']
In [1]: l3 = ['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches', 'Bananas']
In [2]: l3
Out[2]: ['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches', 'Bananas']
In [3]: for fruit in l3:
...: if raw_input("Do you like %s?"%fruit.lower()) == "no":
...: l3.remove(fruit)
...: print l3
...:
Do you like mangos?yes
['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches', 'Bananas']
Do you like apples?no
['Mangos', 'Pears', 'Oranges', 'Peaches', 'Bananas']
Do you like oranges?
# It failed to ask about Pears!!!
In [1]: l3 = ['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches', 'Bananas']
In [2]: l3
Out[2]: ['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches', 'Bananas']
In [3]: x = 0
In [4]: while x < len(l3):
...: if raw_input("Do you like %s?"%l3[x].lower()) == "no":
...: del l3[x]
...: else:
...: x += 1
...: print l3
...:
Do you like mangos?yes
['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches', 'Bananas']
Do you like apples?yes
['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches', 'Bananas']
Do you like pears?no
['Mangos', 'Apples', 'Oranges', 'Peaches', 'Bananas']
Do you like oranges?yes
['Mangos', 'Apples', 'Oranges', 'Peaches', 'Bananas']
Do you like peaches?no
['Mangos', 'Apples', 'Oranges', 'Bananas']
Do you like bananas?yes
['Mangos', 'Apples', 'Oranges', 'Bananas']
In [1]: l1 = ['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches', 'Bananas']
In [2]: l4 = [x[::-1] for x in l1]
In [3]: l1.pop()
Out[3]: 'Bananas'
In [4]: l1
Out[4]: ['Mangos', 'Apples', 'Pears', 'Oranges', 'Peaches']
In [5]: l4
Out[5]: ['sognaM', 'selppA', 'sraeP', 'segnarO', 'sehcaeP', 'sananaB']
In [1]: d1 = {"name": "Dan", "city": "Edmonds", "cake": "German Chocolate"}
In [2]: d1
Out[2]: {'cake': 'German Chocolate', 'city': 'Edmonds', 'name': 'Dan'}
In [3]: del d1["cake"]
In [4]: d1
Out[4]: {'city': 'Edmonds', 'name': 'Dan'}
In [5]: d1["fruit"] = "Mango"
In [6]: d1
Out[6]: {'city': 'Edmonds', 'fruit': 'Mango', 'name': 'Dan'}
In [7]: d1.keys()
Out[7]: ['city', 'fruit', 'name']
In [8]: d1.values()
Out[8]: ['Edmonds', 'Mango', 'Dan']
In [9]: "cake" in d1.keys()
Out[9]: False
In [10]: "Mango" in d1.values()
Out[10]: True
In [1]: d2 = dict([(x, hex(x)) for x in range(16)])
In [2]: d2
Out[2]: {0: '0x0', 1: '0x1', 2: '0x2', 3: '0x3', 4: '0x4', 5: '0x5', 6: '0x6', 7: '0x7', 8: '0x8', 9: '0x9', 10: '0xa', 11: '0xb', 12: '0xc', 13: '0xd', 14: '0xe', 15: '0xf'}
In [1]: d1 = {'city': 'Edmonds', 'fruit': 'Mango', 'name': 'Dan'}
In [2]: d3 = {}
In [3]: for x in d1:
...: d3[x] = d1[x].count("n")
...:
In [4]: d3
Out[4]: {'city': 1, 'fruit': 1, 'name': 1}
In [1]: d1 = {'city': 'Edmonds', 'fruit': 'Mango', 'name': 'Dan'}
In [2]: d3 = dict([(x, d1[x].count("n")) for x in d1])
In [3]: d3
Out[3]: {'city': 1, 'fruit': 1, 'name': 1}
In [1]: s2 = set([x for x in range(21) if x % 2 == 0])
In [2]: s3 = set([x for x in range(21) if x % 3 == 0])
In [3]: s4 = set([x for x in range(21) if x % 4 == 0])
In [4]: s3.issubset(s2)
Out[4]: False
In [5]: s4.issubset(s2)
Out[5]: True
In [1]: s = set("Python")
In [2]: s
Out[2]: set(['h', 'o', 'n', 'P', 't', 'y'])
In [3]: s.add("i")
In [4]: s
Out[4]: set(['i', 'h', 'o', 'n', 'P', 't', 'y'])
In [5]: fs = frozenset("marathon")
In [6]: fs
Out[6]: frozenset(['a', 'h', 'm', 'o', 'n', 'r', 't'])
In [7]: s.union(fs)
Out[7]: set(['a', 'i', 'h', 'm', 'o', 'n', 'P', 'r', 't', 'y'])
In [8]: s.intersection(fs)
Out[8]: set(['h', 't', 'o', 'n'])