如何使用\ n分割长列表

时间:2014-08-19 14:27:57

标签: python string

这是一个长字符串,我将其转换为列表,以便我可以操作它,然后将它连接在一起。我有一些麻烦能够让迭代器遍历列表,当迭代器到达时,让我们说每个第5个对象,它应该在那里插入'\ n'。这是一个例子:

string = "Hello my name is Josh I like pizza and python I need this string to be really really long"

string = string.split()

# do the magic here

string = ' '.join(string)

print(string)

输出:

Hello my name is Josh
I like pizza and python
I need this string to
be really really long

知道如何实现这一目标吗?

我尝试使用:

for words in string:
    if words % 5 == 0:
        string.append('\n')

但它不起作用。我错过了什么?

8 个答案:

答案 0 :(得分:1)

您所做的错误是尝试更改示例中的string,这不会影响列表中包含的字符串...而是需要索引到列表中并直接更改元素。< / p>

text = "Hello my name is Josh I like pizza and python I need this string to be really really long"

words = text.split()    
for wordno in range(len(words)):
    if wordno and wordno % 5 == 0:
        words[wordno] += '\n'

print ' '.join(words)

你不想打电话给string,因为它是一个有时使用的内置模块,可能会让事情变得混乱,而且我还检查过wordno不是0,否则你会在你的重新加入中以一个单词行结束...

答案 1 :(得分:0)

在这种情况下,您可能应该创建一个新字符串,而不是尝试修改现有字符串。您可以使用计数器来确定您所在的单词。这是一个非常简单的解决方案,尽管Jon Clements有一个更复杂(可能更有效)的解决方案:

newstring = ""
str = "Hello my name is Josh I like pizza and python I need this string to be really really long"
strlist = str.split()

for i, words in enumerate(strlist):
  newstring += words
  if (i + 1) % 5 == 0:
    newstring += "\n"
  else:
    newstring += " "

`enumerate1返回单词列表中单词的索引,以及单词本身。这是一个方便的自动计数器,用于确定您所使用的单词。

此外,实际上并未命名您的字符串string 。这是您不想覆盖的模块的名称。

答案 2 :(得分:0)

我相信这可能会短得多,但这可以满足您的需求。它们的关键改进是用于保存所有内容的新字符串以及使用枚举来捕获列表中的第i个单词。

string = "Hello my name is Josh I like pizza and python I need this string to be really really long"
string2 = ""

for (i,word) in enumerate(string.split()):
    string2  = string2 + " " + word
    if (i + 1) % 5 == 0:        
        string2  = string2 + '\n'
print(string2)

答案 3 :(得分:0)

分割后你可以在你的字符串上使用enumerate()。

并迭代:

new_string_list = []

for index, word in string:
    if (index + 1) % 5 == 0:
        word += '\n'

    new_string_list.append(word)

string = ' '.join(new_string_list)

答案 4 :(得分:0)

您尝试使用的for循环的问题在于它没有保留单词的索引,因此无法确定哪个单词是第5个单词。通过使用枚举(iterable),您可以同时获得单词的索引和单词。您也可以使用range(len(iterable))来获取索引,并以相同的方式执行。

string = "Hello my name is Josh I like pizza and python I need this string to be really really long"
string = string.split()


for word_num, word in enumerate(string):
    if word_num and word_num % 5 == 0:
        # access the array since changing only the variable word wont work
        string[word_num] += '\n'

string = ' '.join(string)

print(string)

修改

正如@JonClements指出的那样,这会导致&#34;你好&#34;因为0%5 = 0,所以我添加了一个检查,看看word_num是否计算为True(如果它不等于0,它会这样做)

答案 5 :(得分:0)

使用enumerate在i上获取index和%操作,每n个块附加'\ n'

new_string_list = []
strlist = oldstr.split()

// do magic here 

for i,word in enumerate(strlist):
    new_string_list.append(word) #it is better
    #to append to list then join, using + to concatenate strings is ineffecient
    if count % 5 == 0:
        new_string_list.append('\n')

print("".join(new_string_list)) 

答案 6 :(得分:0)

使用列表切片......

s='hello my name is josh i like pizza and python i need this string to be really really long'

l=s.split()
l[4::5]=[v+'\n' for v in l[4::5] ]
print '\n'.join(' '.join(l).split('\n '))


hello my name is josh
i like pizza and python
i need this string to
be really really long

答案 7 :(得分:0)

我做的可怕的单行作为你不应该做的事情的一个例子。

s='hello my name is josh i like pizza and python i need this string to be really really long'
print '\n'.join([' '.join(l) for l in [s.split()[i:i + 4] for i in range(0, len(s.split()), 4)]])