1015day2:字典-元组-集合基础知识、列表生成式、冒泡排序、两列表zip取max、统计列表元素个数——去重、实现strip功能、求列表max和min、斐波那契、水仙花数(3种)

一、== 斐波那契数列 ==

1 1 2 3 5 8 13
x=1
y=1
for i in range(10):
    x,y = y,x+y
    print(x)

二、== 水仙花束 ==

方法一
for i in range(100,1000):
    strs = str(i)
    bai = strs[0]
    shi = strs[1]
    ge = strs[2]
    if eval('{}**3+ {}**3+ {}**3'.format(bai,shi,ge))==i:
        print(i)

方法二
for s in range(100,1000):
    ge = s%10
    shi = s%100//10
    bai = s//100
    if ge**3+shi**3+bai**3==s:
        print(s)

方法三
for ge in range(10):
    for shi in range(10):
        for bai in range(1,10):
            if ge**3+shi**3+bai**3==ge+shi*10+bai*100:
                print(ge+shi*10+bai*100)

三、求一个列表里的最大值和最小值

lists = [2,3,5,1,7,9,3,56,2]
max = lists[0]
for i in lists[1:]:
    if max<i:
        max = i
print(max)

四、编写代码实现strip的功能(用列表方法)

strs = 'tashiyigedashiren,tajiushiliuzongyuantt'
def hello(strs,h='all',s=' '):
    while h=='l' or h=='all': #左边或者全部
        if strs[0]==s:
             strs = strs[1:]
        else:
            break

    while h=='r' or h=='all': #右边或者全部
        if strs[-1]==s:
            strs=strs[:-1]
        else:
            break
    return strs
print(hello(strs,h='r',s='t'))  #去除右边的t

结果:
tashiyigedashiren,tajiushiliuzongyuan

五、编写程序对列表元素== 去重 ==

方法一:循环遍历
lists = [1,9,1,2,6,2,4,3,0,3,9,4,5]
n_l = []
for i in lists:
    if i not in n_l:
        n_l.append(i)
print(n_l)
结果:[1, 9, 2, 6, 4, 3, 0, 5]

方法二:利用集合去重
lists = [1,9,1,2,6,2,4,3,0,3,9,4,5]
print(list(set(lists)))

结果:[1, 9, 2, 6, 4, 3, 0, 5]

遍历列表元素
lists = [[1,2],[3,4]]
for a,b in lists:
    print(a,b)
    
结果:
1 2
3 4

六、统计列表中每个元素出现的个数

lists = [47,98,3,6,2,98,30,47,3]
n_l = []

for i in range(len(lists)):
    i = lists[i]
    flag = 0
    n = 0
    for a,b in n_l:
        if a==i:
            n_l[n] = [i,b+1]
            flag = 1
        n=n+1
    if flag==0:
        n_l.append([i, 1])
print(n_l)

结果:[[47, 2], [98, 2], [3, 2], [6, 1], [2, 1], [30, 1]]

七、九九乘法表程序

strs = ''
for i in range(1,10):
    for ii in range(1,i+1):
        strs+='{}x{}={} '.format(ii,i,i*ii)
    strs = strs+'\n'

八、如果有两个列表,他们都有10个元素,如何生成一个新的列表,对应下表使用这2个列表中值大的那个?== zip ==

#比如说列表1[1,2,3,4]列表3[2,1,2,2],得到的结果应为[2,2,3,4]

list1 = [1,4,3,7,2,4]
list2 = [1,2,4,5,2,8]
lists = []
for a,b in zip(list1,list2):  #将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的list(列表)
    n = a if a>b else b
    lists.append(n)
print(lists)

结果:[1, 4, 4, 7, 2, 8]

九、== 冒泡排序 ==:对列表元素进行排序

