i for i in s 是什么意思?

时间:2021-04-29 02:58:24

标签: python

def CodelandUsernameValidation(s):
  if len(s)>4 and len(s)<25 and s[0].isalpha() and [i for i in s if i.isalnum() or i=="_"]!=[] and s[-1]!="_":
    return True
  else:
    return False
# keep this function call here 
print(CodelandUsernameValidation(input()))

2 个答案:

答案 0 :(得分:1)

如果你展开它是一个 > Configure project :app Reading env from: .env Checking the license for package Android SDK Build-Tools 29.0.2 in /Users/fsara/Library/Android/sdk/licenses Warning: License for package Android SDK Build-Tools 29.0.2 not accepted. Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/6.5/userguide/command_line_interface.html#sec:command_line_warnings FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring project ':app'. > Cannot query the value of this provider because it has no value available. * Try: Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Exception is: org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':app'. at org.gradle.configuration.project.LifecycleProjectEvaluator.wrapException(LifecycleProjectEvaluator.java:75) at org.gradle.configuration.project.LifecycleProjectEvaluator.addConfigurationFailure(LifecycleProjectEvaluator.java:68) at org.gradle.configuration.project.LifecycleProjectEvaluator.access$600(LifecycleProjectEvaluator.java:51) ...org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64) at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48) at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56) * Get more help at https://help.gradle.org BUILD FAILED in 2m 18s Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/6.5/userguide/command_line_interface.html#sec:command_line_warnings,它看起来像这样 ->

list comprehension

以上代码可以改写为-

result = []
for i in s: # will fetch element one by one from iterable
    if i.isalnum() or i=="_": # checking condition
        result.append(i) # if true, then append it to the list

答案 1 :(得分:0)

这会生成 s 中字母数字或下划线字符的列表。代码实际上是不正确的,因为如果任何字符是字母数字或下划线,它就会通过,而意图肯定是所有字符都必须是字母数字或下划线。这是一种更好的编写方式:

def CodelandUsernameValidation(s):
  return 4 < len(s) < 25 and s[0].isalpha() and all(i.isalnum() or i=='_' for i in s) and s[-1] != '_'

print(CodelandUsernameValidation(input()))