在列表中的单词内添加空格
Adding spaces inside words in list
我正在尝试将“”() 添加到列表中的单词中,但出于某种原因 python 不允许在列表中使用 ljust,它给了我这个错误:
' 'list' object has no attribute 'ljust''
我的代码:
spaced = []
sliced = ['text', 'text']
def spacer(sliced):
for slice in sliced:
print spacer
# finds the word length
lol = len(slice)
lal = spaced.ljust(lol + 1)
print lol
spaced.append(slice)
print spaced
我需要的输出:
切片 = ['text ', 'text ']
关于如何做的想法将不胜感激!谢谢
ljust()
是字符串的一种方法。使用 slice.ljust(lol+1)
sliced = ['text', 'text']
def spacer(sliced):
result = []
for slices in sliced:
# finds the word length
lol = len(slices)
lal = slices.ljust(lol + 1)
result.append(lal)
return result
#or you can you one-liner list comprehension instead of all of the above
#return [word.ljust(len(word)+1) for word in sliced]
print spacer(sliced)
不要更改您正在迭代的列表,而是创建一个新列表并return它。
sliced = [word+' ' for word in sliced]
快速解决方案:
def spacer(sliced):
return [word+' ' for word in sliced]
您可以使用列表理解:
def spacer(sliced)
return [x.ljust(len(x)+1) for x in sliced]
或更简单地说:
def spacer(sliced)
return [x+' ' for x in sliced]
在你的函数中使用方法你可以这样做:
sliced = ['text', 'text']
def spacer(sliced):
spaced = []
for slice in sliced:
lol = len(slice)
lal = slice.ljust(lol + 1)
spaced.append(lal)
return spaced
print spacer(sliced)
在字符串末尾添加 space 就像使用 +
:
一样简单
string = 'a'
new_string = string + ' '
所以你只需要迭代列表中的每个项目并附加 space:
for string in sliced:
print string + ' '
所以可以通过简单的列表理解创建一个新列表
new_sliced = [slice + ' ' for slice in sliced]
或者,如果您想就地更改 sliced
列表,您可以使用内置的 enumerate 来获取列表中每个元素的索引
for i, slice in enumerate(sliced):
sliced[i] = slice + ' '
我正在尝试将“”() 添加到列表中的单词中,但出于某种原因 python 不允许在列表中使用 ljust,它给了我这个错误:
' 'list' object has no attribute 'ljust''
我的代码:
spaced = []
sliced = ['text', 'text']
def spacer(sliced):
for slice in sliced:
print spacer
# finds the word length
lol = len(slice)
lal = spaced.ljust(lol + 1)
print lol
spaced.append(slice)
print spaced
我需要的输出: 切片 = ['text ', 'text '] 关于如何做的想法将不胜感激!谢谢
ljust()
是字符串的一种方法。使用 slice.ljust(lol+1)
sliced = ['text', 'text']
def spacer(sliced):
result = []
for slices in sliced:
# finds the word length
lol = len(slices)
lal = slices.ljust(lol + 1)
result.append(lal)
return result
#or you can you one-liner list comprehension instead of all of the above
#return [word.ljust(len(word)+1) for word in sliced]
print spacer(sliced)
不要更改您正在迭代的列表,而是创建一个新列表并return它。
sliced = [word+' ' for word in sliced]
快速解决方案:
def spacer(sliced):
return [word+' ' for word in sliced]
您可以使用列表理解:
def spacer(sliced)
return [x.ljust(len(x)+1) for x in sliced]
或更简单地说:
def spacer(sliced)
return [x+' ' for x in sliced]
在你的函数中使用方法你可以这样做:
sliced = ['text', 'text']
def spacer(sliced):
spaced = []
for slice in sliced:
lol = len(slice)
lal = slice.ljust(lol + 1)
spaced.append(lal)
return spaced
print spacer(sliced)
在字符串末尾添加 space 就像使用 +
:
string = 'a'
new_string = string + ' '
所以你只需要迭代列表中的每个项目并附加 space:
for string in sliced:
print string + ' '
所以可以通过简单的列表理解创建一个新列表
new_sliced = [slice + ' ' for slice in sliced]
或者,如果您想就地更改 sliced
列表,您可以使用内置的 enumerate 来获取列表中每个元素的索引
for i, slice in enumerate(sliced):
sliced[i] = slice + ' '