【问题标题】:Program to find out unique elements in a list up to a particular index找出列表中直到特定索引的唯一元素的程序
【发布时间】: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


【解决方案1】:

让我们想想dst() 函数应该实现什么。鉴于您的问题设置,该功能应该

  1. 接收列表
  2. 接收索引限制

给定这些信息,函数应该输出输入列表中的唯一元素,直到指定的索引限制。我要说明的一点是N 参数对于dst() 来说是不必要的;仅在构建输入列表以输入dst() 时才有必要,这发生在函数之外。

判断一个元素的唯一性在功能上等同于判断是否有重复。解决这个问题的一种方法是声明一些输出列表res,并在我们循环输入列表直到指定索引时,每当我们看到一个新元素时追加到res。 (在这里,我将 'up to' 解释为排他性的,但您可以通过执行类似 range(idx + 1) 而不是 range(idx) 的操作轻松编辑函数以包含 idx

def unique_elems(idx, lst):
    res = []
    for i in range(idx):
        if lst[i] not in res:
            res.append(lst[i])
    return res

更简单的是,如果您不关心函数返回的唯一元素的顺序,我们可以简单地将子列表转换为集合,这样可以有效地消除所有重复元素。

def unique_elems_2(idx, lst):
    return list(set(lst[:idx]))

编辑:看来我可能误解了你的问题。如果您想要的是唯一元素的数量而不是唯一元素本身,您可以通过返回 len() 来简单地修改上面的函数。例如,

def n_unique_elems(idx, lst):
    return len(set(lst[:idx]))

【讨论】:

  • 这是一个很好的答案,但这并不能回答 OP 问题。这导致了替代解决方案。
  • @Pygirl 这是一个有效的批评。尽管如此,我仍然会在这里留下我的答案以供 OP 参考。我希望让 OP 看到比他们当前的实现更优化的解决方案是有价值的。在某种程度上,它很好地补充了您的答案。
【解决方案2】:

您的代码逻辑错误。而不是计算唯一元素计数重复值,然后从K中减去

def dst(N,K,l):

    print(l)
    d = 0 #<--- Here
    for i in range(1,K):
       a=l[i]
       j=0
       for j in range(i):
            if(a==l[j]):
                d=d+1    #<---- Here            
                break

    print("value of k =")
    print(K)
    print("Count of distinct elements")
    print(K-d)

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)

结果1:

Enter the limit6
Enter the value of k3
Enter the element1
Enter the element2
Enter the element3
Enter the element4
Enter the element5
Enter the element6
[1, 2, 3, 4, 5, 6]
value of k =
3
Count of distinct elements
3

结果 2:

Enter the limit6
Enter the value of k4
Enter the element1
Enter the element1
Enter the element1
Enter the element3
Enter the element5
Enter the element8
[1, 1, 1, 3, 5, 8]
value of k =
4
Count of distinct elements
2

【讨论】:

  • 您可以通过接受有效的解决方案来结束问题吗?
猜你喜欢
  • 2019-11-11
  • 1970-01-01
  • 2021-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多