List
Python list is an ordered collection of the most flexible type of object.
# List iteration and resolve
>>> res = [c*4 for c in 'Spam']
>>> res
['SSSS', 'pppp', 'aaaa', 'mmmm'
>>> res = []
>>> for c in 'Spam':
... res.append(c*4)
...
>>> res
['SSSS', 'pppp', 'aaaa', 'mmmm']
>>> list(map(abs,[-1,-2,0,1,2]))
[1, 2, 0, 1, 2]
# General Operation
>>> L = [5,3,6,2,8]
>>> sorted(L)
[2, 3, 5, 6, 8]
>>> L
[5, 3, 6, 2, 8]
>>> L.sort()
>>> L
[2, 3, 5, 6, 8]
>>> L.insert(0,1)
>>> L
[1, 2, 3, 5, 6, 8]
>>> L.reverse()
>>> L
[8, 6, 5, 3, 2, 1]
>>>
>>> del L[0]
>>> L
[6, 5, 3, 2, 1]
>>> L.pop()
1
>>> L
[6, 5, 3, 2]
>>> L.remove(6)
>>> L
[5, 3, 2]
'''
Place the list of modifications: Because Python only deal with object references, so you need to modify the original objects at a separate area with a new object generated.
Because when you modify an object in place, may also affect more than one reference to it.
'''
Other #
>>> L = ['already','got','one']
>>> L
['already', 'got', 'one']
>>> L[1:]=[]
>>> L
['already']
>>> L[0]=[]
>>> L
[[]]
dictionary
If the list is ordered as a collection of objects, dictionaries can be treated as unordered collection. The main difference is that: among the elements of the dictionary is accessed by a key, rather than by shifting access.
>>> D = {'food':{'ham':1,'egg':2}}
>>> D.get('food')
{ 'Him': 1, 'egg': 2}
>>> D2 = {'a':1,'b':2}
>>> D.update(D2)
>>> D
{'food': {'ham': 1, 'egg': 2}, 'a': 1, 'b': 2}
>>> D.pop('b')
2
>>> len(D)
2
>>> D
{'food': {'ham': 1, 'egg': 2}, 'a': 1}
>>> del D['a']
>>> D
{'food': {'ham': 1, 'egg': 2}}
>>> D = {x:x*2 for x in range(10)}
>>> D
{0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}
>>> D = {'spam':2,'ham':1,'eggs':3}
>>> D['spam']
2
>>> D
{'spam': 2, 'ham': 1, 'eggs': 3}
>>> len(D)
3
>>> 'ham' in D
True
>>> list(D.keys())
['spam', 'ham', 'eggs']
>>> list(D.values())
[2, 1, 3]
>>> D
{'spam': 2, 'ham': 1, 'eggs': 3}
>>> D['ham']=['grill','bake','fry']
>>> D Wuxi look good Men's Hospital Which https://yyk.familydoctor.com.cn/20612/
{'spam': 2, 'ham': ['grill', 'bake', 'fry'], 'eggs': 3}
>>> del D['eggs']
>>> D
{'spam': 2, 'ham': ['grill', 'bake', 'fry']}
>>> D['brunch'] = 'Bacon'
>>> D
{'spam': 2, 'ham': ['grill', 'bake', 'fry'], 'brunch': 'Bacon'}
>>> list(D.items())
[('spam', 2), ('ham', ['grill', 'bake', 'fry']), ('brunch', 'Bacon')]
>>> D2 = {'toast':4,'muffin':5}
>>> D.update(D2)
>>> D
{'spam': 2, 'ham': ['grill', 'bake', 'fry'], 'brunch': 'Bacon', 'toast': 4, 'muffin': 5}
>>> table = {'Python':'Guido van Rossum',
... 'Perl':'Larry Wall',
... 'Tcl':'John Ousterhout'}
>>>
>>> language = 'Python'
>>> creator = table[language]
>>> creator
"Guido van Rossum '
>>> for lang in table:
... print (just, '\ t', table [only])
...
Python Guido van Rossum
Perl Larry Wall
Tcl John Ousterhout
# Three ways to avoid missing-key error
... if (2,3,6) in Matrix:
... print(Matrix[(2,3,6)])
... else:
... print(0)
...
0
>>>
>>>
>>> try:
... print(Matrix[(2,3,6)])
... except KeyError:
... print(0)
...
0
>>>
>>>
>>> Matrix.get((2,3,4),0)
98
>>> Matrix.get((2,3,6),0)
0