在Python中编写这个“双管列表”的更好方法

在Python中编写这个“双管列表”的更好方法,第1张

概述我的意思是第12行的for循环和嵌套在其中的for循环.我不止一次遇到这种情况.我会使用列表理解,但似乎它不会在这里工作. import randomimport stringdef password_generator(): key = zip(string.digits, string.ascii_uppercase) cruft, x = str(random.ra 我的意思是第12行的for循环和嵌套在其中的for循环.我不止一次遇到这种情况.我会使用列表理解,但似乎它不会在这里工作.

import randomimport stringdef password_generator():    key = zip(string.digits,string.ascii_uppercase)    cruft,x = str(random.random()).split('.')    pw = ''    for item in x:        for element in key:            if item in element:                Q = random.random()                if Q > 0.7:                    pw += element[1].lower()                else:                    pw += element[1]    print pw

谢谢.

解决方法 这是一种使用列表理解的方法:

def pw_gen():    key = zip(string.digits,x = str(random.random()).split('.')    def f(i,e):      Q = random.random()      if Q > 0.7:        return e[1].lower()      else:        return e[1]    return [ f(item,element) for item in x for element in key if item in element ]

这将返回一个字符列表.使用“”.join(pw_gen())转换为字符串.

总结

以上是内存溢出为你收集整理的在Python中编写这个“双管列表”的更好方法全部内容,希望文章能够帮你解决在Python中编写这个“双管列表”的更好方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1193255.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存