# #利用冒泡排序对列表进行排序
lists = [4,2,8,7,9,3,6,2]
# lists = [1,2,3,4,6,5]
# [2, 4, 7, 8, 3, 6, 2, 9]
# [2, 4, 7, 8, 3, 6, 2, 9]
for ii in range(len(lists)-1):
    flag = 0
    for i in range(len(lists)-1-ii):
            if lists[i]>lists[i+1]:
                lists[i],lists[i+1] = lists[i+1],lists[i]
                flag = 1
    if flag == 0:
        break
    print(lists)

结果:
[2, 4, 7, 8, 3, 6, 2, 9]
[2, 4, 7, 3, 6, 2, 8, 9]
[2, 4, 3, 6, 2, 7, 8, 9]
[2, 3, 4, 2, 6, 7, 8, 9]
[2, 3, 2, 4, 6, 7, 8, 9]
[2, 2, 3, 4, 6, 7, 8, 9]

十、冒泡排序

lists = [4,2,8,7,9,3,6,5]

for j in range(len(lists)-1):
    flag = 0
    for i in range(len(lists)-1-j):
            if lists[i]>lists[i+1]:
                lists[i],lists[i+1] = lists[i+1],lists[i]
                flag = 1
    if flag==0:
        break
print(lists)

结果:[2, 3, 4, 5, 6, 7, 8, 9]

十一、== 字典 ==:dict基础知识

1.键不可变,键不可重复
dicts = {'hello':'world',(1,2,3):899,'李白':[{12:33}]}
print(dicts)

2.字典出现重复的键会怎么样?
dicts = {'李白':'world',(1,2,3):899,'李白':[{12:33}]}
print(dicts)
结果:{'李白': [{12: 33}], (1, 2, 3): 899}   会自动合并,赋最后出现的值

3.增加
#方法一
dicts = {'hello':'world',(1,2,3):98,'李白':[{12:33}]}
dicts['你好'] = '杜甫'
print(dicts)

#方法二
dicts = {'hello':'world',(1,2,3):98,'李白':[{12:33}]}
dicts.update({'你好': '杜甫'})  #实际上放的是另一个字典,实际上是两个字典的合并
print(dicts)  #打印的是这个字典本身,是在原来的字典里面变化的,所以说字典是可变的

4.删除
根据字典的键
#方法一
dicts = {'hello':'world',(1,2,3):98,'李白':[{12:33}]}
a = dicts.pop('hello')    #弹出的是删除的键所对应的值
print(a,dicts)

#方法二
dicts = {'hello':'world',(1,2,3):98,'李白':[{12:33}]}
del dicts['hello']
print(dicts)

#方法三
dicts = {'hello':'world',(1,2,3):98,'李白':[{12:33}]}
print(dicts.clear())  #清空字典
结果:None

5.修改
dicts = {'hello':'world',(1,2,3):98,'李白':[{12:33}]}
dicts['hello'] = '柳宗元'
print(dicts)

6.查询
dicts = {'hello':'world',(1,2,3):98,'李白':[{12:33}]}
print(list(dicts.keys()),dicts.values())    #可以把字典转成列表进行取值,就可以用列表的方法了
print(dicts.items())

#打印所有键的值
dicts = {'hello':'world',(1,2,3):98,'李白':[{12:33}]}
for i in dicts.keys():
    print(i)

结果:
hello
(1, 2, 3)
李白
#打印所有的值
print(dicts.values())
结果:dict_values(['world', 98, [{12: 33}]])

7.字典遍历的两种方法
dicts = {'hello':'world',(1,2,3):98,'李白':[{12:33}]}
# 方法一(遍历出键值对)
for a,b in dicts.items():
    print(a,b)
结果:
hello world
(1, 2, 3) 98
李白 [{12: 33}]
#方法二
for a in dicts:   #直接遍历,遍历出来的是字典的键
    print(a,dicts[a])

结果:
hello world
(1, 2, 3) 98
李白 [{12: 33}]
print(dicts['hello'])
结果:
world

8.判断字典键里有没有这个值
dicts = {'hello':'world',(1,2,3):98,'李白':[{12:33}]}
if 'hello' in dicts:
    print("有!")

结果:有!

