0
点赞
收藏
分享

微信扫一扫

python 中有哪些实用的方法


列表

A=['cal','what',1,'2020-1-1','join']

**

增加元素

**

1.append

A.append("Hello")    #元素

2.extend

A.extend([4,'much',1])   #列表

3.insert

A.insert(3,"hello")   #元素,固定位置添加(通过索引)

**

删除元素

**

1.pop

B=A.pop()    #索引移除,默认最后一个

2.remove

A.remove("what")   #通过元素名称,移除

3.del

del A[2]   #索引移除

**

队列翻转

**

1、reverse

A.reverse()    #列表翻转   例如:   
# [3,1,'what']变为['what',1,3]

**

元素计数

**

1、count

C=['Hello','Google','Hello','baidu',
'Hello','baidu','mofa','guiyi']

print(count('Hello'))



结果: 3

**

队列排序

**

1、sort

D=[2,8,44,5,12,1]
D.sort()

元组

定义元组

ll=(1,2,3)
print(ll)
print(ll.type())


结果 (1,2,3)
<class 'tuple'>

注:如果元组中只有一个元素,则这个元组后面必须要有一个",",否则元素就还是原来的类型

例如 : ​​A=(1,)​​ ​正确是元组

A=(1)****​错误​ 不是元组

**

删除元素

**

由于元组不能修改,所以元组也不能删除部分元素,要删除只能删除整个元组

元素计数

1、count

C=('Hello','Google','Hello','baidu',
'Hello','baidu','mofa','guiyi')

print(count('Hello'))



结果: 3

**

查找元素索引

**

A=('cal','what',1,'2020-1-1','join')
print("what index is:",A.index("what"))



结果是 what index is:1

**

字典

**

字典定义:字典类型就和他的名字一样,可以向查字典一样去找,其他语言也有类似的类型。例如:Java中的HashMap , PHP中的Array等

chinese={"we":"我们",
"are from":"来自",
"China":"中国"
}
print(chinese)
print("China:",chinese["China"]) #键查找




结果: {'we': '我们', 'are from': '来自', 'China': '中国'}
China:中国

注意:字典的键必须是唯一的,不重复的,如果是空字典,直接用{}表示

empty={}
print(type(empty))


结果 : <class 'dict'>

**

修改键值

**

chinese={"we":"我们",
"are from":"来自",
"China":"中国"
}
print(chinese)
chinese["China"]="未来"
print(chinese)





结果:{'we': '我们', 'are from': '来自', 'China': '中国'}
{'we': '我们', 'are from': '来自', 'China': '未来'}

python 中有哪些实用的方法_元组

**

删除字典元素

**

chinese={"we":"我们",
"are from":"来自",
"China":"中国"
}
print(chinese)
del chinese["China"]
print(chinese)

python 中有哪些实用的方法_python_02

**

清空元素

**

chinese={"we":"我们",
"are from":"来自",
"China":"中国"
}
print(chinese)
# chinese["China"]="未来"
# del chinese["China"]
chinese.clear()
print(chinese)

python 中有哪些实用的方法_python_03

**

复制字典

**

用于修改复制的字典,相当于复制一个新的字典作为修改,原有的字典不变

chinese={"we":"我们",
"are from":"来自",
"China":"中国"
}
Chi=chinese.copy()

**

创建新字典

**

SQ={"name","age","sex"}
student=dict.fromkeys(SQ)
print(student)
student1=dict.fromkeys(SQ,15)
print(student1)

python 中有哪些实用的方法_删除元素_04

返回键对值

chinese={“we”:“我们”,

“are from”:“来自”,

“China”:“中国”

}

print(chinese.get(“we”))

python 中有哪些实用的方法_python_05

**

返回一个列表,包含字典所有键

**

chinese={"we":"我们",
"are from":"来自",
"China":"中国"
}
print(chinese.keys())

python 中有哪些实用的方法_元组_06

**

返回一个列表,包含字典所有值

**

chinese={"we":"我们",
"are from":"来自",
"China":"中国"
}
print(chinese.values())

python 中有哪些实用的方法_开发语言_07

**

返回一个列表,包含字典所有键和值

**

chinese={"we":"我们",
"are from":"来自",
"China":"中国"
}
print(chinese.items())

python 中有哪些实用的方法_元组_08

**

遍历字典

**

注意:因为字典不能直接应用于for循环,我们可以使用items方法来遍历字典

chinese={"we":"我们",
"are from":"来自",
"China":"中国"
}
print(chinese.items())
for k,v in chinese.items():
print(k,"=>",v)

python 中有哪些实用的方法_开发语言_09

**

集合

**

注意:Python中有一种内置类型叫做集合,它是一种非常有用的数据结构。和列表行为相似,唯一区别在于集合不会包含重复的值



举报

相关推荐

0 条评论