每天一习题,提升Python不是问题!!有更简洁的写法请评论告知我!
https://www.cnblogs.com/poloyy/category/1676599.html
题目
a = [1, 2, 3, 4, 5] b = ["a", "b", "c", "d", "e"] 如何得出c = ["a1", "b2", "c3", "d4", "e5"]
解题思路
- a、b两个列表长度一致,获取长度
- 一个for循环,每次获取同下标值
- 字符串拼接,添加到c列表
答案
a = [1, 2, 3, 4, 5] b = ["a", "b", "c", "d", "e"] c = [] # 方案一 for i in a: inx = a.index(i) d = b[inx] if d != -1: c.append(f"{d}{i}") print(c) # 方案二 c = [] for i in range(0, len(a)): a1 = a[i] b1 = b[i] c.append(f"{b1}{a1}") print(c)