Better way to write this "double-barreled list" in Python

yyyyQqxKNqHyy

The for loop at line 12 and the one nested within it, I mean. I've encountered situations like this more than once. I'd use a list comprehension but it doesn't seem like it would work here.

import random

import string

def 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

Thanks.

ErikR

Here's a way to use a list comprehension:

def pw_gen():
    key = zip(string.digits, string.ascii_uppercase)

    cruft, 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 ]

This returns a list of characters. Use "".join( pw_gen() ) to convert to a string.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Better way to write jquery add/remove class

分類Dev

What is a better way to write this SQL query?

分類Dev

RXAndroid better way to write nested subscriptions

分類Dev

Better way to write Case When Multiple conditions

分類Dev

Do you know a better way to write this method?

分類Dev

What is a better way to write an if statement using PHP

分類Dev

better way to write def __str__(self) in django model

分類Dev

Haskell: Is there a better way to write case statements with the same RHS?

分類Dev

Better way to check a list for specific elements - python

分類Dev

Better way to get access to Google Sheet with Python

分類Dev

How to better write multiple exceptions with redundant code in Python?

分類Dev

Better way to write this jQuery code consisting of many links and open/closing divs

分類Dev

Is there a better way of comparing dates

分類Dev

A better way to detect the cursor?

分類Dev

Better way to return boolean

分類Dev

A better way to introspect a capture

分類Dev

Is there a better way to do this? BASH

分類Dev

better way to do this in MATLAB?

分類Dev

Is there a better way to loop code?

分類Dev

Python 3.7 - Fast way to concatenate strings and write them to disk

分類Dev

Is there a better way to define a global variable?

分類Dev

Decorator with Arguments: Would this be a better way?

分類Dev

Is this possible using LEAD or is there a better way?

分類Dev

Is there a way of making discord embeds better?

分類Dev

Is there a better way to do Insertion Sort?

分類Dev

Is there a better way to do this than echo?

分類Dev

Better way to override a function definition

分類Dev

Is there a better way to divide this string into substrings?

分類Dev

Python, Pandas: A Better way to get the first None position in list which give maximum consecutive None count

Related 関連記事

  1. 1

    Better way to write jquery add/remove class

  2. 2

    What is a better way to write this SQL query?

  3. 3

    RXAndroid better way to write nested subscriptions

  4. 4

    Better way to write Case When Multiple conditions

  5. 5

    Do you know a better way to write this method?

  6. 6

    What is a better way to write an if statement using PHP

  7. 7

    better way to write def __str__(self) in django model

  8. 8

    Haskell: Is there a better way to write case statements with the same RHS?

  9. 9

    Better way to check a list for specific elements - python

  10. 10

    Better way to get access to Google Sheet with Python

  11. 11

    How to better write multiple exceptions with redundant code in Python?

  12. 12

    Better way to write this jQuery code consisting of many links and open/closing divs

  13. 13

    Is there a better way of comparing dates

  14. 14

    A better way to detect the cursor?

  15. 15

    Better way to return boolean

  16. 16

    A better way to introspect a capture

  17. 17

    Is there a better way to do this? BASH

  18. 18

    better way to do this in MATLAB?

  19. 19

    Is there a better way to loop code?

  20. 20

    Python 3.7 - Fast way to concatenate strings and write them to disk

  21. 21

    Is there a better way to define a global variable?

  22. 22

    Decorator with Arguments: Would this be a better way?

  23. 23

    Is this possible using LEAD or is there a better way?

  24. 24

    Is there a way of making discord embeds better?

  25. 25

    Is there a better way to do Insertion Sort?

  26. 26

    Is there a better way to do this than echo?

  27. 27

    Better way to override a function definition

  28. 28

    Is there a better way to divide this string into substrings?

  29. 29

    Python, Pandas: A Better way to get the first None position in list which give maximum consecutive None count

ホットタグ

アーカイブ