Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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中使用数组从用户输入字符列表_Python_Arrays - Fatal编程技术网

如何在Python中使用数组从用户输入字符列表

如何在Python中使用数组从用户输入字符列表,python,arrays,Python,Arrays,我正试图建立一个基本的程序,将采取从用户的名字,并将显示名称的数量回来,其中包含超过5个字母 from array import* list=array('u',[]) n=int(input("Enter the number of names first")) for i in range(0,n): y=input("Enter the names one by one") list.append(y) 但是当运行这段代码时,我得到了一个错误 “TypeError

我正试图建立一个基本的程序,将采取从用户的名字,并将显示名称的数量回来,其中包含超过5个字母

from array import*

list=array('u',[])

n=int(input("Enter the number of names first"))

for i in range(0,n):

    y=input("Enter the names one by one")
    list.append(y)
但是当运行这段代码时,我得到了一个错误

“TypeError:数组项必须是unicode字符”


继续评论,保持简单易懂:

n = int(input("Enter the number of names first"))

nameLst = []           # an empty list to store the desired names
for i in range(0,n):
    y = input("Enter the names one by one")     
    if len(y) > 5:               # if the len of name is > 5
        nameLst.append(y)        # append it to the list

for i in range(len(nameLst)):    # iterate over the len of the list
    print(nameLst[i])            # print all the names in the list

继续评论,保持简单易懂:

n = int(input("Enter the number of names first"))

nameLst = []           # an empty list to store the desired names
for i in range(0,n):
    y = input("Enter the names one by one")     
    if len(y) > 5:               # if the len of name is > 5
        nameLst.append(y)        # append it to the list

for i in range(len(nameLst)):    # iterate over the len of the list
    print(nameLst[i])            # print all the names in the list

那么,您的问题是什么?此代码的性能如何与预期不同?请检查
y
len
if>5
存储在
列表中
然后打印该列表。
u
类型代码对应于Python过时的unicode字符。将
关键字
用作列表的
名称
也被认为是不好的做法,使用
lst
namesList
将其与关键字区分开来的任何内容。那么您的问题是什么?此代码的性能如何与预期不同?检查
y
len
if>5
存储在
列表中
然后打印该列表。
u
类型代码对应于Python过时的unicode字符。将
关键字
用作列表的
名称
也被认为是不好的做法,使用
lst
namesList
将其与关键字区分开来的任何东西。我使用了列表而不是数组,效果很好。我仍然有点困惑为什么数组不起作用,我使用了一个函数来打印包含5个以上字母的名称的数量。我使用列表而不是数组,它工作得很好,但我还是有点困惑,为什么数组不工作,我使用了一个函数来打印包含超过5个字母的名称的数量。