Find Python List Elements Question

Asked 1 years ago, Updated 1 years ago, 108 views

a = [[0, 0], [1, 0], [0], [1, 1], [0, 0], [0, 1], [1], [1, 0], [0, 1], [1, 0], [1], [0, 0], [1, 1], [0, 1], [0], [0, 1], [0, 1]]
minn = 1

list= [len(x) for x in a]
for b in list:
        if b == minn:
             print b
#exam
b = [[0, 0], [1], [0], [1, 1], [0, 0], [0, 1], [1], [1, 0], [0], [1, 0], [1], [0, 0], [1, 1], [0, 1], [0], [0, 1], [0, 1]]

# # print
[1] = 3 pieces
[0] = 3 pieces

The current code is to print out a list with a length of 1, and what I want to add is, "I want to print out a list with a length of 1 and an element value of 0 or 1 in the list. What should I add? I'd appreciate it if you could let me know.

python python-2.7

2022-09-22 18:19

1 Answers

You can compare it by adding the and operations to the if syntax.

a = [[0, 0], [1, 0], [0], [1, 1], [0, 0], [0, 1], [1], [1, 0], [0, 1], [1, 0], [1], [0, 0], [1, 1], [0, 1], [0], [0, 1], [0, 1]]
minn = 1

for sub_a in a:
        if len(sub_a) == minn and sub_a[0] == 0:
                print sub_a


2022-09-22 18:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.