Talk about today do practice it, it's worth mentioning.
Today do is, without changing the original order, a list deduplication.
After this idiot thinking and operating three hours to complete the preparation of the following codes:
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)
Well of course, the first thought is to use a certain count () to count, if there are duplicate, directly remove () away. However, this idea is not perfect, because changing the length of the list, corresponding to a fixed element also changes the index value, so that, by traversing the index value to s, where the jumping occurs. This situation will jump consecutive sufficient number of elements at a same time appears.
This imperfect method can be performed by "processing" perfect, that is, after repeated sufficiently many times, we can achieve what we want to re-effect, so while the outer loop is selected, the number of cycles of the original list is selected most elements of the "maximum" appears.
Since then, I asked a friend the great God, people come to me to throw this directly:
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)
Just a few lines to get, do not need to count, just look at the elements are the same on it.
This idiot can not help but issued a sigh, experienced sophisticate is to force, although both realize the function, but is still as simple as possible.
To practice more of it.