Python Forum
Organizing Data in Dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Organizing Data in Dictionary
#1
Hello All,

I'm trying to pull the data from excel and organize it inside the dictionary for further usage. some how I'm failing, please help.

Input File is like:
No Ticket User
1 P12334 Raju
2 P12335 Usha
3 P12336 Suri
4 P12337 Ben
5 P12338 Raju
6 P12339 Roy
7 P12340 Ben
8 P12341 Raju
9 P12342 Roy
10 P12343 Usha
11 P12344 Ben
12 P12345 Roy
13 P12346 Usha

I need output as:
In a dictionary
Raju - P12334, P12337, P12338
Ben - P12340, P12343
Usha - P12350, P12351, P12327

But I'm not getting it, the dictionary is having only one ticket information for each user (key). I'm unable to append or add values. Could you please help?

values = {}
for i in range(sheet.nrows):
    row = sheet.row_values(i)
    print(row[2])
    values[row[2]]={
        'Backlogs':row[1]
    }

#pprint.pprint(values)
print(values)
Reply
#2
There is no reason to have "Backlogs" in the dictionary if it is the one and only entry. To have more than on entry per user, use a list.
values = {}
for i in range(sheet.nrows):
    row = sheet.row_values(i)
    this_value=row[2]  ## save looking up row[2] multiple times
    print(this_value)
    if this_value not in values:
        values[this_value] = []
    values[this_value].append(row[1])
 
#pprint.pprint(values)
print(values)
Reply
#3
Another possibility is to use .setdefault method (instead of rows 6-8):

values.setdefault(this_value, []).append(row[1])
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Thank you both of you. It worked
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matching Data - Help - Dictionary manuel174102 1 353 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  Organizing several similar classes with overlapping variables 6hearts 7 1,328 May-07-2023, 02:00 PM
Last Post: 6hearts
  [SOLVED] Concat data from dictionary? Winfried 4 1,666 Mar-30-2022, 02:55 PM
Last Post: Winfried
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,523 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  Converting data in CSV and TXT to dictionary kam_uk 3 1,948 Dec-22-2020, 08:43 PM
Last Post: bowlofred
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 2,167 Jul-25-2020, 06:12 AM
Last Post: LuisSatch
  Organizing list of objects by attribute scarney1988 3 2,155 Mar-11-2020, 03:55 PM
Last Post: scarney1988
  problem coverting string data file to dictionary AKNL 22 6,263 Mar-10-2020, 01:27 PM
Last Post: AKNL
  Read csv file, parse data, and store in a dictionary markellefultz20 4 4,485 Nov-26-2019, 03:33 PM
Last Post: DeaD_EyE
  Dictionary for Excel Data Ranjirock 0 22,153 Aug-18-2019, 05:26 PM
Last Post: Ranjirock

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020