
正文
python集合的函数名,python集合的用法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
python集合和函数
变量名={元素,元素,,,}
nums={11,24,45,96,28}
nums.add(42)
print(nums)
nums={11,24,45,96,28}
nums2=["anfly","tom"]
nums.update(nums2)
print(nums)
1)nums={11,24,45,96,28} [没有该数字报错]
nums.remove(24)
print(nums)
2)nums={11,24,45,96,28} [随机删]
nums.pop()
print(nums)
3)nums={11,24,45,96,28} [没有该数字不会报错]
nums.discard(24)
print(nums)
交集:(存在相同元素)
set1={"anfly","tom","haha"}
set2={"anfly","susala"}
set3=set1set2
print(set3)
并集:(得到全部集合中全部的元素)
set1={1,2,3,4}
set2={3,4,5,6}
new_set=set1|set2
print(new_set)
运算符 (+ * in)
内置函数 (len max min del)
列:set1 = {"1","2","3"}
print(min(set1))
def函数名():
执行语句
函数名()
列:
defhello():
print("hello word")
hello()
2.函数参数:
def add2num():
a=11
b=22
c=a+b
print(c)
add2num()
3.位置参数:
def fun(a,b):
print("a:",a)
print("b:",b)
fun(2,3)
4.关键字参数:
def fun(a,b):
print("a:",a)
print("b:",b)
fun(a=2,b=3) [第一个‘a’可以直接写数字,也可以展示;第二个以及之后‘b’不能直接写数字,要写‘b=3’这种格式]
5.缺省参数
def printinfo(name,age=20):
print("name:",name)
print("age:",age)
printinfo(name="anfly") [展示都能展示出来]
6.不定长参数
1).*args[展示是元组]
defprintinfo(*args):
print("args:",args)
printinfo(100,200,300,400)
2)**args[展示是k.v格式]
def printinfo(**kwargs):
print("kwargs:",kwargs)
printinfo(a=100,b=200,c=300,d=400)
7.参数位置顺序
def sun(a,*args,b=22,**kwargs):
print("a:",a) ==10
print("args:",args) ==(200,300)
print("b:",b) ==2
print("kwargs:",kwargs) =={‘m’:3,'n':4}
sun(100,200,300,b=2,m=3,n=4)
8.函数返回值
def fan(a,b):
return a+b
print(fan(1,2)) #3
9.匿名函数(lambda函数也叫匿名函数,即函数没有具体的名称)
案例一:
sum=lambda a,b:a+b
print(sum(1,2)) #3
案例二:
sum=lambda a,b:100
print(sum(1,2)) #100
1.局部变量
def jv():
a=3
print(a)
def jv1():
a=99
print(a)
jv() #3
jv1() #99
2.全局变量
def jv():
print(a)
def jv1():
a=99
print(a)
jv() #全局变量
jv1() #99
3.局部变量升成全局变量
def jv():
a=3
global b
b="我也是全局变量"
print(a)
def jv1():
a=99
print(a)
print(b)
jv() #3
jv1() #99 #“我也是全局变量”
相关问答
Q1: python中set()函数的用法
python中set()函数的用法如下:
工具/原料:台式电脑、Win10、Python3.6.5。
1、首先按下“Win+R”组合键,打开运行窗口,如下图所示。
2、在打开文本框输入“cmd”,点击确定,如下图所示。
3、在打开的cmd窗口中,输入:“python”,点击Enter键,如下图所示。
4、在Python环境中,输入:“x = set(('hi', 'hello', 'world'))”,点击Enter键,如下图所示。
5、在Python环境中,输入:“print(x)”,如下图所示。
6、点击Enter键,即可使用Python内置的set()函数创建一个集合对象,并打印出来,如下图所示。
Q2: python集合?
*事先说明:以下代码及结果来自本设备Python控制台,在不同设备上可能结果有区别,望自己尝试为妙
集合(set),是一种Python里的类(class),
集合类似于列表(list),可更改,可迭代(iterable),但是元素不重复
定义集合使用花括号{},例如:
s = {"apple", "banana", "strawberry", "watermelon"}
警告!!!如果使用空括号
a = {}
a.__class__
class 'dict'
a将成为一个字典
想要定义空集合,
请使用类名。
a = set()
类名定义也可以把迭代转换为集合:
b = set("abc")
b
{'a', 'b', 'c'}
但是,保存后它是无序的。
s
{'banana', 'watermelon', 'strawberry', 'apple'}
(结果仅供参考,在不同设备上略有不同)
下面介绍它的性质:
1. 可更改:
使用add(x)方法添加元素x:
s.add("lemon")
s
{'banana', 'strawberry', 'lemon', 'watermelon', 'apple'}
使用update(iter1, iter2, …)方法添加多个可迭代对象(如列表,元组(tuple),另一个集合)里的元素:
s.update(("orange", "grape"))
s
{'banana', 'strawberry', 'orange', 'lemon', 'watermelon', 'apple', 'grape'}
警告!!!如果使用字符串,字符串也会被迭代:
a = set()
a.update("apple")
a
{'a', 'p', 'e', 'l'}
使用remove(x)移除元素x,如果x不存在,则抛出KeyError错误
s.remove("lemon")
s
{'banana', 'strawberry', 'orange', 'watermelon', 'apple', 'grape'}
s.remove("cat")
Traceback (most recent call last):
File "stdin", line 1, in module
s.remove("cat")
KeyError: 'cat'
使用discard(x)移除元素x,不会发生错误
s.discard("grape")
s
{'banana', 'strawberry', 'orange', 'watermelon', 'apple'}
s.discard("dog")
s
{'banana', 'strawberry', 'orange', 'watermelon', 'apple'}
2. 可迭代:
for x in s:
... print(x)
banana
strawberry
orange
watermelon
apple
3. 可以使用 len 函数获取集合长度;
len(s)
5
可以使用 in 关键字检查一个元素是否在集合内,将返回逻辑值(bool):
"apple" in s
True
"candy" in s
False
4.集合具有不重复性,因此它会自动去重:
c = set("Hello World!")
c
{' ', 'e', 'l', 'H', '!', 'r', 'W', 'o', 'd'}
5. 集合的运算
d = set("abcdef")
e = set("efghij")
d
{'c', 'e', 'a', 'b', 'f', 'd'}
e
{'h', 'e', 'g', 'j', 'f', 'i'}
d - e # 集合d去掉集合e含有的元素,或者说集合d包含而集合e不包含的元素(不改变原集合)
{'a', 'b', 'd', 'c'}
d | e # 集合d,e的所有元素
{'c', 'e', 'h', 'g', 'a', 'b', 'j', 'f', 'd', 'i'}
d e # 集合d,e都包含的元素
{'f', 'e'}
d ^ e # 不同时出现在集合d, e中的元素
{'c', 'g', 'h', 'a', 'b', 'j', 'd', 'i'}
注意!!!
字符串、列表通用的+和*不适用于集合
"abc" + "def"
'abcdef'
[1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
d + e
Traceback (most recent call last):
File "stdin", line 1, in module
d + e
TypeError: unsupported operand type(s) for +: 'set' and 'set'
"a" * 5
'aaaaa'
[1] * 3
[1, 1, 1]
d*3
Traceback (most recent call last):
File "stdin", line 1, in module
d * 2
TypeError: unsupported operand type(s) for *: 'set' and 'int'
(作者的小观点:既然集合是不能重复的,那么乘以、重复是没有意义的)







