在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', '###']
结论
如果您对本教程有疑问,请在评论部分中提及它们。
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短