Python:传递列表2中的项目以及与列表1中的某些项目相关的项目?

时间:2016-05-01 19:08:27

标签: python

如果存在某些条件,我怎么能从列表2中重新打开(传递)项目? 好的,我的代码尝试

numbers = ["1","2","5","3","4","9","6","5","7"]
mul = ["a","b"]
for num in numbers:
    for mul_nm in mul:
        h = num+mul_nm
        print(h)
        if (h == "5a"):
            print("don't test 5 with b and pass to next item in list numbers")
        else:
            continue

这个程序给出了这个结果:

1a
1b
2a
2b
5a
don't test 5 with b and pass to next item in list numbers
5b
3a
3b
4a
4b
9a
9b
6a
6b
5a
don't test 5 with b and pass to next item in list numbers
5b
7a
7b

虽然我需要这个过程给出这个结果:

1a
1b
2a
2b
5a
don't test 5 with b and pass to next item in list numbers
3a
3b
4a
4b
9a
9b
6a
6b
5a
don't test 5 with b and pass to next item in list numbers
7a
7b

2 个答案:

答案 0 :(得分:0)

如果我正确理解你的问题(来自示例) - 只需移动到print所在位置并修改if条件以检查“5b”而不是“5a”。

通过这些更改 - 除非组合== 'num + mul',否则将打印5b的组合,在这种情况下,将打印字符串警告。

numbers = ["1","2","5","3","4","9","6","5","7"]
mul = ["a","b"]
for num in numbers:
    for mul_nm in mul:
        h = num+mul_nm
        if (h == "5b"):
            print("don't test 5 with b and pass to next item in list numbers")
        else:
            print(h)

这会产生您想要的结果:

1a
1b
2a
2b
5a
don't test 5 with b and pass to next item in list numbers
3a
3b
4a
4b
9a
9b
6a
6b
5a
don't test 5 with b and pass to next item in list numbers
7a
7b

答案 1 :(得分:0)

使用 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.5.RELEASE</version> </dependency>

break

输出

numbers = ["1","2","5","3","4","9","6","5","7"]
mul = ["a","b"]
for num in numbers:
    for mul_nm in mul:
        h = num+mul_nm
        print(h)
        if (h == "5a"):
            print("don't test 5 with b and pass to next item in list numbers")
            break
        else:
            continue
相关问题