[英]How to split up a long list using \n
這是一個長字符串,我將其轉換為列表,以便可以對其進行操作,然后將其重新結合在一起。 我在讓迭代器遍歷列表時遇到了一些麻煩,當迭代器到達時,讓我們說每個第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')
但這不起作用。 我想念什么?
您做錯了什么,就是嘗試在示例中更改string
,這不會影響列表中包含的字符串...相反,您需要索引到列表中並直接更改元素。
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
事情感到困惑,而且我還檢查了wordno
不為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
。 這是您不想覆蓋的模塊的名稱。
我相信這可能會短得多,但這確實可以滿足您的要求。 它們的關鍵改進是一個容納所有內容的新字符串,並使用枚舉來捕獲列表中的第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)
拆分字符串后,可以在字符串上使用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)
您嘗試使用的for循環的問題在於,它沒有保留單詞的索引,因此無法確定哪個單詞是第5個單詞。 通過使用enumerate(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指出的那樣,這會導致“ Hello”被打印在其自己的行上,因為0%5 =0。因此,我添加了一個檢查以查看word_num的計算結果是否為True(如果它不等於0,則為True)。
使用枚舉獲取i的索引和%操作,每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))
正在使用列表切片...
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
我做的可怕的單線方式是你永遠不應該做的一個例子。
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)]])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.