侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130557 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

Python3内置容器之例子

2023-12-22 星期五 / 0 评论 / 0 点赞 / 75 阅读 / 5414 字

1.归并排序 L1 = [1, 2, 4, 6, 9]L2 = [2, 3, 5, 7, 8]L3 = []for x in L1: while len(L2) > 0: if x

1.归并排序

L1 = [1, 2, 4, 6, 9]L2 = [2, 3, 5, 7, 8]L3 = []for x in L1:    while len(L2) > 0:        if x > L2[0]:            L3.append(L2.pop(0))        else:            L3.append(x)            break    if len(L2) == 0:        L3.append(x)        L3.extend(L2)print(L3)

2.按单词反转字符串

s = 'I Love You'tmp = s.split()s2 = ' '.join(tmp[::-1])print(s2)

3.找出列表不重复元素并按原来的顺序

lst = [1, 2, 1, 3, 1, 2, 5]lst2 = []lst3 = []tmp = []# count方法for i in lst:    if lst.count(i) > 1:        # 删除所有i元素        pass    else:        lst2.append(i)print(lst2)# in方法for x in lst:    if x not in tmp:        lst3.append(x)        tmp.append(x)    else:        if x in lst3:            lst3.remove(x)        print(lst3)

4.查找一个列表中的最大值

lst = [1, 2, 1, 3, 2, 5,4]if len(lst) == 0:    print('List is null.')else:    max = lst[0]    for i in lst:        if i > max:            max = i    print(max)

5.字符串转化为数字

# 传统s = '-123.12345'x = 0  # resultsign = 0  # sign of .loc = 0  # location neg = 0  # sign of negativenum = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '-']i = 0n = 0m = 0for i in s:    if i in num:        if num.index(i) == 10:            sign = 1        elif num.index(i) == 11:            neg = 1        else:            if sign:                loc +=1                x = x + num.index(i) * (0.1 ** loc)            else:                x = x * 10 + num.index(i)if neg:    x *= -1print(x)# pythons2 = '-1234.123'result = 0mloc = 0integers, *decimals = s2.split('.')*nega, integer = integers.partition('-')if len(decimals) == 0:    decimal = '0'else:    decimal = decimals[0]for n in integer:    result = result * 10 + num.index(n)for m in decimal:    mloc += 1    result = result + num.index(m) * (0.1 **mloc)if len(nega) == 2:    result *= -1print(result)

 PS:

        partition ==> rpartition

6.不适用组合数打印杨辉三角

lst = []for n in range(1,11): # 从第1行开始,n就是第几行    if n == 1:        lst.append([1])    elif n == 2:        lst.append([1, 1])    else:        sub_lst = [1]        for k in range(1, n-1): # 从第0列开始,k从第1列到n-1            sub_lst.append(lst[n-2][k-1] + lst[n-2][k])        sub_lst.append(1)        lst.append(sub_lst) # 增加第n行的列表        for s in lst:    print(' '.join(str(x) for x in s))

7.四则运算的括号匹配

exp = '3 * {3 +[(2 -3) * (4+5)]}'brackets = (    ('(', ')'),     ('[', ']'),     ('{', '}'))stack = []for c in exp:    if c in (t[0] for t in brackets):        stack.append(c)    elif c in (t[1] for t in brackets):        if len(stack) == 0:            print('1.error,no enough left brackets {}'.format(c))            break        left = [t[0] for t in brackets if c == t[1]]        if stack.pop() != left[0]:                            print('2.error,no enough right brackets {}'.format(c))            breakelse:    if len(stack) == 0:        print('ok')    else:        print('3.error, unnecessary brackets {}'.format(stack))

8.不带括号的四则运算转化为前缀表达式

exp = '2 * 3 + 1 - 4 / 2 * 7 - 1' # 6 +1 - 14 -1 => -8symbols = {'+': 0, '-': 0, '*': 1, '/': 1}exp = exp.split()stack = []for item in exp:    if item not in symbols.keys():  # 数字直接入栈        stack.append(item)    else:        while len(stack) > 1 and symbols[stack[-2]] >= symbols[item]:            # 栈内有元素,且栈内上层符号优先级大于等于入栈符号            r = stack.pop()            s = stack.pop()            l = stack.pop()            t = {'root':s, 'left':l, 'right':r} #取出上三个,做树            stack.append(t) # 入栈        stack.append(item) #第一个入栈符号,直接入栈|已经做树,入栈# 继续做树while len(stack) > 2:    r = stack.pop()    s = stack.pop()    l = stack.pop()    t = {'root':s, 'left':l, 'right':r}    stack.append(t)    ret = stack.pop()import pprintpprint.pprint(ret)

广告 广告

评论区