Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8035093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:12:49+00:00 2026-06-05T02:12:49+00:00

I have this dataset. I want to update the MySQL table. I can do

  • 0

I have this dataset. I want to update the MySQL table. I can do it in the current form but I thought a conversion to dictionary will shrink the list to be updated.

My dataset :

dataset = [('121', set(['NY'])), ('132', set(['CA', 'NY'])), ('198', set(['NY'])), ('676', set(['NY'])), ('89', set(['NY', 'CA']))]

Desired output :

A dictionary :

output = {'set(['NY'])':121,198,676, 'set(['CA', 'NY'])':132,89}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:12:50+00:00Added an answer on June 5, 2026 at 2:12 am

    You must use a frozenset for the key. There is no guarantee that a set with the same elements will always be turned into the same repr or tuple as sets are unordered. Unless you sort the set elements first of course, but that seems wasteful

    from collections import defaultdict
    
    dataset = [('121', set(['NY'])), ('132', set(['CA', 'NY'])), ('198', set(['NY'])), ('676', set(['NY'])), ('89', set(['NY', 'CA']))]
    output = defaultdict(list)
    for value, key in dataset:
        output[frozenset(key)].append(value)
    

    or using a sorted tuple

    from collections import defaultdict
    
    dataset = [('121', set(['NY'])), ('132', set(['CA', 'NY'])), ('198', set(['NY'])), ('676', set(['NY'])), ('89', set(['NY', 'CA']))]
    output = defaultdict(list)
    for value, key in dataset:
        output[tuple(sorted(key))].append(value)
    

    Random example to illustrate this

    >>> s,t = set([736, 9753, 7126, 7907, 3350]), set([3350, 7907, 7126, 9753, 736])
    >>> s == t
    True
    >>> tuple(s) == tuple(t)
    False
    >>> frozenset(s) == frozenset(t)
    True
    >>> hash(tuple(s)) == hash(tuple(t))
    False
    >>> hash(frozenset(s)) == hash(frozenset(t))
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code so I can grab the values ind the dataset before
I have a question similar to this one , but my dataset is a
I have a DataSet with some Datatables and I am saving this DataSet as
I have the dataset (pts) like this: x <- seq(-124.25,length=115,by=0.5) y <- seq(26.25,length=46,by=0.5) z
This is the code I have: Sub Main() Dim dts As New DataSet Dim
I have created Windows Application. In this, I have multiple tables in dataset, now
I have this string 2012-06-27 16:17:06 and I want to convert it to GMT
i have a table with hierarchical structure. like this: (source: aspalliance.com ) and table
Let's say I have a MySQL database with 3 tables: table 1: Persons, with
I have code that looks like this: var ds = new DataSet(); var fooIDToFoo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.