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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:42:33+00:00 2026-06-01T23:42:33+00:00

I have a dictionary like this: user_dict = { user1: [(video1, 10), (video2, 20),

  • 0

I have a dictionary like this:

user_dict = {
    user1: [(video1, 10), (video2, 20), (video3, 1)]
    user2: [(video1, 4), (video2, 8), (video6, 45)]
    ...
    user100: [(video1, 46), (video2, 34), (video6, 4)]                 
}

(video1, 10) means (videoid, number of request).

Now I want to randomly choose 10 users and do some calculation like

  1. calculate number of videoid for each user.
  2. sum up the number of requests for these 10 random users, etc.

Then I need to increase the random number to 20, 30, 40 respectively.

But random.choice can only choose one value at a time, right? How to choose multiple keys and the list following each key?

  • 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-01T23:42:35+00:00Added an answer on June 1, 2026 at 11:42 pm

    That’s what random.sample() is for:

    Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.

    This can be used to choose the keys. The values can subsequently be retrieved by normal dictionary lookup:

    >>> d = dict.fromkeys(range(100))
    >>> keys = random.sample(list(d), 10)
    >>> keys
    [52, 3, 10, 92, 86, 42, 99, 73, 56, 23]
    >>> values = [d[k] for k in keys]
    

    Alternatively, you can directly sample from d.items().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list like this: [(ip1, video1, 12345.00000),(ip1, video1, 12346.12362),(ip1, video1, 12347.12684),(ip1, video2,12367.12567),(ip2,
I have a dictionary like this: /// <summary> /// Gets the leave entitlement details.
I have a python dictionary of type defaultdict(list) This dictionary is something like this:
Say that i have a generic dictionary with data like this (I hope the
I want to do something like this, I have a dictionary with keys and
I have a string like this: user=u123;name=Test;lastname=User I want to get a dictionary for
I have a dictionary in JSON form, like this: $user = { name: Mary
Suppose I have a Dictionary like so: Dictionary<string, Stream> How can I get a
I have dictionary, like Dictionary<string, bool> accValues = new Dictionary<string, bool>() And I want
I have a string like string strn = abcdefghjiklmnopqrstuvwxyz and want a dictionary 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.