|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为啥我的这个得出的答案和小甲鱼给出的答案不一样??
- list1 = ['1.Jost do It','2.it\'s possible','3. let programming change the world','4. Impossible is Nothing']
- list2 = ['4. 阿迪达斯','2. 李宁','3. 鱼C工作室','1. 耐克']
- list3 = []
- for slogan in list1:
- for name in list2:
- if slogan[0] == name[0]:
- list3.append(name + ':' + slogan[2:])
- print(list3)
复制代码
我得到的结果是这样的:
- ['1. 耐克:Jost do It']
- ['1. 耐克:Jost do It', "2. 李宁:it's possible"]
- ['1. 耐克:Jost do It', "2. 李宁:it's possible", '3. 鱼C工作室: let programming change the world']
- ['1. 耐克:Jost do It', "2. 李宁:it's possible", '3. 鱼C工作室: let programming change the world', '4. 阿迪达斯: Impossible is Nothing']
复制代码
请问下是哪里出了问题??
你每次判断成功了都打印一遍,所以会导致重复。
把打印放到最后就行了:
- list1 = ['1.Jost do It','2.it\'s possible','3. let programming change the world','4. Impossible is Nothing']
- list2 = ['4. 阿迪达斯','2. 李宁','3. 鱼C工作室','1. 耐克']
- list3 = []
- for slogan in list1:
- for name in list2:
- if slogan[0] == name[0]:
- list3.append(name + ':' + slogan[2:])
- print(list3)
复制代码
|
|