摘要:列表嵌套列表,相当于其他语言中的二维数组列表的基本操作宫保鸡丁宫保鸡丁糖醋里脊粉丝蒸虾鱼香肉丝鱼香肉丝宫保鸡丁糖醋里脊粉丝蒸虾鱼香肉丝宫保鸡丁糖醋里脊宫保鸡丁糖醋里脊粉丝蒸虾鱼香肉丝鱼香肉丝宫保鸡丁糖醋里脊粉丝蒸虾鱼香肉丝鱼香肉丝宫保鸡
列表(list)
>>> type([1,2,3,4,5,6])>>> type(["hello","worls",1,9]) >>> type(["hello","worls",1,9,True,False]) >>> type([[1,2],[3,4],[True,False]])//嵌套列表,相当于其他语言中的二维数组
"宫保鸡丁" >>> ["宫保鸡丁","糖醋里脊","粉丝蒸虾","鱼香肉丝"][3] "鱼香肉丝" >>> ["宫保鸡丁","糖醋里脊","粉丝蒸虾","鱼香肉丝"][0:2] ["宫保鸡丁", "糖醋里脊"] >>> ["宫保鸡丁","糖醋里脊","粉丝蒸虾","鱼香肉丝"][-1] "鱼香肉丝" >>> ["宫保鸡丁","糖醋里脊","粉丝蒸虾","鱼香肉丝"][-1:] ["鱼香肉丝"] >>> ["宫保鸡丁","糖醋里脊","粉丝蒸虾","鱼香肉丝"]+["拍黄瓜","炒豆芽"] ["宫保鸡丁", "糖醋里脊", "粉丝蒸虾", "鱼香肉丝", "拍黄瓜", "炒豆芽"] >>> ["拍黄瓜","炒豆芽"]*["拍黄瓜","炒豆芽"] Traceback (most recent call last): File "元组(tuple)", line 1, in ["拍黄瓜","炒豆芽"]*["拍黄瓜","炒豆芽"] TypeError: can"t multiply sequence by non-int of type "list" >>> ["拍黄瓜","炒豆芽"]*3 ["拍黄瓜", "炒豆芽", "拍黄瓜", "炒豆芽", "拍黄瓜", "炒豆芽"] >>> ["拍黄瓜","炒豆芽"]-["拍黄瓜"] Traceback (most recent call last): File " ", line 1, in ["拍黄瓜","炒豆芽"]-["拍黄瓜"] TypeError: unsupported operand type(s) for -: "list" and "list"
>>> (1,2,3,4,5) (1, 2, 3, 4, 5) >>> (1,"-1",True) (1, "-1", True) >>> (1,2,3,4)[0] 1 >>> (1,2,3,4)[0:2] (1, 2) >>> (1,2,3)+(4,5,6) (1, 2, 3, 4, 5, 6) >>> (1,2,3)*3 (1, 2, 3, 1, 2, 3, 1, 2, 3) >>> type((1,2,3))str list tuple都是序列>>> type(1) >>> type([1,2,3]) >>> type("hello") >>> type(("hello")) >>> type((1))//如果括号里只有一个元素,python会默认为是要做数学运算,等同于type(1) >>> (1+1)*2 4 >>> (1,2,3) (1, 2, 3) >>> (1) 1 >>> (1,) (1,) >>> type((1,)) >>> type(()) >>> type([1])
>>> "hello world"[2] "l" >>> [1,2,3][2] 3 >>> (1,2,3)[2] 3切片
>>> [1,2,3,4,5][0:3] [1, 2, 3] >>> [1,2,3,4,5][-1:] [5] >>> >>> "hello world"[0:8:0] Traceback (most recent call last): File "判断一个序列中是否包含某个元素", line 1, in "hello world"[0:8:0] ValueError: slice step cannot be zero >>> "hello world"[0:8:1] "hello wo" >>> "hello world"[0:8:2] "hlow" >>> "hello world"[0:8:3] "hlw" >>> "hello world"[0:8:4] "ho" >>> "hello world"[0:8:5] "h " >>> "hello world"[0:8:6] "hw" >>> "hello world"[0:8:7]
>>> 3 in [1,2,3,4,5,6] True >>> 7 in [1,2,3,4,5,6] False >>> 3 not in [1,2,3,4,5,6] False序列的其他操作
>>> len([1,2,3,4,5,6]) 6 >>> len("hello world") 11 >>> max([1,2,3,4,5,6]) 6 >>> min([1,2,3,4,5,6]) 1 >>> max("hello world") "w" >>> min("hello world") " " >>> min("helloworld") "d"ASCII码
>>> ord("w") 119 >>> ord("d") 100 >>> ord(" ") 32set 集合 集合的特性
>>> type({1,2,3,4,5,6})集合的操作>>> {1,2,3,4,5,6}[0] //集合是无序的,没有像str,list,tuple的架构索引,不能切片 Traceback (most recent call last): File " ", line 1, in {1,2,3,4,5,6}[0] TypeError: "set" object does not support indexing >>> {1,2,3,4,5,6}[0:2] Traceback (most recent call last): File " ", line 1, in {1,2,3,4,5,6}[0:2] TypeError: "set" object is not subscriptable >>> {1,1,2,2,3,3,4,4} //集合的第二个特性:不重复 {1, 2, 3, 4}
>>> len({1,2,3}) 3 >>> len({1,1,1}) 1 >>> len({1,2,2}) 2 >>> 1 in {1,2,3} True >>> 1 not in {1,2,3} False >>> {1,2,3,4,5,6} - {3,4} //求两个集合的差集 {1, 2, 5, 6} >>> {1,2,3,4,5,6} & {3,4} //交集 {3, 4} >>> {1,2,3,4,5,6} | {3,4,7} //合集|并集 {1, 2, 3, 4, 5, 6, 7} //定义一个空的集合set() >>> type({})dict 字典>>> type(set()) >>> len(set()) 0
>>> {"menu1":"宫保鸡丁","menu2":"糖醋里脊","menu3":"粉丝蒸虾","menu4":"鱼香肉丝"}[0] Traceback (most recent call last): File "value的类型可以是str | int | float | list | set | dict", line 1, in {"menu1":"宫保鸡丁","menu2":"糖醋里脊","menu3":"粉丝蒸虾","menu4":"鱼香肉丝"}[0] KeyError: 0 >>> {"menu1":"宫保鸡丁","menu2":"糖醋里脊","menu3":"粉丝蒸虾","menu4":"鱼香肉丝"}["menu1"] "宫保鸡丁" >>> {"menu1":"宫保鸡丁","menu1":"白斩鸡","menu2":"糖醋里脊","menu3":"粉丝蒸虾","menu4":"鱼香肉丝"}["menu1"] "白斩鸡" >>> {"menu1":"宫保鸡丁","menu1":"白斩鸡","menu2":"糖醋里脊","menu3":"粉丝蒸虾","menu4":"鱼香肉丝"} {"menu1": "白斩鸡", "menu2": "糖醋里脊", "menu3": "粉丝蒸虾", "menu4": "鱼香肉丝"} >>> {1:"宫保鸡丁","1":"白斩鸡","menu2":"糖醋里脊","menu3":"粉丝蒸虾","menu4":"鱼香肉丝"} //key也可以是数字 {1: "宫保鸡丁", "1": "白斩鸡", "menu2": "糖醋里脊", "menu3": "粉丝蒸虾", "menu4": "鱼香肉丝"}
>>> type({1:"宫保鸡丁","1":"白斩鸡","menu2":{1:1},"menu3":"粉丝蒸虾",})key必须是不可变的类型(str、tuple)
>>> {[1,2]:"宫保鸡丁","menu2":"糖醋里脊","menu3":"粉丝蒸虾","menu4":"鱼香肉丝"} Traceback (most recent call last): File "", line 1, in {[1,2]:"宫保鸡丁","menu2":"糖醋里脊","menu3":"粉丝蒸虾","menu4":"鱼香肉丝"} TypeError: unhashable type: "list" >>> {(1,2):"宫保鸡丁","menu2":"糖醋里脊","menu3":"粉丝蒸虾","menu4":"鱼香肉丝"} {(1, 2): "宫保鸡丁", "menu2": "糖醋里脊", "menu3": "粉丝蒸虾", "menu4": "鱼香肉丝"}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/44780.html
摘要:示例有理数的算术有理数可表示为整数的比值,并且它组成了实数的一个重要子类。有理数的值需要两部分来描述。这里的重要概念是,通过将有理数表示为整数的比值,我们能够完全避免近似问题。返回有理数的分子。 2.2 数据抽象 来源:2.2 Data Abstraction 译者:飞龙 协议:CC BY-NC-SA 4.0 由于我们希望在程序中表达世界中的大量事物,我们发现它们的大多数都具有...
摘要:一的基本语法缩进统一个或者个空格。中的数据类型中有个标准类型数字字符串列表元组集合字典数字复数在中,只有一种整数类型,表示长整型。如则会显示,并不是换行。空行与代码缩进不同,空行并不是语法的一部分。我们将首行及后面的代码组称为一个子句。 一、python3的基本语法 1、缩进统一(1个tab或者4个空格)。 for i in range(10): print (i) ...
摘要:标题标签会将其中的文本加粗加黑显示,并从到依次减弱。换行标签换行符。可在标签内使用。规定表头单元格可横跨的行数。HTML的概念 概念: HTML 是用来描述网页的一种语言。 HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML 不是一种编程语言,而是一种标记语言 (markup language) 标记语言是一套标记标签 (markup t...
摘要:的锁是非公平锁,默认情况下也是非公平锁,但可以通过带布尔值的构造函数要求使用公平锁。有序性,是保证线程内串行语义,避免指令重排等。公平性是减少线程饥饿个别线程长期等待锁,但始终无法获取情况发生的一个办法。 目录介绍 1.Synchronize和ReentrantLock区别 1.1 相似点 1.2 区别 1.3 什么是线程安全问题?如何理解 1.4 线程安全需要保证几个基本特性 ...
阅读 929·2021-11-22 12:09
阅读 3703·2021-09-27 13:36
阅读 1389·2021-08-20 09:37
阅读 4007·2019-12-27 12:22
阅读 2352·2019-08-30 15:55
阅读 2357·2019-08-30 13:16
阅读 2816·2019-08-26 17:06
阅读 3433·2019-08-23 18:32