9.用字典统计字符串中每个字母出现的个数?
#第一种
strs = 'skcclfdlcgdc'
dicts = {}
for i in strs:
    dicts[i] = strs.count(i)    #引字母作为键dicts[i],统计次数

print(dicts)

结果:{'s': 1, 'k': 1, 'c': 4, 'l': 2, 'f': 1, 'd': 2, 'g': 1}


#第二种
strs = 'skcclfdlcgdc'
dicts = {}
for i in strs:
    if i not in dicts:   #判断取出的值是否在字典里面
        dicts[i] = 1
    else:
        dicts[i] = dicts[i] + 1  #加入的话,取出来再给它+1
print(dicts)

结果:{'s': 1, 'k': 1, 'c': 4, 'l': 2, 'f': 1, 'd': 2, 'g': 1}

十二、== 元组 ==:tuple基础知识

1.遍历
tuples = (1,2,3,4)
for i in tuples:
  print(i)
  
2.查
tuples = (1,2,3,4)
print(tuples[0])

3.类型
t = (1,)
print(type(t))
t = (1)
print(type(t))  #有运算优先级,出来就是整型1

4.元组里面放一个列表,能不能修改列表里面的值?
t = ([1,2,3])
t[0][0] = 5
print(t) #报错,因为元组是不可变元素,Python版本决定可不可以变,很遥远的可能会变,面试就回答直接报错

十三、== 集合 ==:set基础知识

1.遍历
sets = {2,2,2,4,4,3,7}
for i in sets:
    print(i)
    
sets = {2,2,2,4,4,3,7}
print(sets[0])


2.增
sets = {2,2,2,4,4,3,7}
sets.add(8)
print(sets)


3.删除
# 第一种
sets = {2,2,2,4,4,3,7}
sets.pop()
print(sets)

# 第二种remove(2)
sets = {2,2,2,4,4,3,7}
sets.remove(2)
print(sets)

# 第三种discard(7)
sets = {2,2,2,4,4,3,7}
sets.discard(7)
print(sets)



4.并集、交集、差集、补集

sets = {1,2,3}
set1 = {2,3,4}
# 并集
print(sets|set1)

# 交集
print(sets & set1)

# 差集
print(sets-set1)
print(set1-sets)

# 补集
print(sets^set1)

5.特殊情况
print(set([1,2,3]))   #结果:{1, 2, 3}
print(list(set([1,2,3]))   #结果:出错
print(tuple(list(set([1,2,3]))))   #结果:(1, 2, 3)

lists = str(tuple(list(set([1,2,3]))))
print(type(lists))
print(lists)
print(lists[2:])

十四、== 列表生成式 ==

print([x for x in range(1,101)])     #前边的x是放在里面的x,这是for x in range(1,101)对所放x的描述,生成1~100

print([x if x%2==0 else -x for x in range(1,101)])   #对里面的奇数取负值,两个条件就放到for x in range(1,101)的前边

print([x for x in range(1,101) if x %2==0])    #选出里面的偶数,一个条件就可以放到for x in range(1,101)的后边
1.
print([x for x in range(1,101)])     #前边的x是放在里面的x,这是for x in range(1,101)对所放x的描述,生成1~100
2.
print([x if x%2==0 else -x for x in range(1,101)])   #对里面的奇数取负值,两个条件就放到for x in range(1,101)的前边
3.
print([x for x in range(1,101) if x %2==0])    #选出里面的偶数,一个条件就可以放到for x in range(1,101)的后边
4.
#用列表生成式把[[1,2],[3,4],[5,6]]变成[1, 2, 3, 4, 5, 6]
lists = [[1,2],[3,4],[5,6]]
data = [x for y in lists for x in y]   #需要两个变量,先遍历这个[[1,2],[3,4],[5,6]]大列表,
                                # 遍历出的y就是这个[1,2]小列表,然后for x in y再遍历里面的小列表
                                #然后把遍历到的y放到一个列表里面
print(data)

结果:[1, 2, 3, 4, 5, 6]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ゆきな

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值