在Python列表中增加备用元素模式
在本教程中,我们将编写一个程序,其中包括在给定列表中每个元素之后增加元素的顺序。让我们看一个例子来清楚地理解它。
输入值
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', '###']
结论
如果您对本教程有疑问,请在评论部分中提及它们。
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短