Python 2.7 Python列表和新列表
我是一个完全的编程新手,我正在学习Python。我写了以下内容:Python 2.7 Python列表和新列表,python-2.7,Python 2.7,我是一个完全的编程新手,我正在学习Python。我写了以下内容: list = ["de", "sandy", "ger"] 我希望少于4个单词的元素进入名为newList的新列表。请帮忙。到目前为止,我已经做到了: list= ["de", "sandy", "ger"] newlist =[] for name in list: if len(name) <4: print name newlist.append(name[0:3]) print n
list = ["de", "sandy", "ger"]
我希望少于4个单词的元素进入名为newList
的新列表。请帮忙。到目前为止,我已经做到了:
list= ["de", "sandy", "ger"]
newlist =[]
for name in list:
if len(name) <4:
print name
newlist.append(name[0:3])
print newlist
list=[“de”、“sandy”、“ger”]
新列表=[]
对于列表中的名称:
如果len(name)打印一个东西不会使它附加到其他任何地方
list = ["de", "sandy", "ger"]
newlist = []
for name in list:
if len(name) < 4:
newlist.append(name)
print newlist
这是使用for
循环的最后一个结果,这会导致不必要的行为。这意味着“从for循环最后处理的值中提取前3个字符,并将其附加到空的newlist中”。如果您的标识有问题,请尝试使用堆栈溢出编辑器粘贴精确的代码。Python是识别敏感的,因此您的追加
应该与打印名
对齐。非常感谢您的支持
newlist.append(name[0:3])