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 8887337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:47:01+00:00 2026-06-14T21:47:01+00:00

Assume you have a data set as something like a CSV file that contains

  • 0

Assume you have a data set as something like a CSV file that contains mildly sensitive information, like who passed a note to whom in a 12 Grade English class. While it’s not a crisis if this data got out, it would be nice to strip out the identifying information so the data could be made public, shared with collaborators, etc. The data looks something like this:

Giver, Recipient:

Anna,Joe
Anna,Mark
Mark,Mindy
Mindy,Joe

How would you process through this list, assign each name a unique but arbitrary identifier, then strip out the names and replace them with said identifier in Python such that you end up with something like:

1,2
1,3
3,4
4,2

  • 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-14T21:47:02+00:00Added an answer on June 14, 2026 at 9:47 pm

    you can use hash() to generate a unique arbitrary identifier, it will return always return same integer for a particular string:

     with open("data1.txt") as f:
        lis=[x.split(",") for x in f]
        items=[map(lambda y:hash(y.strip()),x) for x in lis]
        for x in items:
            print ",".join(map(str,x))
       ....:         
    
    
    -1319295970,1155173045
    -1319295970,-1963774321
    -1963774321,-1499251772
    -1499251772,1155173045
    

    or you can also use iterools.count:

    In [80]: c=count(1)
    
    In [81]: with open("data1.txt") as f:
        lis=[map(str.strip,x.split(",")) for x in f]
        dic={}
        for x in set(chain(*lis)):
            dic.setdefault(x.strip(),next(c))
        for x in lis:    
            print ",".join(str(dic[y.strip()]) for y in x)
       ....:         
    3,2
    3,4
    4,1
    1,2
    

    or improving my previous answer using the unique_everseen recipe from itertools, you can get the exact answer :

    In [84]: c=count(1)
    
    In [85]: def unique_everseen(iterable, key=None):
            seen = set()
            seen_add = seen.add
            if key is None:
                    for element in ifilterfalse(seen.__contains__, iterable):
                            seen_add(element)
                            yield element
                    else:
                            for element in iterable:
                                    k = key(element)
                                    if k not in seen:
                                            seen_add(k)
                                            yield element
       ....:                         
    
    In [86]: with open("data1.txt") as f:
        lis=[map(str.strip,x.split(",")) for x in f]
        dic={}
        for x in unique_everseen(chain(*lis)):
            dic.setdefault(x.strip(),next(c))
        for x in lis:    
            print ",".join(str(dic[y.strip()]) for y in x)
       ....:         
    1,2
    1,3
    3,4
    4,2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets assume I have two hashes. One of them contains a set of data
Let's assume that we have a data frame x which contains the columns job
Assume I have a application that stores data,gets data and processes data and stores
Let's assume I have the following file structure: data.py foo = [] bar =
Assume that I have the ItemSource bound and i am getting the data. Now
Assume I have some program that pulls airline data. Each airline data uses a
I want to set a background color in a cell.. something like: so I
I have an InputStream that is set up to handle local files, but want
I have a View that is pretty complex. The query pulls data from about
I've written a procedure to dump table data out to a text file like

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.