Python常用命令之集合
2021-02-13 03:17
标签:mod nio not 遍历 one this set 批量添加 输出 获取list长度 追加元素 添加固定位置 删除某元素【只删除一个】 移除最后一个元素 清空List 修改元组值 声明元组 合并元组Tuple 【不会去重】 构造元组【2个括号】 声明Set Set添加元素 批量添加 Set 删除元素 合并set 内容 合并set 内容(不需要新变量) 声明set Dictionary 创建字典 访问字段元素 输出字典的值 输出字典的k,v 复制 Dictionary Python常用命令之集合 标签:mod nio not 遍历 one this set 批量添加 输出 原文地址:https://blog.51cto.com/dba10g/2488431
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, ‘apple‘ is in the fruits list")
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple type is str
thistuple = ("apple")
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
print(thistuple)
thisset = {"apple", "banana", "cherry"}
print(thisset)
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
thisset = {"apple", "banana", "cherry"}
thisset.update(["orange", "mango", "grapes"])
print(thisset)
thisset = {"apple", "banana", "cherry"}
//不存在 抛异常
thisset.remove("banana")
//不存在 不抛异常
thisset.discard("banana")
// 不知道删的内容
x = thisset.pop()
print(thisset)
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)
thisset = set(("apple", "banana", "cherry")) # note the double round-brackets
print(thisset)
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
# 如果没有值,异常
x = thisdict["mod1el"]
print(x)
# 如果没有值,返回None
x = thisdict.get("mo1del")
print(x)
for x in thisdict.values():
print(x)
for x, y in thisdict.items():
print(x, y)
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict)