TGTGInsighttelegram intelligenceLIVE / telegram public index
← Python Заметки

TGINSIGHT SIMILAR POSTS

Најди сличен содржај

Изворен канал @pythonotes · Post #309 · 2 фев.

Метод строки split() разделяет строку на несколько строк по указанному символу >>> "a_b_c".split('_') ['a', 'b', 'c'] Можно указать максимальное количество разделений >>> "a_b_c".split('_', 1) ['a', 'b_c'] Или резать с другой стороны с помощью rsplit() (right split) >>> "a_b_c".rsplit('_', 1) ['a_b', 'c'] А что будет если оставить аргументы пустыми? >>> "a_b_c".split() ['a_b_c'] Получаем список с одним элементом, потому что по умолчанию используется пробельный символ. >>> "a b c".split() ['a', 'b', 'c'] То есть это равнозначно такому вызову? >>> "a b c".split(" ") ['a', 'b', 'c'] Кажется да, но нет! Давайте попробуем добавить пробелов между буквами >>> "a b c".split(" ") ['a', '', '', 'b', '', '', 'c'] И вот картина уже не так предсказуема 😕 А вот что будет по умолчанию >>> "a b c".split() ['a', 'b', 'c'] Всё снова красиво! 🤩 По умолчанию в качестве разделителя используется любой пробельный символ, будь то табуляция или новая строка. Включая несколько таких символов идущих подряд. А также игнорируются пробельные символы по краям строки. >>> "a\t b\n c ".split() ['a', 'b', 'c'] Аналогичный способ можно собрать с помощью регулярного выражения. Но пробелы по краям строки придется обрабатывать дополнительно. >>> import re >>> re.split(r"\s+", ' a b c '.strip()) ['a', 'b', 'c'] Здесь тоже можно указать количество разделений >>> re.split(r"\s+", 'a b c', 1) ['a', 'b c'] А что если мы хотим написать красиво, то есть split() без аргументов, но при этом указать количество разделений? В этом случае первым аргументом передаём None >>> "a\n b c".split(None, 1) ['a', 'b c'] Данный метод не учитывает строки с пробелами, взятые в кавычки 'a "b c" '.split() ['a', '"b', 'c"'] Но для таких случаев есть другие способы. #tricks#basic

Резултати

Пронајдени 69 слични објави

Пребарај: #september

当前筛选 #september清除筛选
悦读「优质少量RSS聚合」

@Dailyrss · Post #13121 · 28.09.2025 г., 00:45

Wikipedia每日一图(#September 28, 2025)https://ift.tt/Jhp6sa7 French chemist and microbiologist Louis Pasteur photographed by Nadar. Renowned for his scientific discoveries, he saved millions by developing vaccines for rabies and anthrax, as well as the process of pasteurization. He died on this day 130 years ago.. 了解更多 . #Wikipedia每日一图

悦读「优质少量RSS聚合」

@Dailyrss · Post #13120 · 27.09.2025 г., 00:45

Wikipedia每日一图(#September 27, 2025)https://ift.tt/suPWQxz Panoramic view of the west bank of Nigeen Lake in Srinagar, India, with the distant Middle Himalayan Pir Panjal mountain range in the background. The city, situated on the banks of the Jhelum river and several lakes in the Himalayan Kashmir Valley, is the summer capital of Jammu and Kashmir and the northernmost million-plus city of India.. 了解更多 . #Wikipedia每日一图

悦读「优质少量RSS聚合」

@Dailyrss · Post #13119 · 26.09.2025 г., 00:45

Wikipedia每日一图(#September 26, 2025)https://ift.tt/0kyxNdY An image of Arab twin brothers Saints Cosmas and Damian from the Grandes Heures of Anne of Brittany. As ancient physicians, they are depicted holding tools of their profession. Today is their feast day on the General Roman Calendar.. 了解更多 . #Wikipedia每日一图

123•••56
ПретходнаСтраница 1 од 6Следна