早教吧作业答案频道 -->其他-->
Python怎么除去list中的重复值?以下是不正确的,结果是[1,1,2,3,4,4,5],为什么?a=[1,1,1,1,2,3,3,3,4,4,4,4,5,5]defune(lst):foriinlst:iflst.count(i)>1:lst.remove(i)printlstune(a)谢谢大家,尤其
题目详情
Python 怎么除去list中的重复值?以下是不正确的,结果是[1, 1, 2, 3, 4, 4, 5],为什么?
a=[1,1,1,1,2,3,3,3,4,4,4,4,5,5]
def une(lst):
for i in lst:
if lst.count(i) > 1:
lst.remove(i)
print lst
une(a)
谢谢大家,尤其是1楼,写了这么多。
找到个正确答案:
def une(lst):
lst.sort()
last=lst[-1]
for i in range(len(lst)-2,-1,-1):
if lst.count(lst[i])> 1:
del lst[i]
print lst
a=[1,1,1,1,2,3,3,3,4,4,4,4,5,5]
def une(lst):
for i in lst:
if lst.count(i) > 1:
lst.remove(i)
print lst
une(a)
谢谢大家,尤其是1楼,写了这么多。
找到个正确答案:
def une(lst):
lst.sort()
last=lst[-1]
for i in range(len(lst)-2,-1,-1):
if lst.count(lst[i])> 1:
del lst[i]
print lst
▼优质解答
答案和解析
需要执行两次une(a)才能去除
改了一下代码,自己跑一下下面的两端代码就应该知道为什么了,效果是一样的
>>>a=[1,1,1,1,2,3,3,3,4,4,4,4,5,5]
>>>def une(lst):
for i in lst:
print 'i=',i
print 'count('+str(i)+')='+str(lst.count(i))
if lst.count(i) > 1:
lst.remove(i)
print 'a=',a
print '-----------------------------------------'
>>>une(a)
>>>a=[1,1,1,1,2,3,3,3,4,4,4,4,5,5]
>>>def une(lst):
num = len(lst)
n=0
for n in range(num):
#print 'n=',n
i = lst[n]
print 'i=',i
print 'count('+str(i)+')='+str(lst.count(i))
if lst.count(i) > 1:
lst.remove(i)
print 'a=',a
print '-----------------------------------------'
>>>une(a)
怎么改une不用我教了吧
改了一下代码,自己跑一下下面的两端代码就应该知道为什么了,效果是一样的
>>>a=[1,1,1,1,2,3,3,3,4,4,4,4,5,5]
>>>def une(lst):
for i in lst:
print 'i=',i
print 'count('+str(i)+')='+str(lst.count(i))
if lst.count(i) > 1:
lst.remove(i)
print 'a=',a
print '-----------------------------------------'
>>>une(a)
>>>a=[1,1,1,1,2,3,3,3,4,4,4,4,5,5]
>>>def une(lst):
num = len(lst)
n=0
for n in range(num):
#print 'n=',n
i = lst[n]
print 'i=',i
print 'count('+str(i)+')='+str(lst.count(i))
if lst.count(i) > 1:
lst.remove(i)
print 'a=',a
print '-----------------------------------------'
>>>une(a)
怎么改une不用我教了吧
看了 Python怎么除去list...的网友还看了以下:
设集合s={0 1 2 3 4 5} A是s的一个子集当x属於A 时 若有x-1不属於A且x+1不 2020-04-06 …
﹛a²—b²=4﹛25/a²+9/b²=4两方程连立的解为何不取分数?┏a²—b²=4┃┗25/a 2020-04-26 …
已知函数f(x)=根号下(1+2^x+a*4^x)的定义域为(-无穷大,1],求a的取值范围?(其 2020-05-13 …
急.做好了加...下列数值哪些是不等式x+3>6的解?哪些不是?-4,-2.5,0,1,2.5,3 2020-05-14 …
已知函数f(x)=cos^2x/2-sinx/2cosx/2-1/2,若f(a)=3*根号2/10 2020-05-16 …
简单方程化简(b/a)4-4(b/a)2-4=0求(b/a)的值重新写一遍题目是不是没看清啊。(b 2020-05-17 …
简单英语发音,他是什么?是的它是.不是它不是问句IsitaA(1声)丝A(1声)特(4声)回答2种 2020-07-14 …
Python怎么除去list中的重复值?以下是不正确的,结果是[1,1,2,3,4,4,5],为什 2020-07-17 …
若分式方程3/x-2=a/x=4/x(x-2)有增根,则增根可能为?我知道答案是3/(x-2)=a 2020-07-30 …
三角函数sin(-(180°/2)+a)用诱导公式k(180°/2)+a奇变偶不变符号看象限,可化 2020-08-02 …