-2

I'm trying to create a long list of strings of letters, then add .com to the end of them, so I can ping each website, then add working ones to a list of websites as a sort of web scraping project, but I just don't know how to create the list of websites, so I am trying to make a list like (a,b,c,d,e...aa,ab,ac,ad), which will all be pinged to check if it responds.

  • You can iterate over character codes, then use `chr()` to convert them to characters. – Barmar Mar 31 '22 at 19:19
  • 3
    Or see https://stackoverflow.com/questions/16060899/alphabet-range-in-python for how to get a string with all the letters, you can iterate over that. You can also use `itertools.combinations()` to create all the combinations. – Barmar Mar 31 '22 at 19:20
  • A successful ping to a host is not a guarantee that there's a web server on that host and, conversely, not being able to ping a host is not a guarantee that there's not a web server on that host afaik. – jarmod Mar 31 '22 at 19:23

1 Answers1

0

If you want to create a structure like the one in your example, you can use combinations from iter tools.

from itertools import combinations

def get_all_combinations(letters: str, max_len: int) -> tuple:
    """Creates all combinations of letters until a maximum length.
    Args:
        letters (str): string with the letters to combine. I.e. "abcde"
        max_len (int): maximum number of characters for the combinations.

    Returns:
        tuple: strings with the combinations.
    """
    all_combinations = []
    for i_len in range(1, max_len + 1):
        i_len_combinations = combinations(letters, i_len)
        all_combinations.extend(["".join(i) for i in i_len_combinations])
    return tuple(all_combinations)


letters = "abcde"
max_len = 3
print(get_all_combinations(letters, max_len))

And like @Barmar said, you can use the link for how to get a string with all the letters as a reference for the letters to be used.

For instance:

import string


print(string.ascii_lowercase)

But if you need also the permutations, you can change the import for the permutation function in itertools.

All combinations for "ab" are:

('a', 'b', 'c', 'ab', 'ac', 'bc')

But all the permutations are:

('a', 'b', 'c', 'ab', 'ac', 'ba', 'bc', 'ca', 'cb')
Nelson
  • 25
  • 1
  • 9