Python Forum
Two questions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Two questions (/thread-9239.html)



Two questions - jure98 - Mar-28-2018

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.


RE: Two questions - Larz60+ - Mar-28-2018

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'



RE: Two questions - jure98 - Mar-29-2018

(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.


RE: Two questions - Larz60+ - Mar-29-2018

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.