我是靠谱客的博主 无限小蝴蝶,最近开发中收集的这篇文章主要介绍用python定义两个元组_python学习笔记2---python中表示“组”的概念与定义,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

列表(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 "", 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'

元组(tuple)

>>> (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))

>>> 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])

str list tuple都是序列

>>> '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(' ')

32

set 集合

集合的特性

>>> 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({})

>>> type(set())

>>> len(set())

0

dict 字典

一个字典可以有很多个key和value,字典是一个集合类型,而不是序列

通过key 得到/访问 value

>>> {"menu1":"宫保鸡丁","menu2":"糖醋里脊","menu3":"粉丝蒸虾","menu4":"鱼香肉丝"}[0]

Traceback (most recent call last):

File "", 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': '鱼香肉丝'}

value的类型可以是str | int | float | list | set | dict

>>> 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': '鱼香肉丝'}

最后

以上就是无限小蝴蝶为你收集整理的用python定义两个元组_python学习笔记2---python中表示“组”的概念与定义的全部内容,希望文章能够帮你解决用python定义两个元组_python学习笔记2---python中表示“组”的概念与定义所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部