python14集合和类初识

集合set,可以列表,字母元素,字符串,元组元素都可以转化为集合,可以去重

>>> set([1,2,3])
{1, 2, 3}
>>> set("abc")
{'b', 'c', 'a'}
>>> set((1,2,3))
{1, 2, 3}
>>> s=set("abcdacds")
>>> s
{'s', 'b', 'c', 'a', 'd'}
>>>set可以自动去重,set中add和update的区别,add是直接添加里面这个元素,update是添加进去每一个元素和整体这一个元素

>>> s.add("efg")
>>> s
{'s', 'b', 'efg', 'c', 'a', 'd'}
>>> s.update("efg")
>>> s
{'s', 'f', 'b', 'efg', 'c', 'a', 'e', 'd', 'g'}
>>>集合是可以遍历的

>>> for i in s:print(i)
...
s
f
b
efg
c
a
e
d
g
>>>

>>> s.remove("d")
>>> s
{'s', 'f', 'b', 'efg', 'c', 'a', 'e', 'g'}
>>>

>>> list(s)
['s', 'f', 'b', 'efg', 'c', 'a', 'e', 'g']

目前是给列表,如果想改变元素,对集合元素的修改,先改成list,再改成set

>>> l=list(s)
>>> l
['s', 'f', 'b', 'efg', 'c', 'a', 'e', 'g']
>>> l[0]=1
>>> l
[1, 'f', 'b', 'efg', 'c', 'a', 'e', 'g']
>>> set(l)
{1, 'efg', 'c', 'a', 'f', 'e', 'b', 'g'}

>>> s=set("abcd")
>>> d=set("cdef")
>>> s&d
{'c', 'd'}
>>> s|d
{'e', 'a', 'c', 'f', 'b', 'd'}
>>> s-d
{'a', 'b'}
>>> d-s
{'e', 'f'}
>>>集合是无序的

练习题目

有2个句子

I am a  boy

I am 18 years old

1)两个句子中一共出现了多少个单词

2)求一个下两个句子中相同的单词的个数

3)不同单词的个数

s1="I am a boy"
s2="I am 18 years old"
word_list1=s1.split()
word_list2=s2.split()
s=set(word_list1)
s.update(word_list2)
len(s)
---------------
s1=set(word_list1)
s2=set(word_list2)
s1&s2
len(s1&s2)
s1|s2
---------------

s3=s1-s2
s4=s2-s1
s3.update(s4)
len(s3)


插播一条
set集合有2中添加方法,一种是add一种是update
区别是啥呢?
add 是将这个元素整体添加到集合中,update是将这个单词拆分成个体添加到集合中

>>> s
{'a', 'c', 'b', 'd'}
>>> s.add("cold")
>>> s
{'a', 'c', 'd', 'b', 'cold'}
>>> s.update("cold")
>>> s
{'o', 'a', 'c', 'l', 'd', 'b', 'cold'}
>>>

>>> import time
>>> time.time()
1610024242.708827
>>> time.time()
1610024252.4945571
>>> t1=time.time()
>>> t2=time.time()
>>> t2-t1
5.330370903015137
>>>
>>> time.localtime()  #时间戳
time.struct_time(tm_year=2021, tm_mon=1, tm_mday=7, tm_hour=20, tm_min=58, tm_sec=7, tm_wday=3, tm_yday=7, tm_isdst=0)
>>> time.localtime()[0]  #年
2021
>>> time.localtime()[1]   #月
1
>>> time.localtime()[2]  #日
7

>>> time.localtime()[3]
21
>>> time.localtime()[4]
0
>>> time.localtime()[5]
19

>>> t=time.localtime()

>>> str(t[0])+"年"+str(t[1])+"月"+str(t[2])+"日"
'2021年1月7日'

>>> date=str(t[0])+"年"+str(t[1])+"月"+str(t[2])+"日"
>>> date
'2021年1月7日'
>>> time=str(t[3])+"-"+str(t[4])+"-"+str(t[5])
>>> time
'21-6-41'
>>> date+" "+time
'2021年1月7日 21-6-41'
两个日期详见的时间差

import datetime

#求两个日期间的天数差

d1=(datetime.datetime(2015,7,5))

d2=(datetime.datetime(2015,8,26))

print((d2-d1).days)

#计算日期

#coding=utf-8

from datetime import datetime

from datetime import timedelta

now=datetime.now()

#设置100天前的时间间隔

delta=timedelta(days=-100)

#得到100天前的时间

oldTime=now+delta

print(oldTime.strftime("%Y-%m-%d"))

类和实例对象

类:就是一类,比如蛋类

实例对象:鹌鹑蛋,鸡蛋,鸽子蛋,具体到实际例子的对象

class Bird:  #类只能有一个

>>> class Bird:
... def __init__(self,length,height,color):  #如果不设置参数是不需要有init
... self.length=length  #self.XX是实例变量
... self.height=height
... self.color=color
...
>>> b=Bird(10,20,"red")  #实例可以有多个,相互独立
>>> b
<__main__.Bird object at 0x0000025A15FC0A90>
>>> c=Bird(12,22,"yellow")
>>> c


<__main__.Bird object at 0x0000025A15FC0B00>
>>> b.color
'red'
>>> c.color
'yellow'
>>>

#类的定义在内存中某个地址存储,且只有一份
类变量在类定义的内存中存储,且只有一份
class Bird:
  count=0
  name="COld" #全局变量
#构造方法:主要设定示例中的参数
def __init__(self,length,height,color):
self.length=length
self.height=height
self.color=color
     Bird.count+=1 #类变量
#实例方法self
def get_color(self):
return self.color
def get_length(self):
return self.length
def get_height(self):
return self.height
#你生成一个实例,那么实例就保存在内存的某个地址
b=Bird(10,20,"red")
c=Bird(11,22,"yellow") #b和c是2个实例,在内存中各自独立保存,互不影响
print(b.get_color())
print(b.get_height())
print(b.get_length())
b.color="black"
print(b.color)
print(c.color)
print(Bird.count) #类变量 全局生效

原理:类(类变量,方法)在内存中只有一个地方存储,那么类中定义的方法也在那个地方储存,

当实例想调用某个方法时,需要把实例对象传递给类中定义的方法,才可以使用。

方法:类里面定义的函数

函数:类外面定义的函数

实例变量:类方法里面定义的带有self.XX变量

python默认把实例地址,自动传递给方法中的self,这样久可以找到不同实例中的实例变量





原文地址:https://www.cnblogs.com/JacquelineQA/p/14244384.html