create lines of text, '\n'.join(my_list) is missing trailing newline :-(

  • Last Update :
  • Techknowledgy :

Just add another \n like this(since the output of join() is also a string):

my_list = ['one', 'two', 'three']
print '\n'.join(my_list) + '\n'

In Python 3.6, you may also use generator expression (PEP289):

''.join(f "{row}\n"
   for row in my_list)

Alternatively to manually adding to the return value of join, you could (manually as well) append the empty string to your list.

>>> my_list = ['one', 'two', 'three'] >>>
   my_list.append('') >>>
   '\n'.join(my_list)
'one\ntwo\nthree\n'

Using print before, it works:

>>> print '\n'.join(ls)
one
two
three
   >>>

Suggestion : 2

Last Updated : 05 Jul, 2022

Output :

The original list: ['gf\ng', 'i\ns', 'b\nest', 'fo\nr', 'geeks\n']
List after newline character removal: ['gfg', 'is', 'best', 'for', 'geeks']

Output:

Geeks
for Geeks

Suggestion : 3

x.argmax(0) gives the indexes along the 1st axis for the maximum values. Use np.indices to generate the indices for the other axis.

x = np.array([
   [
      [1, 2],
      [0, 1]
   ],
   [
      [3, 4],
      [6, 7]
   ]
])
x.argmax(0)
array([
   [1, 1],
   [1, 1]
])
a1, a2 = np.indices((2, 2))
   (x.argmax(0), a1, a2)
   (array([
         [1, 1],
         [1, 1]
      ]),
      array([
         [0, 0],
         [1, 1]
      ]),
      array([
         [0, 1],
         [0, 1]
      ]))

x[x.argmax(0), a1, a2]
array([
   [3, 4],
   [6, 7]
])

x[a1, x.argmax(1), a2]
array([
   [1, 2],
   [6, 7]
])

x[a1, a2, x.argmax(2)]
array([
   [2, 1],
   [4, 7]
])

Suggestion : 4

1 Python Dictionary get() August 7, 2022

The strip() method will remove both trailing and leading newlines from the string. It also removes any whitespaces on both sides of a string.

# strip() method to remove newline characters from a string
text = "\n Welcome to Python Programming \n"
print(text.strip())

Output

Welcome to Python Programming
3._
# rstrip() method to remove trailing newline character from a string
text = "Welcome to Python Programming \n"
print(text.rstrip())

We can also use the map function in Python to iterate the list of strings and remove the newline characters, as shown below. It would be a more optimized and efficient way of coding when compared to the for a loop.

my_list = ["Python\n", "is\n", "Fun\n"]
print(list(map(str.strip, my_list)))

The re.sub() function is similar to replace() method in Python. The re.sub() function will replace the specified newline character with an empty character.

# Python code to remove newline character from string using regex

import re
text = "A regular \n expression is a sequence \n of characters\n that specifies a search\n pattern."
print(re.sub('\n', '', text))

my_list = ["Python\n", "is\n", "Fun\n"]
new_list = []

print("Original List: ", my_list)

for i in my_list:
   new_list.append(re.sub("\n", "", i))
print("After removal of new line ", new_list)

Suggestion : 5

前言:这与Create lines of text, '\n'.join(my_list) is missing trailing newline :-(类似,只是这里它是一个生成器,而不是一个列表。你知道吗

我相信构建这样一个字符串的推荐方法是(假设g是generator对象)

'\n'.join(g)

下面是一个使用','而不是'\n'的示例:

>>> g = (str(i) for i in range(0, 10)) >>>
   ','.join(g)
'0,1,2,3,4,5,6,7,8,9'

我尝试使用itertools.chain()附加一个空字符串,但得到了令人惊讶的结果:

>>>
import itertools
   >>>
   g = itertools.chain((str(i) for i in range(0, 10)), '') >>>
   ','.join(g)
'0,1,2,3,4,5,6,7,8,9'

我喜欢你的想法,你希望它更有效地使用生成器,但是"".join实际上是在加入之前将genexp内部转换成一个列表。之所以这样做是因为它需要测量最后一个字符串的长度并相应地分配内存。这样,它会在生成器上进行两次传递(基本上是创建一个列表以便临时保存值)

py - 3 - m timeit "''.join([str(i) for i in range(100000)])"
10 loops, best of 5: 29.6 msec per loop

py - 3 - m timeit "''.join((str(i) for i in range(100000)))"
10 loops, best of 5: 32.3 msec per loop