How can I generate a structured list of 2 digit integers in Python? [duplicate]












2
















This question already has an answer here:




  • How to get all possible combinations of a list’s elements?

    24 answers




I'm attempting to replicate the attached piece of C# code in Python, but unfortunately I know a lot less about Python than I do with the former. The code essentially generates a list of 2 character/digit values e.g. aa, ab, ac, ad, etc. It includes combinations of A-Z, a-z, and 0-9.



I've looked into django.utils.crypto module, but that creates a randomised output, and the output from my C# code gives the exact amount of combinations when run. This is what I tried with Django's module:



get_random_string(length=2, 
allowed_chars=u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')


Other options I tried do something similar, so I'm at a bit of a loss. I know the basics of Python, but unfortunately I haven't found enough to be able to replicate my C# code in Python. The C# code:



var alpha = 
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var q = alpha.Select(x => x.ToString());
int size = 2;
for (int i = 0; i < size - 1; i++)
q = q.SelectMany(x => alphabet, (x, y) => x + y);

foreach (var item in q)
Console.WriteLine(item);


Converted into python, the function should generate about 62^2 combinations of the values in 2 digit integers.










share|improve this question















marked as duplicate by U9-Forward, martineau python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 9:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    2
















    This question already has an answer here:




    • How to get all possible combinations of a list’s elements?

      24 answers




    I'm attempting to replicate the attached piece of C# code in Python, but unfortunately I know a lot less about Python than I do with the former. The code essentially generates a list of 2 character/digit values e.g. aa, ab, ac, ad, etc. It includes combinations of A-Z, a-z, and 0-9.



    I've looked into django.utils.crypto module, but that creates a randomised output, and the output from my C# code gives the exact amount of combinations when run. This is what I tried with Django's module:



    get_random_string(length=2, 
    allowed_chars=u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')


    Other options I tried do something similar, so I'm at a bit of a loss. I know the basics of Python, but unfortunately I haven't found enough to be able to replicate my C# code in Python. The C# code:



    var alpha = 
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    var q = alpha.Select(x => x.ToString());
    int size = 2;
    for (int i = 0; i < size - 1; i++)
    q = q.SelectMany(x => alphabet, (x, y) => x + y);

    foreach (var item in q)
    Console.WriteLine(item);


    Converted into python, the function should generate about 62^2 combinations of the values in 2 digit integers.










    share|improve this question















    marked as duplicate by U9-Forward, martineau python
    Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Jan 2 at 9:19


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      2












      2








      2









      This question already has an answer here:




      • How to get all possible combinations of a list’s elements?

        24 answers




      I'm attempting to replicate the attached piece of C# code in Python, but unfortunately I know a lot less about Python than I do with the former. The code essentially generates a list of 2 character/digit values e.g. aa, ab, ac, ad, etc. It includes combinations of A-Z, a-z, and 0-9.



      I've looked into django.utils.crypto module, but that creates a randomised output, and the output from my C# code gives the exact amount of combinations when run. This is what I tried with Django's module:



      get_random_string(length=2, 
      allowed_chars=u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')


      Other options I tried do something similar, so I'm at a bit of a loss. I know the basics of Python, but unfortunately I haven't found enough to be able to replicate my C# code in Python. The C# code:



      var alpha = 
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
      var q = alpha.Select(x => x.ToString());
      int size = 2;
      for (int i = 0; i < size - 1; i++)
      q = q.SelectMany(x => alphabet, (x, y) => x + y);

      foreach (var item in q)
      Console.WriteLine(item);


      Converted into python, the function should generate about 62^2 combinations of the values in 2 digit integers.










      share|improve this question

















      This question already has an answer here:




      • How to get all possible combinations of a list’s elements?

        24 answers




      I'm attempting to replicate the attached piece of C# code in Python, but unfortunately I know a lot less about Python than I do with the former. The code essentially generates a list of 2 character/digit values e.g. aa, ab, ac, ad, etc. It includes combinations of A-Z, a-z, and 0-9.



      I've looked into django.utils.crypto module, but that creates a randomised output, and the output from my C# code gives the exact amount of combinations when run. This is what I tried with Django's module:



      get_random_string(length=2, 
      allowed_chars=u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')


      Other options I tried do something similar, so I'm at a bit of a loss. I know the basics of Python, but unfortunately I haven't found enough to be able to replicate my C# code in Python. The C# code:



      var alpha = 
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
      var q = alpha.Select(x => x.ToString());
      int size = 2;
      for (int i = 0; i < size - 1; i++)
      q = q.SelectMany(x => alphabet, (x, y) => x + y);

      foreach (var item in q)
      Console.WriteLine(item);


      Converted into python, the function should generate about 62^2 combinations of the values in 2 digit integers.





      This question already has an answer here:




      • How to get all possible combinations of a list’s elements?

        24 answers








      c# python






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 10:26







      Alex Gho

















      asked Jan 2 at 9:11









      Alex GhoAlex Gho

      163




      163




      marked as duplicate by U9-Forward, martineau python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 2 at 9:19


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by U9-Forward, martineau python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 2 at 9:19


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          1














          Here's an example



          alpha = 'abc'
          from itertools import permutations, combinations_with_replacement
          [''.join(i) for i in list(set(combinations_with_replacement(alpha, 2)) | set(permutations(alpha, 2)))]

          # Output -> ['ab', 'ba', 'aa', 'cc', 'bb', 'cb', 'ac', 'ca', 'bc'] which is 3^2


          Now create alpha as suggested by @Adam Feor in comment



          import string 
          alpha = string.ascii_letters + string.digits
          print(alpha)
          Output -> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789





          share|improve this answer





















          • 1





            You can also import the string module which contains ascii_letters and digits to get the characters you want. characters = string.ascii_letters + string.digits

            – Adam Feor
            Jan 2 at 9:23













          • This is what I was looking for, thanks! Although I'm attempting to run this code but it doesn't seem to output when I attempt to print. I'm probably missing something simple here (not great with Python).

            – Alex Gho
            Jan 2 at 9:47











          • @AlexGho store the last line which goes like [''.join.. into a variable say out = [''.join.. and then use print(out)

            – meW
            Jan 2 at 9:52


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Here's an example



          alpha = 'abc'
          from itertools import permutations, combinations_with_replacement
          [''.join(i) for i in list(set(combinations_with_replacement(alpha, 2)) | set(permutations(alpha, 2)))]

          # Output -> ['ab', 'ba', 'aa', 'cc', 'bb', 'cb', 'ac', 'ca', 'bc'] which is 3^2


          Now create alpha as suggested by @Adam Feor in comment



          import string 
          alpha = string.ascii_letters + string.digits
          print(alpha)
          Output -> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789





          share|improve this answer





















          • 1





            You can also import the string module which contains ascii_letters and digits to get the characters you want. characters = string.ascii_letters + string.digits

            – Adam Feor
            Jan 2 at 9:23













          • This is what I was looking for, thanks! Although I'm attempting to run this code but it doesn't seem to output when I attempt to print. I'm probably missing something simple here (not great with Python).

            – Alex Gho
            Jan 2 at 9:47











          • @AlexGho store the last line which goes like [''.join.. into a variable say out = [''.join.. and then use print(out)

            – meW
            Jan 2 at 9:52
















          1














          Here's an example



          alpha = 'abc'
          from itertools import permutations, combinations_with_replacement
          [''.join(i) for i in list(set(combinations_with_replacement(alpha, 2)) | set(permutations(alpha, 2)))]

          # Output -> ['ab', 'ba', 'aa', 'cc', 'bb', 'cb', 'ac', 'ca', 'bc'] which is 3^2


          Now create alpha as suggested by @Adam Feor in comment



          import string 
          alpha = string.ascii_letters + string.digits
          print(alpha)
          Output -> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789





          share|improve this answer





















          • 1





            You can also import the string module which contains ascii_letters and digits to get the characters you want. characters = string.ascii_letters + string.digits

            – Adam Feor
            Jan 2 at 9:23













          • This is what I was looking for, thanks! Although I'm attempting to run this code but it doesn't seem to output when I attempt to print. I'm probably missing something simple here (not great with Python).

            – Alex Gho
            Jan 2 at 9:47











          • @AlexGho store the last line which goes like [''.join.. into a variable say out = [''.join.. and then use print(out)

            – meW
            Jan 2 at 9:52














          1












          1








          1







          Here's an example



          alpha = 'abc'
          from itertools import permutations, combinations_with_replacement
          [''.join(i) for i in list(set(combinations_with_replacement(alpha, 2)) | set(permutations(alpha, 2)))]

          # Output -> ['ab', 'ba', 'aa', 'cc', 'bb', 'cb', 'ac', 'ca', 'bc'] which is 3^2


          Now create alpha as suggested by @Adam Feor in comment



          import string 
          alpha = string.ascii_letters + string.digits
          print(alpha)
          Output -> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789





          share|improve this answer















          Here's an example



          alpha = 'abc'
          from itertools import permutations, combinations_with_replacement
          [''.join(i) for i in list(set(combinations_with_replacement(alpha, 2)) | set(permutations(alpha, 2)))]

          # Output -> ['ab', 'ba', 'aa', 'cc', 'bb', 'cb', 'ac', 'ca', 'bc'] which is 3^2


          Now create alpha as suggested by @Adam Feor in comment



          import string 
          alpha = string.ascii_letters + string.digits
          print(alpha)
          Output -> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 9:26

























          answered Jan 2 at 9:21









          meWmeW

          2,858120




          2,858120








          • 1





            You can also import the string module which contains ascii_letters and digits to get the characters you want. characters = string.ascii_letters + string.digits

            – Adam Feor
            Jan 2 at 9:23













          • This is what I was looking for, thanks! Although I'm attempting to run this code but it doesn't seem to output when I attempt to print. I'm probably missing something simple here (not great with Python).

            – Alex Gho
            Jan 2 at 9:47











          • @AlexGho store the last line which goes like [''.join.. into a variable say out = [''.join.. and then use print(out)

            – meW
            Jan 2 at 9:52














          • 1





            You can also import the string module which contains ascii_letters and digits to get the characters you want. characters = string.ascii_letters + string.digits

            – Adam Feor
            Jan 2 at 9:23













          • This is what I was looking for, thanks! Although I'm attempting to run this code but it doesn't seem to output when I attempt to print. I'm probably missing something simple here (not great with Python).

            – Alex Gho
            Jan 2 at 9:47











          • @AlexGho store the last line which goes like [''.join.. into a variable say out = [''.join.. and then use print(out)

            – meW
            Jan 2 at 9:52








          1




          1





          You can also import the string module which contains ascii_letters and digits to get the characters you want. characters = string.ascii_letters + string.digits

          – Adam Feor
          Jan 2 at 9:23







          You can also import the string module which contains ascii_letters and digits to get the characters you want. characters = string.ascii_letters + string.digits

          – Adam Feor
          Jan 2 at 9:23















          This is what I was looking for, thanks! Although I'm attempting to run this code but it doesn't seem to output when I attempt to print. I'm probably missing something simple here (not great with Python).

          – Alex Gho
          Jan 2 at 9:47





          This is what I was looking for, thanks! Although I'm attempting to run this code but it doesn't seem to output when I attempt to print. I'm probably missing something simple here (not great with Python).

          – Alex Gho
          Jan 2 at 9:47













          @AlexGho store the last line which goes like [''.join.. into a variable say out = [''.join.. and then use print(out)

          – meW
          Jan 2 at 9:52





          @AlexGho store the last line which goes like [''.join.. into a variable say out = [''.join.. and then use print(out)

          – meW
          Jan 2 at 9:52





          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]