Python,Lists and Dictionaries

返回

>>> rec = {} >>> rec['name'] = 'mel' >>> rec['age'] = 41 >>> rec['job'] = 'trainer/write' >>> >>> print rec['name'] mel >>> mel = { 'name': 'Mark', ... 'jobs': ['trainer', 'writer'], ... 'web': 'www.rmi.net/~lutz', ... 'home':{'state': 'CO', 'zip':80501}} >>> mel['name'] 'Mark' >>> mel['jobs'] ['trainer', 'writer'] >>> mel['jobs'][1] 'writer' >>> mel['home']['zip'] 80501 >>> >>> table = {'Python': 'Guido van Rossum', ... 'Perl': 'Larry Wall', ... 'Tcl': 'John Ousterhout'} >>> language = 'Python' >>> creator = table[language] >>> creator 'Guido van Rossum' >>> for lang in table.keys(): ... print lang,'\t',table[lang} File "<stdin>", line 2 print lang,'\t',table[lang} ^ IndentationError: expected an indented block >>> print lang,'\t',table[lang] Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'lang' is not defined >>> for lang in table.keys(): ... print lang,'\t',table[lang] File "<stdin>", line 2 print lang,'\t',table[lang] ^ IndentationError: expected an indented block >>> table {'Python': 'Guido van Rossum', 'Tcl': 'John Ousterhout', 'Perl': 'Larry Wall'} >>> for lang in table.keys(): ... print lang, '\t', table[lang] ... Python Guido van Rossum Tcl John Ousterhout Perl Larry Wall >>> >>> L = [] >>> L[99] = 'spam' Traceback (most recent call last): File "<stdin>", line 1, in ? IndexError: list assignment index out of range >>> D = {} >>> D[99] = 'spam' >>> D[99] 'spam' >>> D {99: 'spam'} >>> Matrix = {} >>> Matrix[(2,3,4)] = 88 >>> Matrix[(7,8,9)] = 99 >>> >>> X = 2; Y = 3; Z = 4 >>> Matrix[(X,Y,Z)] 88 >>> Matrix {(2, 3, 4): 88, (7, 8, 9): 99} >>> Matrix[(2,3,6)] Traceback (most recent call last): File "<stdin>", line 1, in ? KeyError: (2, 3, 6) >>> if Matrix.has_key[(2,3,6)]: ... print Matrix[(2,3,6)] ... else: ... print 0 ... Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unsubscriptable object >>> try: ... print Matrix[(2,3,6)] ... except KeyError: ... print 0 ... 0 >>> if Matrix.has_key[(2,3,6)]: ... print Matrix[(2,3,6)] ... else: ... print 0 ... Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unsubscriptable object >>> Matrix.get(2,3,4), 0) File "<stdin>", line 1 Matrix.get(2,3,4), 0) ^ SyntaxError: invalid syntax >>> Matrix.get((2,3,4), 0) 88 >>> Matrix.get((2,3,6), 0) 0 >>> >>> d2 = {'spam':2, 'ham':1,'eggs':3} >>> ds['spam'] Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'ds' is not defined >>> d2['spam'] 2 >>> d2 {'eggs': 3, 'ham': 1, 'spam': 2} >>> len(d2) 3 >>> d2.has_key('ham') True >>> 'ham' in d2 True >>> d2.keys() ['eggs', 'ham', 'spam'] >>> d2['ham'] = ['grill', 'bake', 'fry'] >>> d2 {'eggs': 3, 'ham': ['grill', 'bake', 'fry'], 'spam': 2} >>> del de['eggs'] Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'de' is not defined >>> del d2['eggs'] >>> d2 {'ham': ['grill', 'bake', 'fry'], 'spam': 2} >>> d2['brunch'] = 'Bacon' >>> d2 {'brunch': 'Bacon', 'ham': ['grill', 'bake', 'fry'], 'spam': 2} >>> d2.values(), d2.items() (['Bacon', ['grill', 'bake', 'fry'], 2], [('brunch', 'Bacon'), ('ham', ['grill', 'bake', 'fry']), ('spam', 2)]) >>> d2.get('spam'), d2.get('toast'), d2.get('toast', 88) (2, None, 88) >>> d2 {'brunch': 'Bacon', 'ham': ['grill', 'bake', 'fry'], 'spam': 2} >>> d3 = {'toast':4, 'muffin':5} >>> d2.update(d3) >>> d2 {'toast': 4, 'muffin': 5, 'brunch': 'Bacon', 'ham': ['grill', 'bake', 'fry'], 's pam': 2} >>> >>> len([1,2,3]) 3 >>> [1,2,3]+[4,5,6] [1, 2, 3, 4, 5, 6] >>> ['Ni!']*5 ['Ni!', 'Ni!', 'Ni!', 'Ni!', 'Ni!'] >>> 3 in [1 2 3 File "<stdin>", line 1 3 in [1 2 3 ^ SyntaxError: invalid syntax >>> 3 in [1 2 3] File "<stdin>", line 1 3 in [1 2 3] ^ SyntaxError: invalid syntax >>> 3 in [1, 2, 3] True >>> for x in [1,2,3]:print x, ... 1 2 3 >>> [1,2] + list("34") [1, 2, '3', '4'] >>> L = ['spam', 'Spam', 'SPAM!'] >>> L[2] 'SPAM!' >>> L[-2] 'Spam' >>> L[1:] ['Spam', 'SPAM!'] >>> matrix = [[1,2,3],[4,5,6],[7,8,9]] >>> matrix[1] [4, 5, 6] >>> matrix[1][1] 5 >>> matrix=[[1,2,3], ... [4,5,6], ... [7,8,9]] >>> matrix[1][1] 5 >>> >>> L = ['spam', 'Spam', 'SPAM!'] >>> L ['spam', 'Spam', 'SPAM!'] >>> L.append('please') >>> L ['spam', 'Spam', 'SPAM!', 'please'] >>> L.sort() >>> L ['SPAM!', 'Spam', 'please', 'spam'] >>> L = [1,2] >>> L.extend([3,4,5]) >>> L [1, 2, 3, 4, 5] >>> L.pop() 5 >>> L [1, 2, 3, 4] >>> L.reverse() >>> L [4, 3, 2, 1] >>> L =[] >>> L.append(1) >>> L.append(2) >>> L [1, 2] >>> L.pop() 2 >>> L [1] >>> L = ['SPAM!', 'eat', 'more', 'please'] >>> del[0] SyntaxError: can't assign to literal >>> del L[0] >>> L ['eat', 'more', 'please'] >>> del L[1:] >>> L ['eat'] >>> L = ['Already', 'got', 'one'] >>> L[1:] = [] >>> L ['Already'] >>> L[0] = [] >>> L [[]] >>>
返回