两个list中相同的则标1,否则标0,如何实现?

依依雨柔
  • 233

例: 输入: PP SBAR WHNP VP NP ADVP

         PP SBAR WHNP NP
  输出: [1,1,1,0,1,0]

如何实现?

回复
阅读 2.3k
4 个回答
藕丝空间
  • 1.7k
✓ 已被采纳

图片描述

list1 = ["aa", "bb", "cc","dd","ee","ff"]
list2 = ["aa", "cc", "cc", "dd"]

[1 if l1 in list2 else 0 for l1 in list1 ]
import pandas as pd
a = pd.Series(list1)
b = pd.Series(list2)
c = a.isin(b).astype(int)

var list1 = ["aa", "bb", "cc","dd","ee","ff"];

        list2 = ["aa", "cc", "cc", "dd"],
        newList=[],
        max=0;
        if(list1.length>list2.length){
            max=list1.length;
        }else{
            max=list2.length
        }
        for(let i=0;i<max;i++){
            if(list1[i]==list2[i]){
                newList.push("1");
            }else{
                newList.push("0");
            }
        }
        console.log(newList);
        

用js写了一个python应该也差不多吧

遍历
for i in list1

if i in list2:
  flag[i]=1
else:
    flag[i]=0
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