今天要介绍的是Python的组合数据类型
整理不易,希望得到大家的支持,欢迎各位读者评论点赞收藏
感谢!
S = {1010, "1010", 78.9}
print(type(S))
#
print(len(S))
# 3
print(S)
# {78.9, 1010, '1010'}
T = {1010, "1010", 12.3, 1010, 1010}
print(T)
# {1010, '1010', 12.3}
S = {1010, "1010", 78.9}
T = {1010, "1010", 12.3, 1010, 1010}
print(S - T)
# {78.9}
print(T – S)
# {12.3}
print(S & T)
# {1010, '1010'}
print(T & S)
# {1010, '1010'}
print(S ^ T)
# {78.9, 12.3}
print(T ^ S)
# {78.9, 12.3}
print(S | T)
# {78.9, 1010, 12.3, '1010'}
print(T | S)
# {1010, 12.3, 78.9, '1010'}
S = set('知之为知之不知为不知')
print(S)
# {'不', '为', '之', '知'}
for i in S:
print(i, end="")
# 不为之知
映射类型是“键-值”数据项的组合,每个元素是一个键值对,即元素是(key, value),元素之间是无序的。键值对是一种二元关系,源于属性和值的映射关系
映射类型是序列类型的一种扩展。在序列类型中,采用从0开始的正向递增序号进行具体元素值的索引。而映射类型则由用户来定义序号,即键,用其去索引具体的值。
键(key)表示一个属性,也可以理解为一个类别或项目,值(value)是属性的内容,键值对刻画了一个属性和它的值。键值对将映射关系结构化,用于存储和表达。
ls = [1010, "1010", [1010, "1010"], 1010]
print(ls)
# [1010, '1010', [1010, '1010'], 1010]
print(list('列表可以由字符串生成'))
# ['列', '表', '可', '以', '由', '字', '符', '串', '生', '成']
print(list())
# []
ls = [1010, "1010", [1010, "1010"], 1010]
print(ls[3])
# 1010
print(ls[-2])
# [1010, '1010']
print(ls[5])
'''
Traceback (most recent call last):
File "", line 1, in
ls[5]
IndexError: list index out of range'''
for <循环变量> in <列表变量>:
<语句块>
ls = [1010, "1010", [1010, "1010"], 1010]
for i in ls:
print(i*2)
'''
2020
10101010
[1010, '1010', 1010, '1010']
2020'''
<列表或列表变量>[N: M]
或
<列表或列表变量>[N: M: K]
ls = [1010, "1010", [1010, "1010"], 1010]
print(ls[1:4])
# ['1010', [1010, '1010'], 1010]
print(ls[-1:-3])
# []
print(ls[-3:-1])
# ['1010', [1010, '1010']]
print(ls[0:4:2])
# [1010, [1010, '1010']]
ls = [1010, "1010", [1010, "1010"], 1010]
print(len(ls))
# 4
lt =["Python", ["1010", 1010, [
1010, "
Python"]]]
print(len(lt))
# 2
ls = [1010, 10.10, 0x1010]
print(min(ls))
# 10.1
lt = ["1010", "10.10", "Python"]
print(max(lt))
# 'Python'
ls = ls + lt
print(ls)
# [1010, 10.1, 4112, '1010', '10.10', 'Python']
print(min(ls))
'''
Traceback (most recent call last):
File "", line 1, in
min(ls)
TypeError: '<' not supported between instances of 'str' and 'float''' '
print(list("Python"))
# ['P', 'y', 't', 'h', 'o', 'n']
print(list({"小明", "小红", "小白", "小新"}))
# ['小红', '小明', '小新', '小白']
print(list({"201801":"小明", "201802":"小红", "201803":"小白"}))
# ['201801', '201802', '201803']
<列表变量>.<方法名称>(<方法参数>)
lt = ["1010", "10.10", "Python"]
lt.append(1010)
print(lt)
# ['1010', '10.10', 'Python', 1010]
lt.append([1010, 0x1010])
print(lt)
# ['1010', '10.10', 'Python', 1010, [1010, 4112]]
lt = ["1010", "10.10", "Python"]
ls = [1010, [1010, 0x1010]]
ls += lt
print(lt)
['1010', '10.10', 'Python', 1010, [1010, 4112]]
lt = ["1010", "10.10", "Python"]
lt.insert(1, 1010)
print(lt)
# ['1010', 1010, '10.10', 'Python']
lt = ["1010", "10.10", "Python"]
lt.clear()
print(lt)
# []
lt = ["1010", "10.10", "Python"]
print(lt.pop(1))
# 10.10
print(lt)
# ["1010", "Python"]
lt = ["1010", "10.10", "Python"]
lt.remove("10.10")
print(lt)
# ["1010", "Python"]
del <列表变量>[<索引序号>] 或
del <列表变量>[<索引起始>: <索引结束>]
lt = ["1010", "10.10", "Python"]
del lt[1]
print(lt)
# ["1010", "Python"]
lt = ["1010", "10.10", "Python"]
del lt[1:]
print(lt)
# ["1010"]
lt = ["1010", "10.10", "Python"]
print(lt.reverse())
# ['Python', '10.10', '1010']
lt = ["1010", "10.10", "Python"]
ls = lt.copy()
lt.clear() # 清空lt
print(ls)
# ["1010", "10.10", "Python"]
lt = ["1010", "10.10", "Python"]
ls = lt # 仅使用等号
lt.clear()
print(ls)
# []
lt = ["1010", "10.10", "Python"]
lt[1] = 1010
print(lt)
# ["1010", 1010, "Python"]
{<键1>:<值1>, <键2>:<值2>, … , <键n>:<值n>}
d = {"201801":"小明", "201802":"小红", "201803":"小白"}
print(d)
# {'201801': '小明', '201802': '小红', '201803': '小白'}
<值> = <字典变量>[<键>]
d = {"201801":"小明", "201802":"小红", "201803":"小白"}
print(d["201802"])
# 小红
d["201802"] = '新小红'
print(d)
# {'201801': '小明', '201803': '小白', '201802': '新小红'}
t = {}
t["201804"] = "小新"
print(d)
# {'201804': '小新'}
d = {"201801":"小明", "201802":"小红", "201803":"小白"}
print(len(d))
# 3
d = {"201801":"小明", "201802":"小红", "201803":"小白"}
print(min(d))
# '201801'
print(max(d))
# '201803'
d = dict()
print(d)
# {}
<字典变量>.<方法名称>(<方法参数>)
d = {"201801":"小明", "201802":"小红", "201803":"小白"}
print(d.keys())
# dict_keys(['201801', '201802', '201803'])
print(type(d.keys()))
#
print(list(d.keys()))
# ['201801', '201802', '201803']
d = {"201801":"小明", "201802":"小红", "201803":"小白"}
print(d.values())
# dict_values(['小明', '小红', '小白'])
print(type(d.values()))
#
print(list(d.values()))
# ['小明', '小红', '小白']
d = {"201801":"小明", "201802":"小红", "201803":"小白"}
print(d.items())
# dict_items([('201801', '小明'), ('201802', '小红'),('201803', '小白')])
print(type(d.items()))
#
print(list(d.items()))
# [('201801', '小明'), ('201802', '小红'), ('201803', '小白')]
d = {"201801":"小明", "201802":"小红", "201803":"小白"}
print(d.get('201802'))
'小红'
print(d.get('201804'))
print(d.get('201804', '不存在'))
'不存在'
d = {"201801":"小明", "201802":"小红", "201803":"小白"}
print(d.pop('201802'))
# '小红'
print(d)
# {'201801': '小明', '201803': '小白'}
print(d.pop('201804', '不存在'))
# '不存在'
d = {"201801":"小明", "201802":"小红", "201803":"小白"}
print(d.popitem())
# ('201803', '小白')
print(d)
# {'201801': '小明', '201802': '小红'}
d = {"201801":"小明", "201802":"小红", "201803":"小白"}
d.clear()
print(d)
# {}
d = {"201801":"小明", "201802":"小红", "201803":"小白"}
del d["201801"]
print(d)
# {'201802': '小红', '201803': '小白'}
d = {"201801":"小明", "201802":"小红", "201803":"小白"}
print("201801" in d)
# True
print("201804" in d)
# False
for <变量名> in <字典名>
<语句块>
d = {"201801":"小明", "201802":"小红", "201803":"小白"}
for k in d:
print("字典的键和值分别是:{}和{}".format(k, d.get(k)))
'''
字典的键和值分别是:201801和小明
字典的键和值分别是:201802和小红
字典的键和值分别是:201803和小白'''
在很多情况下,会遇到这样的问题:对于一篇给定文章,希望统计其中多次出现的词语,进而概要分析文章的内容。这个问题的解决可用于对网络信息进行自动检索和归档。
在信息爆炸时代,这种归档或分类十分有必要。这就是“词频统计”问题。
统计《哈姆雷特》英文词频
第一步:分解并提取英文文章的单词
通过txt.lower()函数将字母变成小写,排除原文大小写差异对词频统计的干扰。为统一分隔方式,可以将各种特殊字符和标点符号使用txt.replace()方法替换成空格,再提取单词。
第二步:对每个单词进行计数
if word in counts:
else:
counts[word] = 1
或者,这个处理逻辑可以更简洁的表示为如下代码:
counts[word] = counts.get(word,0) + 1
items = list(counts.items())#将字典转换为记录列表
items.sort(key=lambda x:x[1], reverse=True) #以第2列排序
# CalHamlet.py
def getText():
txt = open("hamlet.txt", "r").read()
txt = txt.lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
txt = txt.replace(ch, " ") #将文本中特殊字符替换为空格
return txt
hamletTxt = getText()
words = hamletTxt.split()
counts = {}
for word in words:
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(10):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))
>>>
the 1138
and 965
to 754
of 669
you 550
a 542
i 542
my 514
hamlet 462
in 436
主要针对初学程序设计的读者,具体讲解了程序设计语言的基本概念,理解程序开发的IPO编写方法,配置Python开发环境的具体步骤,以及Python语言和Python程序特点等内容,进一步给出了5个简单Python实例代码,帮助读者测试Python开发环境,对该语言有一个直观认识。
Python大戏即将上演,一起来追剧吧。