D
D
Dizro2022-04-20 15:46:54
Python
Dizro, 2022-04-20 15:46:54

Is it possible to change the array (combine the words in it) before and after a certain word?

# Имеется строка:
c = "§YCentralization§! indicates how much ruling is conducted by the nation's central government. There are benefits to both §GHigh Centralization§! and §YLow Centralization§!, however no benefits are gained from §YModerate Centralization§!."

#Создается массив из слов, которые разделены пробелом.
h = [x for x in c.split()]

print(h) # ['§YCentralization§!', 'indicates', 'how', 'much', 'ruling', 'is', 'conducted', 'by', 'the', "nation's", 'central', 'government.', 'There', 'are', 'benefits', 'to', 'both', '§GHigh', 'Centralization§!', 'and', '§YLow', 'Centralization§!,', 'however', 'no', 'benefits', 'are', 'gained', 'from', '§YModerate', 'Centralization§!.']

# Нужно чтобы слова содержащие символ §,в данном случае: §YCentralization§!, §GHigh, Centralization§!, §YLow, Centralization§!, §YModerate, Centralization§! - не изменялись, а другие слова соединились.

# Готовый массив должен выглядеть так:  ['§YCentralization§!', "indicates how much ruling is conducted by the nation's central government. There are benefits to both", '§GHigh', 'Centralization§!', 'and', '§YLow', 'Centralization§!,', 'however no benefits are gained from', '§YModerate', 'Centralization§!.']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2022-04-20
@Dizro

Can. To do this, you can, for example, loop through the words forming a list-answer. If the current word does not contain a paragraph symbol, then it must either be added to the answer list, or added to the last word there. Or it might be simpler to maintain a variable with the current word concatenation. If the word in the list does not contain a paragraph - add to the variable. If you met a pragaraf, then add a variable and a word with a paragraph in response and clear the variable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question