Python笔记:string,tuple,list,dictionary的区别(之二,高级用法与类型转换)

上一篇《Python笔记:string,tuple,list,dictionary的区别(之一,基本用法与区别)》讲述了这四种类型的基本用法与区别,本篇讲述的是高级用法与类型转换。关于list的更多的高级用法在下一篇笔记中有详细演示。《List的高级用法 (aliasing, mutability, cloning详解)》

tuple的用法

平时写代码tuple用的不多。具体的用法在MIT的python课程里提到过两种。

  1. 定义函数的返回值时,用tuple可以返回多个值。但是我发现list也有这个功能,可能tuple的不可修改性更适合完成这个场景。
#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 的用法

list作为一种常见object,有很多的method可以用。

x_list = ['Peggy','Susie','Daniel']
y_list = ['Pedro','Rebecca']
print(x_list)
print(y_list)
['Peggy', 'Susie', 'Daniel']
['Pedro', 'Rebecca']
ExpressionResult
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']
  1. append(),无论输入是element还是list,都会当成element。
  2. '+'的作用类似于extend,输入都是list,然后把里面的element添加到list里面。

string 和list 之间的转换,split(),join() 用于文本分析

  1. string可以使用split()函数,将其中每一个单词分离成单独元素,结果是list类型。
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?']
  1. list可以使用join()函数,将很多个字符串用指定的符号链接起来,返回字符串类型
text_string_rejoin = ' '.join(text_list)
text_string_rejoin
'Who knows what will happen in the future?'

生成dictionary的方法

  1. 通过字符串生成字典,在文本处理中常见。
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}
  1. 通过两个同样长度的list来生成字典
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'}

###参考链接

  1. Python - Lists https://www.tutorialspoint.com/python/python_lists.htm
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值