在本教程中,我们将编写一个程序,其中包括在给定列表中每个元素之后增加元素的顺序。让我们看一个例子来清楚地理解它。
alphabets = ['a', 'b', 'c']
输出结果
['a', '#', 'b', '##', 'c', '###']
请按照以下步骤解决问题。
初始化列表。
3创建一个空列表。
遍历初始列表。
将当前元素和相应的哈希数添加到空列表。
打印结果列表。
# initializing the list
alphabets = ['a', 'b', 'c']
# empty list
result = []
# iterating over the alphabets
for i in range(len(alphabets)):
# appending the current element
result.append(alphabets[i])
# appending the (i + 1) number of hashes
result.append((i + 1) * '#')
# printing the result
print(result)
输出结果
如果运行上面的代码,则将得到以下结果。
['a', '#', 'b', '##', 'c', '###']
如果您对本教程有疑问,请在评论部分中提及它们。