为了账号安全,请及时绑定邮箱和手机立即绑定

检查子字符串数组是否都存在于字符串数组中

检查子字符串数组是否都存在于字符串数组中

翻过高山走不出你 2022-09-13 20:08:18
我有一个有点复杂的问题(对于我有限的Python知识)关于迭代和检查数组是否有任何缺失值。我有一个键字符串数组,我需要检查数组是否包含另一个数组中的所有子字符串。如果没有,我需要输出缺少的内容。例:array1 = ['key/value/one123904', 'key/value/two342389', 'key/value/three234093']array2 = ['one', 'two', 'three', 'four']我的理想输出是说所有元素都存在,如果它们存在,或者在上面的例子中,输出array2array1No key for value: four
查看完整描述

5 回答

?
MM们

TA贡献1886条经验 获得超2个赞

您可以通过迭代子字符串列表来实现此目的,并测试 中的任何键字符串是否包含此子字符串,即:array2array1


for string in array2:

    if not any(string in key_string for key_string in array1):

        print("No key for value: " + string)

        break                                                                   

else:                                                                           

    print("All elements of array2 exist in array1")

如果您不熟悉 的子句,这将仅在循环正常退出时执行,即如果用于提前终止循环,则不会执行。elseforbreak


如果要记录所有不存在的子字符串:


missing = [string for string in array2                                          

           if not any(string in ks for ks in array1)]                           

if missing:                                                                     

    for string in missing:                                                      

        print("No key for value: " + string)                                    

else:                                                                           

    print("All elements of array2 exist in array1")


查看完整回答
反对 回复 2022-09-13
?
米琪卡哇伊

TA贡献1998条经验 获得超6个赞

这是我能够为您的问题制作的方法,


def missing(arr1, arr2):

    #arr1 is the array of strings to be searched

    #arr2 is the array of substrings

    notFound=""

    for i in arr2: # i = each element in array 2

        for j in arr1: # j = each element in array 1

            if i in j: # if substring of i is in an element in j

                break # moves onto next element in the array

            elif j == arr1[-1]: # if not found in the string, checks if  on the last item in the array.

                notFound = notFound+" "+i

    if notFound != "":

        print("No key for value:", notFound)

    else:

        print("all elements of array2 exist in array1")


查看完整回答
反对 回复 2022-09-13
?
慕田峪9158850

TA贡献1794条经验 获得超8个赞

sum_array1 =""

for string1 in array1:

    sum_array1 = sum_array1 + string1 + ","

missing = [string2 for string2 in array2 if string2 not in sum_array1]

if missing:                                                                     

    for string in missing:                                                      

        print("No key for value: " + string)                                    

else:                                                                           

    print("All elements of array2 exist in array1")


查看完整回答
反对 回复 2022-09-13
?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

在一行中:


print(

    "No key for value(s): {}".format(

        " ".join([k for k in array2 if not any(k in v for v in array1) ])

    )

)

或者,如果您想更正确地处理所有值存在的情况


no_match = [k for k in array2 if not any(k in v for v in array1) ]

print(

    "No key for value(s): {}".format(" ".join(no_match))

    if no_match

    else "All keys have values"

)


查看完整回答
反对 回复 2022-09-13
?
慕勒3428872

TA贡献1848条经验 获得超6个赞

array1 = ['key/value/one123904', 'key/value/two342389', 'key/value/three234093']

array2 = ['one', 'two', 'three', 'four']



def does_match_in_array_of_string(key: str, search_list : list) -> bool:

    for item in search_list:

        if key in item:

            return True

    return False;



match_failures = [key for key in array2 if not does_match_in_array_of_string(key, array1)]


if len(match_failures):

    print(f'No key for values: {match_failures}')

else:

    print('All keys have values')


查看完整回答
反对 回复 2022-09-13
  • 5 回答
  • 0 关注
  • 174 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号