Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Python列表和新列表_Python 2.7 - Fatal编程技术网

Python 2.7 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

我是一个完全的编程新手,我正在学习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 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])