Python 如何在列表变量之间添加字符串?
我试图在列表中的每个变量后添加“!”。 但我的代码只在初始列表后添加“!”序列。 例如:Python 如何在列表变量之间添加字符串?,python,Python,我试图在列表中的每个变量后添加“!”。 但我的代码只在初始列表后添加“!”序列。 例如: lst = [1,2,3,4] def addmark(lst): emptylst = [] for n in range(0, len(lst)): lst.append("!") return lst 这将返回[1,2,3,4,“!”、“!”、“!”、“!”、“!”] 我想返回[1,!”,2,!”,3,!”,4,!”]这是代码 #!/usr/bin/env
lst = [1,2,3,4]
def addmark(lst):
emptylst = []
for n in range(0, len(lst)):
lst.append("!")
return lst
这将返回[1,2,3,4,“!”、“!”、“!”、“!”、“!”]
我想返回[1,!”,2,!”,3,!”,4,!”]这是代码
#!/usr/bin/env python
# coding:utf-8
'''黄哥Python'''
def addmark(lst):
result = []
for i, item in enumerate(lst):
result.append(item)
result.append("!")
return result
if __name__ == '__main__':
lst = [1,2,3,4]
print addmark(lst)
这是密码
#!/usr/bin/env python
# coding:utf-8
'''黄哥Python'''
def addmark(lst):
result = []
for i, item in enumerate(lst):
result.append(item)
result.append("!")
return result
if __name__ == '__main__':
lst = [1,2,3,4]
print addmark(lst)
使用itertools的可接受答案的替代方案:
from itertools import chain, repeat
lst = [1, 2, 3]
marker = repeat("!")
list(chain.from_iterable(zip(lst, marker)))
>>> [1, '!', 2, '!', 3, '!']
使用itertools的可接受答案的替代方案:
from itertools import chain, repeat
lst = [1, 2, 3]
marker = repeat("!")
list(chain.from_iterable(zip(lst, marker)))
>>> [1, '!', 2, '!', 3, '!']
使用插入:
def addmark(lst):
add = 0 # needed cause after every insertion of '!' the position where you want to add the next '!' changes
for i in range (1,len(lst)+1): # (start: adding after ls[0], finish: adding after the last element)
lst.insert(i+add, '!')
add += 1
return lst
列表。插入(i,x)在给定位置插入项目。第一 参数是要插入的元素的索引,因此 a、 在列表前面插入(0,x)个插入项,以及a.插入(len)(a), x) 相当于a.append(x) 参考: 代码:
def addmark(lst):
add = 0 # needed cause after every insertion of '!' the position where you want to add the next '!' changes
for i in range (1,len(lst)+1): # (start: adding after ls[0], finish: adding after the last element)
lst.insert(i+add, '!')
add += 1
return lst
使用插入:
def addmark(lst):
add = 0 # needed cause after every insertion of '!' the position where you want to add the next '!' changes
for i in range (1,len(lst)+1): # (start: adding after ls[0], finish: adding after the last element)
lst.insert(i+add, '!')
add += 1
return lst
列表。插入(i,x)在给定位置插入项目。第一 参数是要插入的元素的索引,因此 a、 在列表前面插入(0,x)个插入项,以及a.插入(len)(a), x) 相当于a.append(x) 参考: 代码:
def addmark(lst):
add = 0 # needed cause after every insertion of '!' the position where you want to add the next '!' changes
for i in range (1,len(lst)+1): # (start: adding after ls[0], finish: adding after the last element)
lst.insert(i+add, '!')
add += 1
return lst
创建列表列表,然后展平
lst = [1,2,3,4]
lst2 = [[i,'!'] for i in lst]
lst3 = [item for sublist in lst2 for item in sublist]
print lst2
print lst3
>>> [[1, '!'], [2, '!'], [3, '!'], [4, '!']]
>>> [1, '!', 2, '!', 3, '!', 4, '!']
作为一个班轮:
lst = [1,2,3,4]
lst2 = [item for sublist in [[i,'!'] for i in lst] for item in sublist]
print lst2
>>> [1, '!', 2, '!', 3, '!', 4, '!']
创建列表列表,然后展平
lst = [1,2,3,4]
lst2 = [[i,'!'] for i in lst]
lst3 = [item for sublist in lst2 for item in sublist]
print lst2
print lst3
>>> [[1, '!'], [2, '!'], [3, '!'], [4, '!']]
>>> [1, '!', 2, '!', 3, '!', 4, '!']
作为一个班轮:
lst = [1,2,3,4]
lst2 = [item for sublist in [[i,'!'] for i in lst] for item in sublist]
print lst2
>>> [1, '!', 2, '!', 3, '!', 4, '!']
虽然此代码可以回答问题,但提供关于如何和/或为什么解决问题的附加上下文将提高答案的长期价值。虽然此代码可以回答问题,但提供关于如何和/或为什么解决问题的附加上下文将提高答案的长期价值。