【问题标题】:Input control on a string array字符串数组上的输入控件
【发布时间】:2022-11-21 02:04:20
【问题描述】:

我试图让用户只输入字符串数组中的字母字符。我需要检查每个字符,看看它是否按字母顺序排列。没有 for 循环可以完成吗?我也不是在寻找正则表达式。

from numpy import*
t = array([str]*5)

def remplir(t,i):
    if i<5:
        t[i] = input("Donner chaine: ")
        while "A">t[i] or t[i]>="Z":
            print("Test")
remplir(t,0)

【问题讨论】:

  • thestring.isalpha() 会告诉您字符串是否包含所有字母字符。
  • 我不能使用“.isalpha()”,因为我们不允许在学校的程序中使用它

标签: python arrays


【解决方案1】:

您可以使用 all 关键字:

print(all(c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" for c in t))

【讨论】:

  • 如果您限制自己使用 ASCII,这没问题,但对于非英语文本会失败。
猜你喜欢
  • 2017-05-30
  • 2016-10-19
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-25
相关资源
最近更新 更多