The bumpy python function, set

function

Role: to implement a function, function is understood as a tool, the tool used to have problems with

Advantages: more convenient function, multiplexing, a plurality of scenes can be used in

1, with parameters of the function

name='lzs-nice'
nane.split('-')   ##以-进行分割

2, with a return value of the function

def add_sum(num):           ##定义一个函数,并带一个参数
    count=0
    for i in range(1,num):
        count+=1
        return count
    res=add_sum(101)    ##计算1到100相加的结果

set

Set: a set of data collection, there are a
large opening and a plurality of brackets with a comma elements, set (hash table) is disordered, deduplication

##集合和集合之间会有:交集&,并集|,补集^,差集-
s1={'lzs','nice','good'}
s2={'lalala','lzs','huhuhu'}
print(s1&s2)    ##lzs
print(s1|s2)    ##lzs nice good lalala huhuhu
print(s1^s2)    ##nice good lalala huhuhu
print(s1-s2)    ##nice good

Built-in methods for the collection:

s={'lzs','nice','huhuhu','hahaha'}
 ##往数组里添加数据
s.add('lalala')                  ##lzs nice huhuhu hahaha lalala
#去除数组中的某些数据,丢掉的如果是没有的则报错
s.remove('huhuhu')               ##lzs nice hahaha
#去除数组中的某些数据,丢掉的如果是没有的不报错
s.discard('444')                 ##lzs nice huhuhu hahaha
##清空数组元素
s.clear()                        ##set()
##复制数组到另外一个数组
s1=s.copy()                      ##s1={'lzs','nice','huhuhu','hahaha'}

Sequence type

Sequence type, that data type with an index of: strings, lists, ancestral

#字符串、列表、元祖共有的方法
s='lzs nice'
##求字符串的长度
print(len(s))    ##8
##判断lzs是否在字符串中
print('lzs' in s)  ##ture
##判断good是否不在字符串中
print('good' not in name) ##ture
##把数组的元素打印出来
for i in s:
    print(1,end=' ')       ##l z s  n i c e

List: a plurality of elements within the square brackets separated by commas

##将列表的中元素用字符串的方式显示出来
s=['lzs','nice','huhuhu']
s1=set(s)
s=list(s1)
print(s1)
#列表的内置方法
##将lalalla追加到列表的最后
s1.append('lalalala')     ##['lzs','nice','huhuhu','lalala']
##按照索引删除
del s1[0]                 ##['nice','huhuhu']
##删除列表
s.clear()
##排序
s=[1,9,5,7,6]
s.sort()                   ##[1,5,6,7,9]
##翻转
s.reverser()               ##[6,7,5,9,1]

Ganso: in brackets is to replace the list of parentheses, and then he did not have built-in method

##只可取不可更改
tu = (1,3,2,100,4,5)
print(tu[0])       ##1

Dictionary Type

The curly braces (Key (descriptive sense): value) stored in the form of a comma-separated key data

s={'name':'lzs','height':184,'weight':130,'hobby':['play','sing']}
##取值
print(s['name'])             ##lzs
print(s['hobby'][1])         ##play
##如果没有key。则取none,可以自定义没有值能取到的值
print(s.get(name1),wow)       ##wow
print(s.get('hebby'[2]))      ##play

Cycle values:

s={'name':'lzs','height':184,'weight':130,'hobby':['play','sing']}
##取出来的是key的值
for i in s:
    print(i)         
##取出来的是key后面对应的值
for i in s.values():
    print(i)
##取出来的是所有的值
for i in s.items():
    print(i)

Delete value

s={'name':'lzs','height':184,'weight':130,'hobby':['play','sing']}
s.setdefault('weight',130)
print(s)

jieba Hyogo

jieba word generally used for library

res = jieba.lcut('中华人民共和国是一个伟大的国家')  # 第一个就是尽量长,主要掌握这一个
print(res)
res = jieba.lcut_for_search('中华人民共和国是一个伟大的国家')  # 第二个就是把第一个的长的单词继续切
print(res)

Guess you like

Origin www.cnblogs.com/lzss/p/11209549.html