【发布时间】:2020-05-25 06:07:05
【问题描述】:
问题:给定一个大小为 N 的列表,找出其中的唯一元素 设置到给定的索引 K。 输入 1:N:集合的大小 - N 输入 2:K:需要找出唯一元素的部分索引
def dst(N,K,l):
print(l)
c=1
for i in range(1,K):
a=l[i]
j=0
for j in range(i):
if(a==l[j]):
break
if(i==j+1):
c=c+1
print("value of k =")
print(K)
print("Count of distinct elements")
print(c)
l=[]
n=int(input("Enter the limit"))
k=int(input("Enter the value of k"))
for i in range(1,n+1):
a=int(input("Enter the element"))
l.append(a)
dst(n,k,l)
Enter the limit 6
Enter the value of k 4
Enter the element 1
Enter the element 1
Enter the element 1
Enter the element 3
Enter the element 5
Enter the element 8
[1, 1, 1, 3, 5, 8]
value of k = 4
Count of distinct elements
3
原始输出:2(1 和 3 是不同的元素,直到第 4 个元素)
谁能帮我解决这个问题?
【问题讨论】:
-
试试这个
len(set(a[:k]))
标签: python list indexing unique