编写程序,输入包含任意数据的列表,检查列表中数据的重复情况,判断列表的类型。如果列表内所有元素都是一样的,输出“complete repetition”;如果列表中所有元素都互相不一样,输出“complete non repetition”;
时间: 2023-06-15 11:06:03 浏览: 51
可以使用以下 Python 代码来实现该功能:
```python
def check_list(lst):
# 判断列表类型
if len(set(lst)) == 1:
print("complete repetition")
elif len(set(lst)) == len(lst):
print("complete non repetition")
else:
print("not complete repetition or non repetition")
# 检查列表重复情况
repeat = False
for i in range(len(lst)):
if lst[i] in lst[i+1:]:
repeat = True
break
if repeat:
print("There are duplicates in the list")
else:
print("There are no duplicates in the list")
```
其中,`set(lst)` 可以将列表转换为集合,从而去除重复元素。`len(set(lst))` 可以得到集合的大小,从而判断列表是否存在重复元素。
在函数 `check_list` 中,首先判断列表类型,并输出相应的结果。然后使用循环遍历列表,判断是否存在重复元素,并输出相应的提示信息。
可以调用该函数,并传入任意数据的列表进行测试,例如:
```python
lst1 = [1, 2, 3, 4, 5]
check_list(lst1) # 输出 "complete non repetition" 和 "There are no duplicates in the list"
lst2 = [1, 2, 3, 4, 4, 5]
check_list(lst2) # 输出 "not complete repetition or non repetition" 和 "There are duplicates in the list"
lst3 = [1, 1, 1, 1, 1]
check_list(lst3) # 输出 "complete repetition" 和 "There are duplicates in the list"
```