Python Forum

Full Version: Two questions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,

I'm today start in Python so i have two questions.

1)I want to generate all possible characters.I'm do it,but i don't have idea how that I start from "abab" for example,i don't want to start from begin every time.("a")

keywords = [''.join(i) for i in product(ascii_lowercase, repeat = x)]
So i just want that I can start from certain characters.


2)
I have problem when i use repeat 6 + in this line:

keywords = [''.join(i) for i in product(ascii_lowercase, repeat = 6)]
When I use repeat = 5 all works :/

#Errors
Error:
Traceback (most recent call last): File "C:\Users\paulc\Desktop\test.py", line 15, in <module> keywords = [''.join(i) for i in product(ascii_lowercase, repeat = 6)] File "C:\Users\paulc\Desktop\test.py", line 15, in <listcomp> keywords = [''.join(i) for i in product(ascii_lowercase, repeat = 6)] MemoryError
I hope you can understand me!

Thanks

I'm fix second problem,i use:

keywords = map(''.join, product(ascii_lowercase, repeat=6))
And all is ok.
1.
import string
keywords = string.ascii_lowercase

def rotate(s, n):
    return s[n:] + s[:n]
example:
Output:
>>> x = string.ascii_lowercase >>> rotate(x, 3) 'defghijklmnopqrstuvwxyzabc' >>> >>> rotate(x, -3) 'xyzabcdefghijklmnopqrstuvw'
(Mar-28-2018, 09:15 PM)Larz60+ Wrote: [ -> ]1.
import string
keywords = string.ascii_lowercase

def rotate(s, n):
    return s[n:] + s[:n]
example:
Output:
>>> x = string.ascii_lowercase >>> rotate(x, 3) 'defghijklmnopqrstuvwxyzabc' >>> >>> rotate(x, -3) 'xyzabcdefghijklmnopqrstuvw'

Sorry but i don't understand how to use this :( I'm begginer so i really don't know what to do with your code.
It's quite simple:
keywords = string.ascii_lowercase
creates a string of all lower case alpha characters a to z in keywords.
The function rotate, rotates a list (a string is a list) the number of steps forward or backward
specified as the second argument, with string name as argument 1.