如何在Python中使用数组从用户输入字符列表
我正试图建立一个基本的程序,将采取从用户的名字,并将显示名称的数量回来,其中包含超过5个字母如何在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
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个字母的名称的数量。