Python 如何输出每个字母之间有空格的字符串

Python 如何输出每个字母之间有空格的字符串,python,python-2.7,python-2.x,Python,Python 2.7,Python 2.x,例如,如果我输入了我爱狗,它需要如下所示: I l o v e d o g s 此代码不执行我需要它执行的操作: def spaceitout(source): pile = "" for letter in source: pile = pile+letter print pile print pile 这能满足你的需要吗 pile = ' '.join(source) 这将采用“source”的元素,并使用单个空格作为连接符将

例如,如果我输入了
我爱狗
,它需要如下所示:

I  l o v e  d o g s
此代码不执行我需要它执行的操作:

def spaceitout(source):
    pile = ""
    for letter in source:
        pile = pile+letter
        print pile
    print pile

这能满足你的需要吗

pile = ' '.join(source)
这将采用“source”的元素,并使用单个空格作为连接符将它们连接起来

如果您只需要分隔字母,则只构建字母列表,并将其连接:

pile = ' '.join([c for c in source if c.isalpha()])

这能满足你的需要吗

pile = ' '.join(source)
这将采用“source”的元素,并使用单个空格作为连接符将它们连接起来

如果您只需要分隔字母,则只构建字母列表,并将其连接:

pile = ' '.join([c for c in source if c.isalpha()])

字母之间的空格:

def spaceitout(source, count): 
    return (' '*count).join([letter for letter in source.replace(' ','')])
单词之间的空格:

def spaceitout(source, count):
    return (' '*count).join(source.split())
所有字符之间的空格:

def spaceitout(source, count): 
    return (''.join([c + (' '*count) for c in source]).strip())

字母之间的空格:

def spaceitout(source, count): 
    return (' '*count).join([letter for letter in source.replace(' ','')])
单词之间的空格:

def spaceitout(source, count):
    return (' '*count).join(source.split())
所有字符之间的空格:

def spaceitout(source, count): 
    return (''.join([c + (' '*count) for c in source]).strip())

简单的答案是:

def spaceitout(source):
    pile = ""
    for letter in source:
        pile = pile + letter + " "
    pile = pile[:-1] #Strip last extraneous space.
    print pile

简单的答案是:

def spaceitout(source):
    pile = ""
    for letter in source:
        pile = pile + letter + " "
    pile = pile[:-1] #Strip last extraneous space.
    print pile
使用itertools:

import itertools

def space_word(word, spaces_count=1):
    if spaces_count < 1:
        raise ValueError("spaces_count < 1")

    def space_word_wrapped(word, spaces_count):
        letters = itertools.chain.from_iterable(zip(word, itertools.cycle(" ")))
        skipped = 0  # have to keep track of how many letter skips
                     # or else from_word starts breaking!
                     # TODO : better implementation of this
        for i, lett in enumerate(letters, start=1):
            from_word = (i + skipped) % 2
            if lett.isspace() and from_word:
                if spaces_count == 1:
                    next(letters)
                    skipped += 1
                    continue       # and skip the space itself
                elif spaces_count == 2:
                    continue       # just count the space
                elif spaces_count == 3:
                    pass           # count everything
                else:          # spaces_count >= 4
                    yield from [" "] * (spaces_count - 3)
            yield lett

    return ''.join(space_word_wrapped(word, spaces_count)).rstrip()
导入itertools
定义空格\u单词(单词、空格\u计数=1):
如果空间计数小于1:
raise VALUE ERROR(“空格数<1”)
def space_word_wrapped(单词、空格计数):
字母=itertools.chain.from_iterable(zip(单词,itertools.cycle(“”))
skipped=0#必须跟踪跳过多少个字母
#否则我的话就要断掉了!
#TODO:更好地实现此功能
对于i,请输入枚举(字母,开始=1):
from_word=(i+skipped)%2
如果lett.isspace()和from_word:
如果空间\u计数=1:
下一篇(信件)
跳过+=1
继续,跳过空格本身
elif空间_计数==2:
继续#数一数空格
elif空格_计数==3:
通过#数一数
其他:#空格数>=4
[“”]*(空格计数-3)的收益率
收益率
返回“”。join(单词、空格、计数)rstrip()
在这里使用迭代器可能更便宜,但我喜欢嵌套函数方法中的生成器。所以起诉我!:)

使用itertools:

import itertools

