上一篇《Python笔记:string,tuple,list,dictionary的区别(之一,基本用法与区别)》讲述了这四种类型的基本用法与区别,本篇讲述的是高级用法与类型转换。关于list的更多的高级用法在下一篇笔记中有详细演示。《List的高级用法 (aliasing, mutability, cloning详解)》
平时写代码tuple用的不多。具体的用法在MIT的python课程里提到过两种。
#used to return more than one value from a function
def quotient_and_remainder(x, y):
q = x // y
r = x % y
return(q, r)
result = quotient_and_remainder(4,5)
print(result)
(0, 4)
2.互换赋值(swap),python的这种操作可以减少一个临时变量的使用。这种用法同样可以用在list类型上。
x_tuple = ('a',1)
y_tuple = ('b',2)
(x_tuple, y_tuple) = (y_tuple, x_tuple)
print(x_tuple)
print(y_tuple)
('b', 2)
('a', 1)
list作为一种常见object,有很多的method可以用。
x_list = ['Peggy','Susie','Daniel']
y_list = ['Pedro','Rebecca']
print(x_list)
print(y_list)
['Peggy', 'Susie', 'Daniel']
['Pedro', 'Rebecca']
Expression | Result |
---|---|
x_list.append(‘George’) | x_list = [‘Peggy’, ‘Susie’, ‘Daniel’, ‘George’] |
x_list.pop() | x_list = [‘Peggy’, ‘Susie’, ‘Daniel’] |
x_list.extend(y_list) | x_list = [‘Peggy’, ‘Susie’, ‘Daniel’, ‘Pedro’, ‘Rebecca’] |
x_list.remove(‘Daniel’) | x_list = [‘Peggy’, ‘Susie’, ‘Pedro’, ‘Rebecca’] |
del(x_list[1]) | x_list = [‘Peggy’, ‘Pedro’, ‘Rebecca’] |
x_list.append('George')
print(x_list)
x_list.pop()
print(x_list)
x_list.extend(y_list)
print(x_list)
x_list.remove('Daniel')
print(x_list)
del(x_list[1])
print(x_list)
['Peggy', 'Susie', 'Daniel', 'George']
['Peggy', 'Susie', 'Daniel']
['Peggy', 'Susie', 'Daniel', 'Pedro', 'Rebecca']
['Peggy', 'Susie', 'Pedro', 'Rebecca']
['Peggy', 'Pedro', 'Rebecca']
text_string = 'Who knows what will happen in the future?'
text_string.split()[-1]
'future?'
text_list = text_string.split()
text_list
['Who', 'knows', 'what', 'will', 'happen', 'in', 'the', 'future?']
text_string_rejoin = ' '.join(text_list)
text_string_rejoin
'Who knows what will happen in the future?'
data = 'the quick brown fox jumps over a lazy dog.'
chars = list(set(data))
data_size, vocab_size = len(data), len(chars)
print('There are %d total characters and %d unique characters in your data.' % (data_size, vocab_size))
There are 42 total characters and 28 unique characters in your data.
char_to_ix = { ch:i for i,ch in enumerate(sorted(chars)) }
ix_to_char = { i:ch for i,ch in enumerate(sorted(chars)) }
print(ix_to_char)
print(char_to_ix)
{0: ' ', 1: '.', 2: 'a', 3: 'b', 4: 'c', 5: 'd', 6: 'e', 7: 'f', 8: 'g', 9: 'h', 10: 'i', 11: 'j', 12: 'k', 13: 'l', 14: 'm', 15: 'n', 16: 'o', 17: 'p', 18: 'q', 19: 'r', 20: 's', 21: 't', 22: 'u', 23: 'v', 24: 'w', 25: 'x', 26: 'y', 27: 'z'}
{' ': 0, '.': 1, 'a': 2, 'b': 3, 'c': 4, 'd': 5, 'e': 6, 'f': 7, 'g': 8, 'h': 9, 'i': 10, 'j': 11, 'k': 12, 'l': 13, 'm': 14, 'n': 15, 'o': 16, 'p': 17, 'q': 18, 'r': 19, 's': 20, 't': 21, 'u': 22, 'v': 23, 'w': 24, 'x': 25, 'y': 26, 'z': 27}
names = ["Peggy", "Susie", "Daniel"]
animals = ["pig", "sheep", "dog"]
最直接的想法是分别对names,和animals做遍历赋值
names_animals = {}
for i in range(len(names)):
names_animals[names[i]] = animals[i]
print(names_animals)
{'Peggy': 'pig', 'Susie': 'sheep', 'Daniel': 'dog'}
另一种方法,是调用zip函数将两个list合并,再做for循环。这种方法更像python的风格。
names_animals = {}
names_animals_zipped = zip(names, animals)
names_animals = { name:animal for name,animal in names_animals_zipped }
print(names_animals)
{'Peggy': 'pig', 'Susie': 'sheep', 'Daniel': 'dog'}
###参考链接