检查python列表中预定义的匹配项
我是一个Python初学者,我想知道是否有人可以建议我如何检查预定义的对数列表检查python列表中预定义的匹配项,python,python-3.x,list,pattern-matching,Python,Python 3.x,List,Pattern Matching,我是一个Python初学者,我想知道是否有人可以建议我如何检查预定义的对数列表 green = yellow and blue purple = blue and red orange = red and yellow grey = white and black beige = white and yellow brown = black and yellow 下面是一些示例列表,我希望遍历这些示例列表并检查其中是否存在上述任何一对。如果它们确实存在于列表中,那么我想返回这对的名
green = yellow and blue
purple = blue and red
orange = red and yellow
grey = white and black
beige = white and yellow
brown = black and yellow
下面是一些示例列表,我希望遍历这些示例列表并检查其中是否存在上述任何一对。如果它们确实存在于列表中,那么我想返回这对的名称
Lists:
List1 ['white','black','yellow'] (return: brown, beige)
List2 ['yellow','white','black','red'](return: beige, grey, orange)
List3 ['blue','yellow','red'] (return: green, purple)
List4 ['white','red','blue'] (return: purple)
到目前为止,我尝试按列表的长度筛选列表,然后检查每个元素的成员资格,但它并没有返回所有可能的对
谢谢你的帮助
到目前为止,我已经尝试了以下几点:
(1) 循环浏览每个列表并对每个项目进行测试(但这不会返回所有可能的解决方案)
(二)
(三)
此函数使用次要颜色及其组件颜色的字典,并检查组件颜色集是否是传递给它的颜色集的子集:
secondary_colours = {"green": ("yellow", "blue"),
"purple": ("blue", "red"),
"orange": ("red", "yellow"),
"grey": ("white", "black"),
"beige": ("white", "yellow"),
"brown": ("black", "yellow")}
def obtainable_colours(primary_colours):
"""Returns a list of secondary colours obtainable
from combinations of the primary colours provided.
Assumes additive pigment mixing as defined above."""
global secondary_colours
response = []
for s_col,components in secondary_colours.items():
if set(components) < set(primary_colours):
response.append(s_col)
return response
你说你试过过滤。你能分享一下这些尝试吗?非常感谢你-我完全忘记了字典在我困惑中寻找不同的解决方案!!!
len_of_list = len(lists)
if len_of_list == 3:
three = lists
for item in three:
if any (colour in three for colour in ('yellow','black')):
print(three)
if len_of_list ==3:
three = lists
first_el = three[0]
second_el = three[1]
last_el = three[-1]
if "yellow" in first_el and "black" in second_el:
result = 'brown'
elif 'yellow' in first_el and 'black' in last_el:
result = 'brown'
elif 'yellow' in second_el and 'black' in last_el:
result = 'brown'
secondary_colours = {"green": ("yellow", "blue"),
"purple": ("blue", "red"),
"orange": ("red", "yellow"),
"grey": ("white", "black"),
"beige": ("white", "yellow"),
"brown": ("black", "yellow")}
def obtainable_colours(primary_colours):
"""Returns a list of secondary colours obtainable
from combinations of the primary colours provided.
Assumes additive pigment mixing as defined above."""
global secondary_colours
response = []
for s_col,components in secondary_colours.items():
if set(components) < set(primary_colours):
response.append(s_col)
return response
p1 = ['white','black','yellow']
p2 = ['yellow','white','black','red']
p3 = ['blue','yellow','red']
p4 = ['white','red','blue']
for p in [p1,p2,p3,p4]:
print(p)
print(obtainable_colours(p))
print("\n")
>>>
['white', 'black', 'yellow']
['grey', 'beige', 'brown']
['yellow', 'white', 'black', 'red']
['orange', 'grey', 'beige', 'brown']
['blue', 'yellow', 'red']
['green', 'purple', 'orange']
['white', 'red', 'blue']
['purple']