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
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短