Python
 Computer >> コンピューター >  >> プログラミング >> Python

リストから一意の値を出力するPythonプログラム


リストが与えられた場合、私たちのタスクはすべての一意の番号を印刷することです。

Input:A = [1, 2, 3, 4, 2, 1, 9]
Unique list is [1, 2, 3, 4, 9]

アルゴリズム

Step 1: Create user input list.
Step 2: Create an empty list.
Step 3: Traverse all elements in the list.
Step 4: Check the unique element is present or not.
Step 5: Append unique element one by one into that empty list.
Step 6: Display that list.

サンプルコード

# Python program to print unique values from a list

def unique(A):
   # intilize a null list
   uniquevalues = []

   # traversing the list
   for i in A:
   # check unique valueis present or not
   if i not in uniquevalues:
      uniquevalues.append(i)
      # print (A)
      for i in uniquevalues:
      print (i),

      # Driver code
      A=list()
      n=int(input("Enter the size of the List ::"))
      print("Enter the Element of List ::")
      for i in range(int(n)):
      k=int(input(""))
      A.append(k)

print("The unique values from the List is ::>")
unique(A)

出力

Enter the size of the List :: 6
Enter the Element of List ::
1
2
5
2
1
7
The unique values from the List is ::>
1
2
5
7

  1. 整数のリストから重複を印刷するPythonプログラム?

    ここでは、番号のリストからすべての重複する番号を印刷しようとしています。そのため、リスト内で複数回出現するすべての番号を出力しようとしています(リスト内で一意ではありません)。 例 Input: given_list = [ 3, 6, 9, 12, 3, 30, 15, 9, 45, 36, 12] Output: desired_output = [3, 9, 12] Input: given_list = [-27, 4, 29, -27, -2 , -99, 123, 499, -99] Output: desired_output = [-27, -99] 以下は、特定のリストから

  2. リストのすべてのサブリストを出力するPythonプログラム。

    リストを指定して、リストのすべてのサブリストを印刷します。 例- Input : list = [1, 2, 3] Output : [], [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]] アルゴリズム Step 1 : given a list. Step 2 : take one sublist which is empty initially. Step 3 : use one for loop till length of the given list. Step 4 : Run a loop from i+1 to length of th