简体   繁体   English

如何 append 在 for 循环中创建唯一列表

[英]How to append a unique list created within a for loop

I have a dataframe about movies我有一个关于电影的 dataframe

df = pd.read_csv("https://raw.githubusercontent.com/Giovanni1085/UvA_CSDA_2021/main/assignments/miniproject/Pudding-Film-Dialogue-Clean.csv"

It has the columns of gender and proportion_of_dialogue per film.每部电影都有genderproportion_of_dialogue对话列。 The later shows the proportion of dialogue of each given character and the 'gender' columns shows of cours the gender.后者显示每个给定角色的对话比例,“性别”列显示课程的性别。

I want to create a for loop that goes over every row and creates a new dictionary every time a row gives a new movie after several characters.我想创建一个遍历每一行的 for 循环,并在每次一行在几个字符之后给出一部新电影时创建一个新字典。 I want in this dictionary list of the proportion of dialogue from woman and from man.我想在这本字典中列出女性和男性对话的比例。 This all together goes in it's own dictionary.这一切都在它自己的字典中。 The outcome would be something like:结果将是这样的:

What I have is this (it's a lot):我所拥有的是这个(很多):

proportion_per_film_dictionary  = {}
prop_woman = 0
prop_man = 0

for i in range(len(df.index)):
  if df.loc[i,'index1'] == 0:           #I made a column with the index so I could do this for the first row
    dct['film_%s' %i] = []
    if df.loc[i, 'gender'] == 'woman':
      prop_woman = prop_woman + df.loc[i,'proportion_of_dialogue']
    if df.loc[i,'gender'] == 'man':
      prop_man = prop_man + df.loc[i,'proportion_of_dialogue']
    if df.loc[i,'title'] != df.loc[i+1,'title']:
      dct['film_%s'%i].append(prop_man)
      dct['film_%s'%i].append(prop_woman)
      proportion_per_film_dictionary.append(dct['film_%s'%i])
      prop_woman = None
      prop_man = None
  
  elif df.loc[i,'title'] == df.loc[i+1,'title']:
    if df.loc[i, 'gender'] == 'woman':
      prop_woman = prop_woman + df.loc[i,'proportion_of_dialogue']
    if df.loc[i,'gender'] == 'man':
      prop_man = prop_man + df.loc[i,'proportion_of_dialogue']
    
  elif df.loc[i,'title'] != df.loc[i+1, 'title']:
    if df.loc[i, 'gender'] == 'woman':
      prop_woman = prop_woman + df.loc[i,'proportion_of_dialogue']
    if df.loc[i,'gender'] == 'man':
      prop_man = prop_man + df.loc[i,'proportion_of_dialogue']
    if df.loc[i,'title'] != df.loc[i+1,'title']:
      dct['film_%s'%i].append(prop_man)
      dct['film_%s'%i].append(prop_woman)
      proportion_per_film_dictionary.append(dct['film_%s'%i])
      prop_woman = None
      prop_man = None

This code shows the iteration over every movie title, I first also tried i-1 but apparently Python doesn't do -1 , although it does +1 .这段代码显示了每个电影标题的迭代,我首先也尝试了i-1但显然 Python 没有-1 ,虽然它有+1

I now get the following error:我现在收到以下错误:

dict' object has no attribute 'append' dict' object 没有属性 'append'

I don't know why.我不知道为什么。 Anybody who can help me?有谁能帮助我吗? Thank you谢谢

In python dictionaries you don't use append , that's for lists.在 python 字典中,您不使用append ,这是用于列表的。 For dictionaries, it's enough to add a new value by assigning a key.对于字典,通过分配一个键来添加一个新值就足够了。 Like this:像这样:

dct['film_%s'%i] = prop_man
dct['film_%s'%i] = prop_woman

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM