empty_dict = {}
d1 = {'a':'some value','b':[1,2,3,4]}
d1
{'a': 'some value', 'b': [1, 2, 3, 4]}
d1[7] = 'an integer'
d1
{'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer'}
d1['b']
[1, 2, 3, 4]
'b' in d1
True
d1[5] = 'some value'
d1
{'a': 'some value', 'b': [1, 2, 3, 4], 7: 'an integer', 5: 'some value'}
d1['dummy'] = 'another value'
d1
{'a': 'some value',
'b': [1, 2, 3, 4],
7: 'an integer',
5: 'some value',
'dummy': 'another value'}
del d1[5]
d1
{'a': 'some value',
'b': [1, 2, 3, 4],
7: 'an integer',
'dummy': 'another value'}
ret = d1.pop('dummy')
ret
'another value'
list(d1.keys())
['a', 'b', 7]
list(d1.values())
['some value', [1, 2, 3, 4], 'an integer']
d1.update({'b':'foo','c':12})
d1
{'a': 'some value', 'b': 'foo', 7: 'an integer', 'c': 12}
mapping = dict(zip(range(5),reversed(range(5))))
mapping
{0: 4, 1: 3, 2: 2, 3: 1, 4: 0}
words = ['apple','bat','bar','atom','book']
by_letter = {}
for word in words:
letter = word[0]
if letter not in by_letter:
by_letter[letter] = [word]
else:
by_letter[letter].append(word)
by_letter
{'a': ['apple', 'atom'], 'b': ['bat', 'bar', 'book']}
for word in words:
letter = word[0]
by_letter.setdefault(letter,[]).append(word)
by_letter
{'a': ['apple', 'atom', 'apple', 'atom', 'apple', 'atom'],
'b': ['bat', 'bar', 'book', 'bat', 'bar', 'book', 'bat', 'bar', 'book']}
hash('string')
-5063368329735123182
set([2,1,2,3,4,1])
{1, 2, 3, 4}
{2,1,1,2,3,4}
{1, 2, 3, 4}
a = {1,2,3,4,5}
b = {3,4,5,6,7}
a.union(b)
{1, 2, 3, 4, 5, 6, 7}
a|b
{1, 2, 3, 4, 5, 6, 7}
a.intersection(b)
{3, 4, 5}
a&b
{3, 4, 5}
string = ['a','as','bat','car','dove','python']
[x.upper()for x in string if len(x)>2]
['BAT', 'CAR', 'DOVE', 'PYTHON']
unique_lengths = {len(x)for x in string}
unique_lengths
{1, 2, 3, 4, 6}
loc_mapping = {val:index for index,val in enumerate(string)}
loc_mapping
{'a': 0, 'as': 1, 'bat': 2, 'car': 3, 'dove': 4, 'python': 5}
all_data = [['John','Emily','Michael','Mary','Steven'],['Maria','Juan','Javier','Natalia','Pilar']]
result = [name for names in all_data for name in names
if name.count('e')>=2]
result
['Steven']
some_tuples = [(1,2,3),(4,5,6),(7,8,9)]
flatted = [x for tup in some_tuples for x in tup]
flatted
[1, 2, 3, 4, 5, 6, 7, 8, 9]
states = [' Alabama ', 'Georgia!', 'Georgia', 'georgia', 'FlOrIda',
'south carolina##', 'West virginia?']
import re
def remove_punctuation(value):
return re.sub('[!#?]','',value)
clean_ops = [str.strip,remove_punctuation,str.title]
def clean_string(strings,ops):
result = []
for value in strings:
for function in ops:
value = function(value)
result.append(value)
return result
clean_string(states,clean_ops)
['Alabama',
'Georgia',
'Georgia',
'Georgia',
'Florida',
'South Carolina',
'West Virginia']
equiv_anon = lambda x:x*2
def apply_to_list(some_list,f):
return [f(x) for x in some_list]
ints = [2,3,3,4,2,1]
apply_to_list(ints,lambda x:x*2)
[4, 6, 6, 8, 4, 2]
string = ['foo', 'card', 'bar', 'aaaa', 'abab']
string.sort(key=lambda x:len(set(list(x))))
string
['aaaa', 'foo', 'abab', 'bar', 'card']
def add_number(x,y):
return x+y
add_five = lambda y:add_number(5,y)
some_dict = {'a':1,'b':2,'c':3}
for key in some_dict:
print(key)
a
b
c
dict_iterator = iter(some_dict)
dict_iterator
list(dict_iterator)
['a', 'b', 'c']
def squares(n=10):
print('Generating squares from 1 to {0}'.format(n**2))
for i in range(1,n+1):
yield i**2
gen = squares()
gen
for x in gen:
print(x,end=" ")
Generating squares from 1 to 100
1 4 9 16 25 36 49 64 81 100
gen = (x**2for x in range(100))
gen
at 0x000001A2DFDBF930>
sum(x**2 for x in range(100))
328350
dict((i,i**2)for i in range(5))
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
import itertools
first_letter = lambda x :x[0]
names = ['Alan', 'Adam', 'Wes', 'Will', 'Albert', 'Steven']
for letter,names in itertools.groupby(names,first_letter):
print(letter,list(names))
A ['Alan', 'Adam']
W ['Wes', 'Will']
A ['Albert']
S ['Steven']
float('something')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()
1 #错误和异常处理
----> 2 float('something')
ValueError: could not convert string to float: 'something'
def attempt_float(x):
try:
return float(x)
except:
return x
attempt_float('something')
'something'
float((1,2))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
----> 1 float((1,2))
TypeError: float() argument must be a string or a number, not 'tuple'
def attempt_float(x):
try:
return float(x)
except (TypeError, ValueError):
return x
attempt_float((1,2))
(1, 2)
try:
write_to_file(f)
except:
print('Failed')
else:
print('Succeeded')
finally:
f.close()
path = 'example.txt'
f = open(path)
lines = [x.rstrip() for x in open(path)]
lines
['lzq', 'lfp', 'lkp', 'mjy']
f.close()
with open(path)as f:
lines = [x.rstrip()for x in f]
f = open(path)
f.read(3)
'lzq'
f2 = open(path,'rb')3
f2.read(3)
b'lzq'
import sys
sys.getdefaultencoding()
'utf-8'
f.close()
f2.close()
with open('example.txt','w')as handle:
handle.writelines(x for x in open(path) if len(x)>1)
with open('example.txt')as f:
lines = f.readlines()
lines
[]
with open(path)as f:
chars = f.read(3)
chars
'lzq'
with open(path, 'rb') as f:
data = f.read(3)
data
b'lzq'
data.decode('utf8')
'lzq'