Python 创建一个唯一的列表
我已经写了下面的python程序来获得唯一列表。但我想为相同的输出减少这段代码。由于python没有break/continue外部循环,因此我无法减少此计数。你能帮我以某种方式减少吗Python 创建一个唯一的列表,python,Python,我已经写了下面的python程序来获得唯一列表。但我想为相同的输出减少这段代码。由于python没有break/continue外部循环,因此我无法减少此计数。你能帮我以某种方式减少吗 empty_list=list() length=int(input("enter the unique element length: ")) number=int(input("enter the element:")) empty_list.append(number) x=1 while(x<
empty_list=list()
length=int(input("enter the unique element length: "))
number=int(input("enter the element:"))
empty_list.append(number)
x=1
while(x<length):
z=0
a=int(input("Enter the element:"))
for y in empty_list:
if(y==a):
z=1
if z==0:
empty_list.append(a)
x+=1
else:
x=x
print(empty_list)
empty_list=list()
length=int(输入(“输入唯一元素长度:”)
number=int(输入(“输入元素:”)
空列表。追加(编号)
x=1
而(x而不是列表
使用集合集合
。您不需要检查唯一性,只需在末尾从集合
创建一个列表
length=int(input("enter the unique element length: "))
number=int(input("enter the element:"))
number_set = set()
x=1
while(x<length):
z=0
a=int(input("Enter the element:"))
number_set.add(a)
x = x + 1
print(list(number_set))
length=int(输入(“输入唯一元素长度:”)
number=int(输入(“输入元素:”)
编号\u set=set()
x=1
而(x而不是列表
使用集合集合
。您不需要检查唯一性,只需在末尾从集合
创建一个列表
length=int(input("enter the unique element length: "))
number=int(input("enter the element:"))
number_set = set()
x=1
while(x<length):
z=0
a=int(input("Enter the element:"))
number_set.add(a)
x = x + 1
print(list(number_set))
length=int(输入(“输入唯一元素长度:”)
number=int(输入(“输入元素:”)
编号\u set=set()
x=1
而(xunique=list()
length=int(输入(“输入唯一元素长度:”)
而len(unique)<长度:
number=int(输入(“输入元素:”)
如果编号不唯一:
唯一。追加(编号)
打印(唯一)
unique=list()
length=int(输入(“输入唯一元素长度:”)
而len(unique)<长度:
number=int(输入(“输入元素:”)
如果编号不唯一:
唯一。追加(编号)
打印(唯一)
空列表
似乎用词不当。你到底想实现什么?你想获得用户输入并列出收到的唯一元素吗?是的。上面的程序做了我所期望的。但我想减少行数以获得相同的输出。对你的编码风格的一些反馈:z
似乎具有布尔函数的功能,所以你应该这样声明。还要给它一个描述性的名称。空列表
也应该得到一个不同的名称来描述所包含的内容。x=x
没有任何作用。在=
符号前后留一个空格。空列表
似乎用词不当。你到底想实现什么?你想得到吗ser输入并列出收到的唯一元素?是的。上面的程序做了我所期望的。但我想减少行数以获得相同的输出。对您的编码风格的一些反馈:z
似乎具有布尔函数,因此您应该声明它。还应该给它一个描述性名称。空列表
也应该获取描述所包含内容的其他名称。x=x
不起任何作用。在符号前后留出空格。我不想使用“设置”如果顺序重要怎么办?我不想使用“设置”如果顺序重要怎么办?