python学习记录——day9

先来一个小题
约瑟夫环:30个人围成一圈,依次报数,数到9那么第九个人就被弄死。。然后重新报数,最后剩下15个人都是基督徒,问,最开始怎么站的?

def main():
    num = 9
    list1 = [x for x in range(30)]
    list2 = [0]*30
    for i in range(15):
        list2[list1[num-1]] = 1
        list1 = list1[num:]+list1[:num-1]
    print(list2)

今天主要讲了四个数据类型

列表 元组 集合 字典

  • 列表:

sys.getrefcount(list1)得到对象引用技术的值

import sys
list1 = [0]*10
list2 = list1
list3 = list2
print(sys.getsizeof(list1))
print(sys.getrefcount(list1))
del list2
del list3
del list1[0]
print(sys.getrefcount(list1))
mylist = [x + y for x in 'ABCDE' for y in '1234567']
mylist = mylist+[20,30]
print(mylist)
str1 = 'dsag'
str1.upper()
  • 二维数组
def main():
    names = ['刘','关','张','赵','马']
    subject = ['语文','数学','python']
    scores = [[0]*len(subject) for _ in range(len(names))]
    print(id(scores[0][1]))
    print(id(scores[1]))
    for row ,name in enumerate(names):
        print('请输入%s的成绩'%name)
        for col,sub in enumerate(subject):
            (scores[row])[col] = input('%s'%sub)
    print(scores)
不可这样写scores = [[0] * len(subject)]*len(names)
这样只是创建5个列表的3个引用,地址是一样的,一变全变
  • 元组:

元组不可写,只可读
找出第二大的数

def second_max(x):
    (m1,m2) = (x[0],0) if x[0] > x[1] else (x[1],0)
    for index in range(2,len(x)):
        if x[index] > m1:
            m2 = m1
            m1 = x[index]
        elif x[index] > m2 and x[index]!=m1:
            m2 = x[index]
    return (m1,m2) if m2 != 0 else (m1,'没有第二大')
  • 集合

集合是无序的

def main():
    #集合是无序的
    list1 = [1,1,2,2,3,3,]
    print(list1)
    tuple1 = (1,1,2,2,3,3)
    print(tuple1)
    set1 = {1,1,3,3,2,2}
    print(set1)
    set1.add(5)
    set1.add(4)
    print(set1)
    set2 = {1,3,5,7,9}
    print(set2)
    set3 = set1.intersection(set2)#交集
    set3 = set1 & set2
    print(set3)
    set3 = set1.union(set2)#并集
    set3 = set1 | set2
    print(set3)
    set3 = set1.difference(set2)#差集
    set3 = set1 - set2
    print(set3)
    set3 = set2 - set1
    set3 = set2.difference(set1)
    print(set3)
    set3 = set1.symmetric_difference(set2)#对称差
    set3 = set1 ^ set2
    print(set3)
    for i in set2:
        print(i)
    # print(set2[2]) 集合没有序列
    print(set2.pop())
    if 3 in set2:
        set2.remove(3)
    print(set2)
    print(set2.issubset(set1))#判断是不是子集
    print(set2<=set1)
    print(set1.issuperset(set2))#判断是不是超集
    print(set2>=set1)
    set4 = {1,2}
    print(set1.issuperset(set4))
    set5 = {88,5,2,5,67,3,143,789,456,13,546,68}
    print(set5)
    print(set5.pop())
    print(sorted(set5))
  • 字典
def main():
    dict1 = {'name':'宋佳鑫','age': 22,'gender': True}
    print(dict1['name'])
    dict1['name'] = 'god'
    print(dict1['name'])
    # dict1 += {'tel':'18883362575'}
    dict1.update(hight=175,fav=['吃','喝','玩'])
    print(dict1.pop('gender'))
    print(dict1.popitem())
    dict1['name'] = None
    del dict1['age']
    for x in dict1:
        print(x,'---->',dict1[x])
    dict1.setdefault('motto','heheda')#设默认值

作业

1.杨辉三角
def yanghui_triangle(n):
    """
    生成指定行数的杨辉三角
    :param n: 行数
    :return: 返回一个杨辉三角的二维数组
    """
    list1 = [[1],[1,1]]
    for i in range(2,n):
        list2 = [1]
        for j in range(i-1):
            a = list1[i-1][j+1]+list1[i-1][j]
            list2.append(a)
        list2.append(1)
        list1.append(list2)
    return list1

def my_print(l):
    #输出二维数组
    for i in range(len(l)):
        for j in range(len(l[i])):
            print(l[i][j],end='\t')
        print()

if __name__ == '__main__':
    # my_print(yanghui_triangle(10))
    my_print(helix_array(6))
2.螺旋矩阵
def helix_array(num):
    list1 = [[0]*num for _ in range(num)]
    x = 0 # 初始行
    y = 0 # 初始列
    value = 1 # 初始值
    direction = 'r' # 初始方向
    circle = 1 # 初始圈数
    while True:
        # 向右
        if direction == 'r':
            list1[x][y] = value
            if y >= num - circle:
                direction = 'd'
                continue
            y += 1
        # 向下
        if direction == 'd':
            list1[x][y] = value
            if x >= num - circle:
                direction = 'l'
                continue
            x += 1
        #向左
        if direction == 'l':
            list1[x][y] = value
            if y <= circle-1:
                direction = 'u'
                continue
            y -= 1
        #向上
        if direction == 'u':
            list1[x][y] = value
            if x-1 <= circle:
                direction = 'r'
                circle += 1
            x -= 1
        value += 1
        if value > num**2:
            break
    return list1

小知识

之前说过python内存回收机制:

Java是GC 而python和ios则是objc

GC会暂停整个程序的运行,深入内存去寻找垃圾,然后释放掉
安卓由Java编写的,而Java释放内存时,所有线程都要暂停以便GC,这就是安卓卡顿和闪退的原因,,,以至于很多游戏都有很多,加速,无敌等大招效果,其实无敌的这段时间就是在释放内存。。。不过如今的Java已经改善的很多

而ios的objc则是用计数来管理内存,对象引用一次+1,解引用一次-1,计数为0就立即释放内存
这样内存释放就及时、平滑、时机可控,,,但也有一个缺点,那就是有内存泄漏,要时刻保持清晰的对象间联系

你可能感兴趣的:(python学习记录——day9)