繁体   English   中英

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

[英]How do you input a list of characters from the user using arrays in Python

我正在尝试构建一个基本程序,该程序将从用户那里获取名称,并将显示包含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 times : '))
name =list(map(str, input("enter name spe by space : ").strip().split()))
if len(name)==n:
    name2 =[i for i in name if len(i)>=5]
    print("name are ->", *name2)

"""
output 

enter times : 3

enter name spe by space : john edward philips
name are -> edward philips

"""

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM