Functions, sets, lists, dictionaries, jieba organize knowledge library

function

Function is used to: implement a function, function as a tool to understand the problems encountered can use this tool to take over

Benefits function: function more convenient, multiplexing, can be used in multiple scenes

Function definition: def function name (parameter)

Using the Recall function to solve the circle of pi

def get_pi(num):
    import random

    count = 0
    for i in range(num):
        x, y = random.random(), random.random()
        dist = pow((x - 0) ** 2 + (y - 0) ** 2, 0.5)

        if dist < 1:
            count += 1

    print(4 * count / num)

get_pi(10)
get_pi(100)
get_pi(1000)
get_pi(10000)
get_pi(100000)

Function into a function with parameters and return value of the function

def add_sum(num):#  num就是一个参数

    count = 0
    for i in range(1, num):
        count += i

    return count  #返回值, 是count


res1= add_sum(101)  #101就是参数的值传给函数的参数
print('res1:',res1)
res2 = add_sum(1001)
print('res2:',res2)
res3 = add_sum(10001)
print('res3:',res3)

set

What is the set: A collection is a set of data

Defining a set (the set of definitions can be set (), you can also be defined directly enclosed by {})

s1={'nick','handsome',121,11}#
s2 = set('nick')  # iterable(序列类型)
s3 = set(['1', 2, 3])

#输出结果{1.0, 'nick', 'dsb', 1231, 'wuhao', 'handsome'}
        {'n', 'i', 'c', 'k'}
        {'1', 2, 3}

A set of operators

Operators and Applications description
S | T It returns a new set, comprising all of the elements in the set S and T
S - T It returns a new set, comprising a set of elements in S but not in T
S & T It returns a new collection, including at the same time elements of the set S and T
S ^ T It returns a new set, comprising a set S and T non-identical elements
S <= T或S < T Returns True / False, determining the subset of the relationship between S and T
S >= T或S > T Returns True / False, comprising determining the relationship between S and T

Processing methods for the collection

The method of operation or function description
S.add(x) If x is not in the set S, the increased S x
S.discard(x) S removal elements x, if x is not in the set S, not given
S.remove(x) S removal elements x, if x is not in the set S, the abnormality is generated KeyError
S.clear() Remove all elements of S
S.pop() Returns a random element of S, S update, if S is empty produce abnormal KeyError
S.copy() Returns a copy of the set S
only the (S) Returns the number of elements in the set S
x in S Analyzing elements S x, x in the set S, returns True, otherwise return False
x not in S Analyzing elements S x, x S is not set, it returns False, otherwise True
set(x) The other type of variable x into a set type

sequence

There will be a serial number sequence, the same as the index,

Type sequence, with an index of the data type is: character, a list, ancestral

Comma-separated list of intermediate brackets

E.g:

lt = ["nick","handsome",1,1]
print(lt)

A list of defined functions

Function or method description
ls[i] = x Alternatively the i-th list element is ls x
ls[i: j: k] = lt After replacing the list with ls lt element sections corresponding sublist
ls the [i] I deleted the first element of the list ls
del ls[i: j: k] Delete the first list ls i to j-k is a long step to the elements
ls += lt Ls update list, the list will be added to the list of elements lt ls of
ls = n * Update the list ls, whose elements are repeated n times

List of definitions

Function or method description
ls.append(x) In the list of the last ls add an element x
ls.clear() Delete all of the elements in the list ls
ls.copy() Generate a new list, all elements assigned ls
ls.insert(i,x) Adding elements x i of the list of ls
ls.pop(i) The i-th position of the list element ls removed and remove the element
ls.remove(x) The first element in the list that appears in x delete ls
ls.reverse() Ls will list the elements in reverse
# 清除
lt.clear()

#
lt2 = lt.copy()


# 排序
lt = [1,3,2,100,4,5]
lt.sort()
print(lt)

# 翻转
lt.reverse()
print(lt)


# 元组
# 元组就是把列表的中括号换成小括号,然后他没有内置方法
lt = [1,3,2,100,4,5]

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

Basic statistical methods

A set of data input by the user (a user input), and the median calculation data, mean, variance, and other operations required o

nums = []

while True:
    num1 = input('请输入你需要输入的数字(输入 q 退出):')
    if num1 == 'q':
        break
    nums.append(int(num1))


def get_median(nums):  # ['123', '32', '123', '234']
    #                       0      1      2    3
    nums.sort()

    nums_len = len(nums)
    if nums_len % 2 == 0:
        print((nums[int(nums_len / 2 - 1)] + nums[int(nums_len / 2)]) / 2)
    else:                           # ['123', '32', '123']   # 1
        print(nums[nums_len // 2])  # ['123', '32', '123','123', '32', '123','123']  # 3


# get_median(nums)
def get_sum(nums):  # ['123', '123', '213', '234', '98234']
    count = 0
    for i in nums:
        count += int(i)

    return count

# count = get_sum(nums)
# print('count:',count)


def get_average(nums):
    count = get_sum(nums)
    average_count = count/len(nums)

    return average_count

# average_count = get_average(nums)
# print(average_count)

def get_variance(nums):
    average_count = get_average(nums)

    variance_count = 0
    for i in nums:
        variance_count += pow(i-average_count,2)

    return variance_count

variance_count = get_variance(nums)
print(variance_count)

Dictionary Type

Dictionary definition of the type can also be defined directly dict

d = { "China": "Beijing", "America": "Washington"}

Dictionary handler method

Function or method description
of d [k] D button to delete the dictionary data corresponding to the value k
k in d Determine whether the key k d in the dictionary, if in return True, otherwise False
d.keys() Return all the key information in the dictionary d
d.values() All the information returned value in the dictionary d
d.items() D return dictionary of all the key information

A dictionary manipulation functions and methods

Function or method description
d.get(k, ) Bond k is present, the corresponding value is returned, the return value is not
d.pop(k, ) Bond k is present, the corresponding values ​​are fetched, the return value is not
d.popitem () D taken randomly from the dictionary in a key-value pairs in the form of tuples to return
d.clear() Remove all key-value pairs
len (d) Returns the number of elements in the dictionary d

jieba Hyogo

jieba word generally used for library

Learn three major statement

# 马尔可夫模型(两层)

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


res = jieba.lcut_for_search('中华人民共和国是一个伟大的国家')  # 第二个就是把第一个的长的单词继续切
print(res)


res = jieba.lcut('中华人民共和国是一个伟大的国家',cut_all=True) # 把所有的可能全部切出来
print(res)

Guess you like

Origin www.cnblogs.com/quyang46/p/11209386.html