exampleArray = [2, "a", "b"]
allowedItems = ["a", "b"]
for x in exampleArray:
if x in allowedItems:
print("Valid Item: "+str(x))
else:
print("Invalid Item: "+str(x))
# Another way: You can use set()
exampleArray = [2, "a", "b"]
allowedItems = ["a", "b"]
# Just want to know allowed list items
# intersection and '&' is same
set(allowedItems).intersection(set(exampleArray))
set(allowedItems) & set(exampleArray)
# Print allowed items list
[ print(x+' is Allowed') for x in set(allowedItems).intersection(set(exampleArray)) ]
[ print(x+' is Allowed') for x in set(allowedItems) & set(exampleArray) ]
exampleArray = [2, "a", "b"]
allowedItems = ["a", "b"]
[print(f"{i} is a valid Item") if i in set(allowedItems) else print(f"{i} is not a valid Item") for i in exampleArray]
example_array = [2, "a", "b"]
allowed_items = ["a", "b"]
for _ in range(example_array[0]):
for item in example_array[1:]:
if item in allowed_items:
print(f"{item} is in allowed items")
5条答案
按热度按时间6l7fqoea1#
vzgqcmou2#
输出:-
aoyhnmkz3#
xdnvmnnf4#
列表理解:
xzv2uavs5#
我认为你正在努力实现这一目标。
该循环将迭代
examle_array[0]
次,就像上面的2次一样,然后对于每个项目,除了第一个项目(example_array[0]
- 2),检查该项目是否在allowed_list
中。下面是输出: