Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Two questions
#1
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.
Reply
#2
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'
Reply
#3
(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.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 34,271 Nov-25-2017, 06:21 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020