Python 有人能帮我删除输出列表中的空白点吗?我采用了不同的方法,但似乎没有任何效果:(
有人能帮我删除输出列表中的空白点吗?我使用了不同的方法,但似乎没有任何效果:( 功能:Python 有人能帮我删除输出列表中的空白点吗?我采用了不同的方法,但似乎没有任何效果:(,python,Python,有人能帮我删除输出列表中的空白点吗?我使用了不同的方法,但似乎没有任何效果:( 功能: def clean_list(values): values: [] clean = [] for i in values: if i not in clean: clean.append(i) print("Cleaned: {}".format(clean)) from functions import clea
def clean_list(values):
values: []
clean = []
for i in values:
if i not in clean:
clean.append(i)
print("Cleaned: {}".format(clean))
from functions import clean_list
values = input("values:")
clean_list(values)
Write and test the following function:
def clean_list(values):
"""
-------------------------------------------------------
Removes all duplicate values from a list: values contains
only one copy of each of its integers. The order of values
must be preserved.
Use: clean_list(values)
-------------------------------------------------------
Parameters:
values - a list of integers (list of int)
Returns:
None
-------------------------------------------------------
"""
Add this function to the PyDev module functions.py. Test it from t01.py.
Reminder: this function does not return a list - it updates the list it takes as a parameter.
values:1, 2, 0, 1, 4, 1, 1, 2, 2, 5, 4, 3, 1, 3, 3, 4, 2, 4, 3, 1, 3, 0, 3, 0, 0
Cleaned: ['1', ',', ' ', '2', '0', '4', '5', '3']
t01.py:
def clean_list(values):
values: []
clean = []
for i in values:
if i not in clean:
clean.append(i)
print("Cleaned: {}".format(clean))
from functions import clean_list
values = input("values:")
clean_list(values)
Write and test the following function:
def clean_list(values):
"""
-------------------------------------------------------
Removes all duplicate values from a list: values contains
only one copy of each of its integers. The order of values
must be preserved.
Use: clean_list(values)
-------------------------------------------------------
Parameters:
values - a list of integers (list of int)
Returns:
None
-------------------------------------------------------
"""
Add this function to the PyDev module functions.py. Test it from t01.py.
Reminder: this function does not return a list - it updates the list it takes as a parameter.
values:1, 2, 0, 1, 4, 1, 1, 2, 2, 5, 4, 3, 1, 3, 3, 4, 2, 4, 3, 1, 3, 0, 3, 0, 0
Cleaned: ['1', ',', ' ', '2', '0', '4', '5', '3']
问题:
def clean_list(values):
values: []
clean = []
for i in values:
if i not in clean:
clean.append(i)
print("Cleaned: {}".format(clean))
from functions import clean_list
values = input("values:")
clean_list(values)
Write and test the following function:
def clean_list(values):
"""
-------------------------------------------------------
Removes all duplicate values from a list: values contains
only one copy of each of its integers. The order of values
must be preserved.
Use: clean_list(values)
-------------------------------------------------------
Parameters:
values - a list of integers (list of int)
Returns:
None
-------------------------------------------------------
"""
Add this function to the PyDev module functions.py. Test it from t01.py.
Reminder: this function does not return a list - it updates the list it takes as a parameter.
values:1, 2, 0, 1, 4, 1, 1, 2, 2, 5, 4, 3, 1, 3, 3, 4, 2, 4, 3, 1, 3, 0, 3, 0, 0
Cleaned: ['1', ',', ' ', '2', '0', '4', '5', '3']
样本运行
Values: [1, 2, 0, 1, 4, 1, 1, 2, 2, 5, 4, 3, 1, 3, 3, 4, 2, 4, 3, 1, 3, 0, 3, 0, 0]
Cleaned: [1, 2, 0, 4, 5, 3]
我得到的输出:
def clean_list(values):
values: []
clean = []
for i in values:
if i not in clean:
clean.append(i)
print("Cleaned: {}".format(clean))
from functions import clean_list
values = input("values:")
clean_list(values)
Write and test the following function:
def clean_list(values):
"""
-------------------------------------------------------
Removes all duplicate values from a list: values contains
only one copy of each of its integers. The order of values
must be preserved.
Use: clean_list(values)
-------------------------------------------------------
Parameters:
values - a list of integers (list of int)
Returns:
None
-------------------------------------------------------
"""
Add this function to the PyDev module functions.py. Test it from t01.py.
Reminder: this function does not return a list - it updates the list it takes as a parameter.
values:1, 2, 0, 1, 4, 1, 1, 2, 2, 5, 4, 3, 1, 3, 3, 4, 2, 4, 3, 1, 3, 0, 3, 0, 0
Cleaned: ['1', ',', ' ', '2', '0', '4', '5', '3']
值
是一个字符串,因此您正在清理字符串中的字符列表。这些不是“空白点”,而是您在输入中键入的空格和逗号字符
如果您想将其视为数字列表,则必须将其拆分并转换为数字
values = list(map(int, input("values:").split(',')))
正如Barmar所说,你需要一个字符串,所以有两件事,你的数字是字符串,数据类型不是列表。因此,为了使它成为一个列表,我们可以在
处拆分字符串,
使用map将列表中的每个元素转换为int
,并使用list将对象转换为iterable。最后,我想说,为了完成你的目标是你的目标
所以下面我提供了一个你应该做什么的例子
def clean_list(values):
values = list(map(int,values.split(', ')))
print(f"Cleaned {set(values)}")
input
函数返回str
。值:[]
应该是什么?@Barmar看到了:@AndréKuljis不应该是values:List
?它不应该在参数列表中吗?def clean_List(values:List):