经验还是很重要的。

Posted marvelous-dong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了经验还是很重要的。相关的知识,希望对你有一定的参考价值。

  说说今天做的练习吧,还是值得一说的。

  今天做的是,在不改变原有顺序的情况下,进行列表去重。

  本笨蛋经过三个小时的思考与操作,完成了下面代码编写:

chong = ["a","a","a","a","a","a","a","a","a","a","a","a","a","a","b","b","b","b","b","b","s","a","a","a","a","a","a","a","a","c","c","c","c","c","c","c","c","c","c","s","b","s"]
chong_T = chong[::-1]
k = 0
a = []
for i in range(0,len(chong)):
    b = chong.count(chong[i])
    a.append(b)
while k <= max(a):
    s = 0
    t = 0
    while s < len(chong_T):
        if chong_T.count(chong_T[s]) > 1:
            chong_T.remove(chong_T[s])
            s += 1
        else:
            s += 1
    while t < len(chong_T):
        if chong_T.count(chong_T[t]) > 1:
            chong_T.remove(chong_T[t])
            t += 1
        else:
            t += 1
    k += 1
chong_T = chong_T[::-1]
print(chong_T)

    当然嘛,首先想到的一定是利用 count()去计数,如果有重复的,直接 remove()掉。但是,这种思路是不完善的,因为改变了列表的长度,一个固定元素对应的索引值也就改变了,这样,用 s 去遍历索引值,就会发生跳跃的情况。这种跳跃的情况会在某一相同元素连续出现充分多的时候出现。

    这种不完善的方法可以通过“加工”进行完善,那就是重复充分多的次数后,就能达到我们想要的去重效果,于是选定了外层 while 循环,循环次数选定了原列表出现元素最多的那个“最大值”。

    此后,我问了一个大神朋友,人家直接给我扔过来这个:

    

chong = ["a","a","a","a","a","a","a","a","a","a","a","a","a","a","b","b","b","b","b","b","s","a","a","a","a","a","a","a","a","c","c","c","c","c","c","c","c","c","c","s","b","s"]
for x in chong:
    for y in range(len(chong)-1,chong.index(x),-1):
        if chong[y] == x:
            chong.pop(y)
print(chong)

    就几行就搞定了,不需要计数,只需要看元素是否相同就可以了。

    本笨蛋不禁发出了感叹,经验丰富的老油条就是给力,虽说都实现了功能,but 还是越简单越好。

    多多练习吧。

以上是关于经验还是很重要的。的主要内容,如果未能解决你的问题,请参考以下文章

第一次个人作业(心得经验)

智能门锁测试程序和PCB板线路通断检测程序经验总结

521灵魂拷问:代码重要还是我重要?

熟悉 JUnit 测试

业务重要还是技术和代码质量重要

想进大厂做前端,代码质量和项目经验需要到这个程度才行...