简体   繁体   English

Python。 我如何制作一个更大列表的小部分列表?

[英]Python. How do I make a list of small portions of a bigger list?

for word in list6:
    if word = "TRUMP":

So, I have a list of every word in a debate transcript.所以,我有一个辩论记录中每个单词的列表。 When Trump speaks, it starts with "TRUMP".当特朗普说话时,它以“TRUMP”开头。 I need to take his words and put them into a seperate list.我需要把他的话放在一个单独的列表中。 If the word in list6 is "TRUMP", then I need to put everything into a list until it says another person's name.如果 list6 中的单词是“TRUMP”,那么我需要将所有内容放入一个列表中,直到它说出另一个人的名字。 He speaks more than once.他不止一次讲。

I just need help completing this loop.我只需要帮助完成这个循环。

list6 = ['TRUMP','I','am','good', 'HILLARY','I','am','good','too','TRUMP','But','How?']
person_words = {'TRUMP':[], 'HILLARY':[]}

person_names = person_words.keys()

one_person_onetime_words = []

for word in list6:
    if word in person_names:
        if len(one_person_onetime_words):
            person_words[this_person].append(one_person_onetime_words)
            one_person_onetime_words = []
        this_person = word
    else:
        one_person_onetime_words.append(word)

person_words[this_person].append(one_person_onetime_words)

print person_words

Gives

{'HILLARY': [['I', 'am', 'good', 'too']], 'TRUMP': [['I', 'am', 'good'], ['But', 'How?']]}

So, this in a single shot gives all the different talks by all the persons.所以,这一次就给出了所有人的所有不同的谈话。

As mentioned by you in the comments to your question, if you want to get one person's words only you can use the following:正如您在对问题的评论中所提到的,如果您只想获得一个人的话,您可以使用以下内容:

from copy import copy

list6 = ['TRUMP','I','am','good', 'HILLARY','I','am','good','too','TRUMP','But','How?']
person_words = []
all_persons = ['TRUMP', 'HILLARY']
person_looking_for = 'TRUMP'

filter_out_persons = copy(all_persons)
filter_out_persons.remove(person_looking_for)

person_onetime_words = []

capture_words = False
for word in list6:
    if word == person_looking_for:
        capture_words = True
        if len(person_onetime_words):
            person_words.append(person_onetime_words)
            person_onetime_words = []
    elif word not in filter_out_persons and capture_words:
        person_onetime_words.append(word)
    else:
        capture_words = False

person_words.append(person_onetime_words)
print "{}'s words".format(person_looking_for)
print person_words

That gives那给

TRUMP's words
[['I', 'am', 'good'], ['But', 'How?']]

And, the following will give a dictionary with words as keys and the value will be a dictionary again with frequency of each person for that word.并且,下面将给出一个以单词为键的字典,值将再次是一个字典,每个人对该单词的频率。

import pprint

list6 = ['TRUMP','I','am','good', 'HILLARY','I','am','good','too','TRUMP','But','How?']

person_names = ['TRUMP','HILLARY']

word_frequency = {}
for word in list6:
    if word in person_names:
        person = word
    else:
        word = word.lower()
        if word in word_frequency:
            if person in word_frequency[word]:
                word_frequency[word][person] += 1
            else:
                word_frequency[word][person] = 1
        else:
            word_frequency[word] = {person: 1}

pprint.pprint(word_frequency)

Gives

{'am': {'HILLARY': 1, 'TRUMP': 1},
 'but': {'TRUMP': 1},
 'good': {'HILLARY': 1, 'TRUMP': 1},
 'how?': {'TRUMP': 1},
 'i': {'HILLARY': 1, 'TRUMP': 1},
 'too': {'HILLARY': 1}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在给定的列表中,如何确定大数字后面的小数字? (Python) - In a given list, how to determine the small number that followed a bigger number? (Python) 我怎样才能从一个更大的阵列做一个小阵列? Python - How can I make a small array from a bigger one? Python 我想使用python中的列表理解一一打印列表中的项目。 怎么能这样做? - I want print items in list one by one using list comprehension in python. How can do that? 我如何制作列表python的列表 - how do i make list of list python 在Python中使用Tkinter。 如何一次将一个列表变量调用到画布中 - With Tkinter in Python. How do I call a list variable into a canvas one at a time 如何从列表中删除所有元素,该列表是 python 中同一列表中另一个更大元素的子序列? - How do I remove all elements from a list which is a subsequence of another bigger element in the same list in python? 我在python中有一个扁平化的列表。 如何在不限制每行元素数的同时将列表的元素写入文件? - I have a flattened list in python. How do I write the elements of the list into a file, while restricting the number of elements per line? Python。 如何对包含字符串和整数的列表求和? - Python. How can I sum a list with strings and integers? 我想在 python 中运行小型 Julia 代码/模块。 我该怎么做? - I want to run small Julia code/modules in python. How should I do that? 蟒蛇。 更改列表 - Python. Change list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM