简体   繁体   English

如何将ljust()转换应用于字符串列表的每个元素?

[英]How do i apply a ljust() transformation to every element of a string list?

I am learning python and i'm trying to add 2 ** in front of each line of some example text. 我正在学习python,并尝试在示例文本的每一行前添加2 **。 However when i call ljust(2,'*') on every element it doesn't change the original string. 但是,当我在每个元素上调用ljust(2,'*')时,它都不会更改原始字符串。 I want to replace the new element with this old string but how? 我想用这个旧字符串替换新元素,但是如何?

This is what i' ve tried. 这就是我尝试过的。 First i tried it with a regular for loop but that didn't work. 首先,我尝试了一个常规的for循环,但是没有用。 Then i came across a question where list comprehensions are explained Perform a string operation for every element in a Python list so i tried that. 然后我遇到了一个问题,其中解释了列表推导。 对Python列表中的每个元素执行字符串操作,所以我尝试了这一点。

This is what i have now 这就是我现在所拥有的

example_string = '''hello there how are you doing!
i am doig well thank you
lets get to work!!! '''


def modify_example_string():
    global example_string
    new_string_list = [element.ljust(2,'*') for element in example_string.split('\n')]
    example_string = '\n'.join(new_string_list)
    print(new_string_list)

modify_example_string()

This should return a new list with all the ljust transformations but it doesn't so i would like to know the proper way of solving this. 这应该返回具有所有ljust转换的新列表,但事实并非如此,因此我想知道解决此问题的正确方法。

It seems like you misunderstood what ljust(2, '*') is doing. 好像您误解了ljust(2, '*')在做什么。 It does not add two * to the beginning of the string but will pad the string with * to a total length of 2. All your lines are longer, so it does nothing. 它不会在字符串的开头添加两个* ,但是会在字符串的末尾加上*使其总长度为2。所有行都更长,因此它什么也不做。

Instead, just use "**" + line to add the stars to the lines. 取而代之的是,只需使用"**" + line星号添加到行中即可。

def modify_example_string():
    global example_string
    example_string = "\n".join("**" + line for line in example_string.splitlines())

Also, instead of using global I'd recommend using parameters and return values: 另外,建议不要使用global参数,而应该使用参数和返回值:

def prepend_stars(s):
    return "\n".join("**" + line for line in s.splitlines())

example_string = prepend_stars(example_string)

The ljust method doesn't do what you expect. ljust方法无法达到您的期望。 The documentation says: 文件说:

Return the string left justified in a string of length width. 返回以长度为宽度的字符串左对齐的字符串。 Padding is done using the specified fillchar (default is an ASCII space). 使用指定的fillchar填充(默认为ASCII空间)。 The original string is returned if width is less than or equal to len(s) . 如果width小于或等于len(s),则返回原始字符串

Your list comprehension is correct. 您的列表理解是正确的。

One solution might be to use format . 一种解决方案可能是使用format A good tutorial here . 一个很好的教程在这里

Example code with format : format示例代码:

example_string = '''hello there how are you doing!
i am doig well thank you
lets get to work!!! '''


def modify_example_string(example_string, ch, n):
    new_string_list = ["{} {}".format(ch * n, element)
                       for element in example_string.split('\n')]
    example_string = '\n'.join(new_string_list)
    return example_string

print(modify_example_string(example_string, "*", 2))
# ** hello there how are you doing!
# ** i am doig well thank you
# ** lets get to work!!!

I would split your string using the splitlines() method and use a for loop to iterate through the lines and build a new, concatenated string for output. 我将使用splitlines()方法拆分您的字符串,并使用for循环遍历各行并构建新的串联字符串以输出。

Something like this: 像这样:

example_string = '''hello there how are you doing!
i am doig well thank you
lets get to work!!! '''


def modify_example_string(input_string):
    new_string_list = ''
    for line in input_string.splitlines():
        new_string_list += f'**{line}\n'
    return new_string_list

print(modify_example_string(example_string))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM