Limit items in a dictionary

Asked

Viewed 123 times

0

I am trying to analyze data from a research of own authorship, the files are already tabulated in .xlsx. format I use openpyxl.

My intention at that time is to take the state of the participants + their opinion and play in a dictionary.

The problem is that this dictionary is returning only 26 items from random positions.

The solution that I seek is: create a dicio with states:opinions with all 318 research participants.

My code:

estados = []
u_opiniao = []
estados_x_opiniao = {}

for rowOfCellObjects in ws['B2':'B319']:
    for cellObj in rowOfCellObjects:
        valorestado = str(cellObj.value)
        estados.append(valorestado)

for rowOfCellObjects in ws['J2':'J319']:
    for cellObj in rowOfCellObjects:
        opiniao_do_u = str(cellObj.value)
        u_opiniao.append(opiniao_do_u)

for i in estados:
    estado = i
    for i in u_opiniao:
        opscen = i
        estados_x_opiniao[estado] = opscen

Return

Respostas(total):  318
Total de itens no estados_x_opiniao: 26
  • 2

    In a dictionary you cannot create equal keys with different values. The dictionary defines a 1:1 relational map, so it won’t be possible to do what you want. A simple alternative I see is to define the dictionary with the status as key and a list of comments as value.

  • Great suggestion !

1 answer

0

Do you have more than one opinion per state? For your code will only take the last opinion of a state and keep.

To store more than one opinion per state, create a list of values. Example:

estados_x_opiniao[estado] = [] 
estados_x_opiniao[estado].append(opscen)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.