如何创建元组列表?

2024-09-28 21:59:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文档,它是一个元组列表。 每个元组中的每个元素都是(word, label)对。你知道吗

基本上,文档是一个句子列表,其中每个句子都是一个元组列表。你知道吗

我试图忽略出现次数少于10次的单词,并按照以前的格式构建一个新文档。 为此,我使用以下代码:

     i=0;
     j=0;
     dictWords=dict()
     for sentence in ldata:
       for word in sentence:
        j=j+1
        if word[0] not in dictWords:
           dictWords[word[0]]=1
           i=i+1
        else:
           dictWords[word[0]]=1+dictWords[word[0]]

    ldata=[[("the","det"),("boy","noun"),("is",'verb'),("ugly","adj")], [("I","Pronoun"), ("am","verb") ("here" ,"Place")]
    lnewdata = []

    i = 0
    for sentence in ldata:
       newSent = []
       for word in sentence:
       if dictWords[word[0]] < 10:
          newSent.append(("unk","unk"))
          #dictWords is a dictionnary containing each word's occurences
       else:
          newSent.append(word)   
          i = i + 1
lnewdata.extend(newSent)

我的问题是lnewdata的格式如下:

[["the" "det" "boy" "noun" "is" "verb" "ugly" "adj"] ["I" "noun" "am" "verb" "here" "Place" ]

你建议怎么解决这个问题?你知道吗


Tags: in文档列表forissentence句子word
1条回答
网友
1楼 · 发布于 2024-09-28 21:59:15

为了理解你的问题,我基本上能弄明白以下几点。你知道吗

  1. 您有一个文档,它有许多元组,如(word,label)

  2. 您需要出现次数超过10次的单词并创建一个新列表。

我不明白你为什么要添加出现次数小于10的元组。你知道吗

这是我能找出的密码。你知道吗

    lnewdata=[]
    i=0;
    for sentence in ldata:
       newSent=[]
       for word in sentence:
       if dictWords.count(word[0])>10:
          newSent.append((word[0],word[1]))
    lnewdata = list(newSent)

相关问题 更多 >