def space_word(word, spaces_count=1):
    if spaces_count < 1:
        raise ValueError("spaces_count < 1")

    def space_word_wrapped(word, spaces_count):
        letters = itertools.chain.from_iterable(zip(word, itertools.cycle(" ")))
        skipped = 0  # have to keep track of how many letter skips
                     # or else from_word starts breaking!
                     # TODO : better implementation of this
        for i, lett in enumerate(letters, start=1):
            from_word = (i + skipped) % 2
            if lett.isspace() and from_word:
                if spaces_count == 1:
                    next(letters)
                    skipped += 1
                    continue       # and skip the space itself
                elif spaces_count == 2:
                    continue       # just count the space
                elif spaces_count == 3:
                    pass           # count everything
                else:          # spaces_count >= 4
                    yield from [" "] * (spaces_count - 3)
            yield lett

    return ''.join(space_word_wrapped(word, spaces_count)).rstrip()
导入itertools
定义空格\u单词(单词、空格\u计数=1):
如果空间计数小于1:
raise VALUE ERROR(“空格数<1”)
def space_word_wrapped(单词、空格计数):
字母=itertools.chain.from_iterable(zip(单词,itertools.cycle(“”))
skipped=0#必须跟踪跳过多少个字母
#否则我的话就要断掉了!
#TODO:更好地实现此功能
对于i,请输入枚举(字母,开始=1):
from_word=(i+skipped)%2
如果lett.isspace()和from_word:
如果空间\u计数=1:
下一篇(信件)
跳过+=1
继续,跳过空格本身
elif空间_计数==2:
继续#数一数空格
elif空格_计数==3:
通过#数一数
其他:#空格数>=4
[“”]*(空格计数-3)的收益率
收益率
返回“”。join(单词、空格、计数)rstrip()

在这里使用迭代器可能更便宜,但我喜欢嵌套函数方法中的生成器。所以起诉我!:)

允许您指定单词之间的空格和字符之间的空格。基于BigOldTree提供的答案

def space_it(text, word_space=1, char_space=0):
    return (' '*word_space).join([(' '*char_space).join(x) for x in text.split(' ')])

注意:这会将输入文本中的两个空格视为它们之间有一个“不可见的单词”,如果不需要,请将
text.split(“”)
更改为
text.split()

允许您指定单词之间的空格和字符之间的空格。基于BigOldTree提供的答案

def space_it(text, word_space=1, char_space=0):
    return (' '*word_space).join([(' '*char_space).join(x) for x in text.split(' ')])

注意:这会将输入文本中的两个空格视为它们之间有一个“不可见的单词”,如果不需要,请将
text.split(“”)
更改为
text.split()

我想这就是您想要的:

line = 'I love dogs'
for i in line:
 if i != ' ':
  print i,
 else:
  print '',

我想这就是你想要的:

line = 'I love dogs'
for i in line:
 if i != ' ':
  print i,
 else:
  print '',
这会列出你的单词('his'=['h','i','s']),然后用空格而不是逗号连接它

def space(word):
   return ' '.join(list(word))
这会列出你的单词('his'=['h','i','s']),然后用空格而不是逗号连接它

def space(word):
   return ' '.join(list(word))

它将为空格添加额外的空格,而这不是他想要的。我如何才能像添加计数或索引那样在每个字母之间选择空格的数量注意:永远不要执行
[c代表c在源代码中]
。这只是
列表(c)
请添加一些解释。传授基本逻辑比仅仅给出代码更重要,因为这有助于OP和其他读者自己解决此问题和类似问题。这将为空格添加额外的空格,而这不是他想要的。我如何才能像添加一个计数或索引注意:永远不要做
[c代表源代码中的c]
。那只是
列表(c)
请添加一些解释。传授基本逻辑比仅仅给出代码更重要,因为这有助于OP和其他读者自己解决此问题和类似问题。我如何才能做到这一点,以便我可以选择空格的数量,现在只在单词之间。这取决于您希望如何处理空格字里行间的步调?你想让“我爱狗”变成“我爱狗”吗?你需要/想要的东西会决定你怎么做。我需要它变成“我爱狗”,我在字里行间选择物种的数量。我的意思是,你打算如何处理先前存在的空间?你说的是“字里行间”,但这在这里是不明确的。你是指两个单词之间的空格,还是两个单词中字符之间的空格?两个单词之间应该有多少空格?我该怎么做