python how to generate permutations of putting a singular character into a word

2024/7/7 6:54:23

No idea how to word this so the title sucks my bad,

Basically, I have a 4 letter word and I want to generate every permutation of putting a dash in it.

So if my word was Cats, I want to get every permutation of it having a dash in it,

Example:

c-ats
ca-ts
-c-ats
etc,

Is anybody able to help me?

Answer

I assume that you want to generate all possible ways to insert zero or more hyphens into your word s.t. no two hyphens are adjacent. Here is one way to do this with itertools.product:

from itertools import productdef hyphens(word):# create template, each {} is to be filled with "" or "-"template = "{}" + "{}".join(word) + "{}"# iterate over all possible ways to fill empty bracesfor chars in product(["", "-"], repeat=len(word) + 1):yield template.format(*chars)list(hyphens('cats'))
# ['cats', 'cats-', 'cat-s', 'cat-s-', 'ca-ts', 'ca-ts-', 'ca-t-s', 'ca-t-s-', 
# 'c-ats', 'c-ats-', 'c-at-s', 'c-at-s-', 'c-a-ts', 'c-a-ts-', 'c-a-t-s', 
# 'c-a-t-s-', '-cats', '-cats-', '-cat-s', '-cat-s-', '-ca-ts', '-ca-ts-', 
# '-ca-t-s', '-ca-t-s-', '-c-ats', '-c-ats-', '-c-at-s', '-c-at-s-', '-c-a-ts', 
# '-c-a-ts-', '-c-a-t-s', '-c-a-t-s-']
https://en.xdnf.cn/q/120304.html

Related Q&A

Selenium Scraping Javascript Table

I am stuggling to scrape as per code below. Would apprciate it if someone can have a look at what I am missing? Regards PyProg70from selenium import webdriver from selenium.webdriver import FirefoxOp…

PYTHON REGEXP to replace recognized pattern with the pattern itself and the replacement?

Text- .1. This is just awesome.2. Google just ruined Apple.3. Apple ruined itself! pattern = (dot)(number)(dot)(singlespace)Imagine you have 30 to 40 sentences with paragraph numbers in the above patt…

How can I extract the text between a/a? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How do I access classes and get a dir() of available actions?

I have been trying to get access to available functions for a Match Object from re.search. I am looking for a way to do that similar to how I could do dir(str) and I can find .replace.This is my dir() …

Python - IndexError: list index out of range

Why would data[entities][urls][0][expanded_url] would produce IndexError: list index out of range error? I understand what this error means but cant see why? perhaps too sleepy at 2 am? Please helpd…

Python: Use Regular expression to remove something

Ive got a string looks like thisABC(a =2,b=3,c=5,d=5,e=Something)I want the result to be likeABC(a =2,b=3,c=5)Whats the best way to do this? I prefer to use regular expression in Python.Sorry, somethi…

Python delete row in file after reading it

I python 2.7 I am reading data from file in while loop. When I successfully read row, I would like to delete this row from a file, but I dont know how to do it - Efficient way so i dont waste to much o…

Trying to keep the same type after saving a dataframe in a csv file

When I try to get my dataframe out of the csv file the type of the data changed. Is there a way I can avoid this?

Merge blocks of images to produce new image

Hi is there a way of merging specific blocks from multiple images of same size(say 100x100) and putting them together in a new image. To be more specific, consider I have a set of images which have bee…

Removing Characters from python Output

I did alot of work to remove the characters from the spark python output like u u u" [()/" which are creating problem for me to do the further work. So please put a focus on the same .I have …