【发布时间】:2017-01-25 06:06:27
【问题描述】:
假设我在 python [[],[]] 中有一个双重列表:
doublelist = [["all", "the", "big", "dogs", "eat", "chicken", "all", "the", "small", "kids", "eat", "paste"],
["the", "big", "dogs", "eat", "chicken", "all", "the", "small", "kids", "eat", "paste", "lumps"]]
我想计算doublelist[0][0] & doublelist[1][0] = all, the 在双重列表中出现的次数。第二个 [] 是索引。
例如,您在doublelist[0][0] doublelist[1][0] 看到一个计数,在doublelist[0][6] doublelist[1][6] 看到另一个计数。
我将在 Python 3 中使用什么代码来遍历 doublelist[i][i] 获取每个值集 ex。 [["all"],["the"]] 以及一个整数值,表示该值集在列表中存在的次数。
理想情况下,我想将其输出到包含[i][i] 值和第三个[i] 中的整数的三重列表triplelist[[i],[i],[i]]。
示例代码:
for i in triplelist[0]:
print(triplelist[0][i])
print(triplelist[1][i])
print(triplelist[2][i])
输出:
>"all"
>"the"
>2
>"the"
>"big"
>1
>"big"
>"dogs"
>1
等等……
此外,它最好跳过重复项,因此[i][i][i] = [[all],[the],[2]] 的列表中不会有 2 个索引,因为原始列表中有 2 个实例([0][0] [1][0] & [0] [6] [1][6])。我只想要所有唯一的双组词以及它们在原文中出现的次数。
代码的目的是查看给定文本中一个词跟随另一个词的频率。它本质上是为了构建一个智能马尔可夫链生成器,它对单词值进行加权。为此,我已经有代码将文本分成一个双重列表,其中包含第一个列表中的单词和第二个列表中的以下单词。
这是我当前的参考代码(问题是在我初始化 wordlisttriple 之后,我不知道如何让它按照我上面描述的那样做):
#import
import re #for regex expression below
#main
with open("text.txt") as rawdata: #open text file and create a datastream
rawtext = rawdata.read() #read through the stream and create a string containing the text
rawdata.close() #close the datastream
rawtext = rawtext.replace('\n', ' ') #remove newline characters from text
rawtext = rawtext.replace('\r', ' ') #remove newline characters from text
rawtext = rawtext.replace('--', ' -- ') #break up blah--blah words so it can read 2 separate words blah -- blah
pat = re.compile(r'([A-Z][^\.!?]*[\.!?])', re.M) #regex pattern for grabbing everthing before a sentence ending punctuation
sentencelist = [] #initialize list for sentences in text
sentencelist = pat.findall(rawtext) #apply regex pattern to string to create a list of all the sentences in the text
firstwordlist = [] #initialize the list for the first word in each sentence
for index, firstword in enumerate(sentencelist): #enumerate through the sentence list
sentenceindex = int(index) #get the index for below operation
firstword = sentencelist[sentenceindex].split(' ')[0] #use split to only grab the first word in each sentence
firstwordlist.append(firstword) #append each sentence starting word to first word list
rawtext = rawtext.replace(', ', ' , ') #break up punctuation so they are not considered part of words
rawtext = rawtext.replace('. ', ' . ') #break up punctuation so they are not considered part of words
rawtext = rawtext.replace('"', ' " ') #break up punctuation so they are not considered part of words
sentencelistforwords = [] #initialize sentence list for parsing words
sentencelistforwords = pat.findall(rawtext) #run the regex pattern again this time with the punctuation broken up by spaces
wordsinsentencelist = [] #initialize list for all of the words that appear in each sentence
for index, words in enumerate(sentencelist): #enumerate through sentence list
sentenceindex = int(index) #grab the index for below operation
words = sentencelist[sentenceindex].split(' ') #split up the words in each sentence so we have a nested lists that contain each word in each sentence
wordsinsentencelist.append(words) #append above described to the list
wordlist = [] #initialize list of all words
wordlist = rawtext.split(' ') #create list of all words by splitting the entire text by spaces
wordlist = list(filter(None, wordlist)) #use filter to get rid of empty strings in the list
wordlistdouble = [[], []] #initialize the word list double to contain words and the words that follow them in sentences
for index, word in enumerate(wordlist): #enumerate through word list
if(int(index) < int(len(wordlist))-1): #only go to 1 before the end of list so we don't get an index out of bounds error
wordlistindex1 = int(index) #grab index for first word
wordlistindex2 = int(index)+1 #grab index for following word
wordlistdouble[0].append(wordlist[wordlistindex1]) #append first word to first list of word list double
wordlistdouble[1].append(wordlist[wordlistindex2]) #append following word to second list of word list double
wordlisttriple = [[], [], []] #initialize word list triple
for index, unit in enumerate(wordlistdouble[0]): #enumerate through word list double
word1 = wordlistdouble[0][index] #grab word at first list of word list double at the current index
word2 = wordlistdouble[1][index] #grab word at second list of word list double at the current index
count = 0 #initialize word double data set counter
wordlisttriple[0].append(word1) #these need to be encapsulated in some kind of loop/if/for idk
wordlisttriple[1].append(word2) #these need to be encapsulated in some kind of loop/if/for idk
wordlisttriple[2].append(count) #these need to be encapsulated in some kind of loop/if/for idk
#for index, unit1 in enumerate(wordlistdouble[0]):
#if(wordlistdouble[0][int(index)] == word1 && wordlistdouble[1][int(index)+1] == word2):
#count++
#sentencelist = list of all sentences
#firstwordlist = list of words that start sentencelist
#sentencelistforwords = list of all sentences mutated for ease of extracting words
#wordsinsentencelist = list of lists containing all of the words in each sentence
#wordlist = list of all words
#wordlistdouble = dual list of all words plus the words that follow them
任何建议将不胜感激。如果我以错误的方式解决这个问题,并且有一种更简单的方法可以完成同样的事情,那也将是惊人的。谢谢!
【问题讨论】:
-
doublelist[i][i] = [[all], [the]]是什么意思? -
例如 doublelist[0][0] = [["all"],["the"]] 和 doublelist[6][6] = [["all"],["the "]]
-
没有。
doublelist[0][0] == 'all' -
抱歉,我的措辞不正确。我只是引用第一个和第二个列表的索引。我的意思是:
doublelist = [["all", "the", "big", "dogs", "eat", "chicken", "all", "the", "small", "kids", "eat", "paste"], ["the", "big", "dogs", "eat", "chicken", "all", "the", "small", "kids", "eat", "paste", "lumps"]] print(doublelist[0][0]) print(doublelist[1][0])将返回 >all >the 。对不起,我只是措辞不佳。我在上面的文本中取出了第一组 [0] 和 [1]。我会尽量用词更好。
标签: python list python-3.x nested-lists counting