how to group element's of a list with respect of some information in each elements?

elham

I have a list. each element's of my list is like this:

list[0]={'Keywords': ' foster care case aide ',
 'categoryId': '1650',
 'result': {'categoryId': '1650',
  'categoryName': 'case aide',
  'score': '1.04134220123291'}}

can I collect all keywords whose have the same categoryId in the same group. and count for each categoryId how many keywords do I have ?

please let me know if it is not possible

Tom

You could use the collections.defaultdict to make a set for each categoryId and add the associated words:

from collections import defaultdict

output = defaultdict(set)

for entry in list:
    kwds = entry['Keywords'].strip().split(' ')
    for word in kwds:
        output[entry['categoryId']].add(word)

I'm using a set because I assumed you don't want repeats of words within each categoryId. You could instead use a list or some other collection.

You can then get out the size of each ID:

for k, v in output.items():
    print(f'ID: {k}, words: {len(v)}')

# ID: 1650, words: 4

Responding to the comments from OP:

If you are getting KeyError: 'categoryId', that means some entries do not have the key 'categoryId'. If you want to simply skip those entries, you can add a small catch into the above solution:

for entry in list:
    # catch if there is a missing ID field
    if entry.get('categoryId', None) is None: 
        break
  
    # otherwise the same
    kwds = entry['Keywords'].strip().split(' ')
    for word in kwds:
        output[entry['categoryId']].add(word)

If there is no categoryID, the loop will break, and the entry will be skipped.

Note that we are also depending on a "Keywords" field being there as well, so you may need to add a catch for that as well.

Or, if you want to collect all the keywords from entries without an ID, you can just use dict.get() in the original solution:

for entry in data:
    kwds = entry['Keywords'].strip().split(' ')
    for word in kwds:
        output[entry.get('categoryId', None)].add(word)

Now if there is no categoryId, the keywords will be added to the key None in output.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to get some elements after specified element then put these elements into each independent List

Filter list's elements by type of each element

Self generating list where each element appends some elements to the end

Make a list element of each group with dplyr's group_by function

Groovy List : Group By element's count and find highest frequency elements

how to determine duplicate rows with respect of a group and then select whole element of that group

How to group elements of a list that are within n of each other

How to apply an Arithmetic operation on each Group of elements with the same ID in the list

how to remove the first row of some of the group's elements?

how pick some elements of a group who have some relation with each other other?

How do I remove elements in a list based on values in each element?

how we can choice some group's element

How to group by elements of a list

How to group elements of a list?

How to sort a list of tuples by the first element in each tuple, and pick the tuple with the largest last element in each group

how to filter all the elements of the list in R? but the filter will use the mean of each element so the condition will change for each element

How to use Collectors.groupingBy to instead of list of grouped elements get sum of some property of elements in same group?

How to use Collectors.groupingBy to instead of list of grouped elements get sum of some property of elements in same group?

How to use Collectors.groupingBy to instead of list of grouped elements get sum of some property of elements in same group?

execute some code for each element of a list in batch

How to repeat list's elements with each different n times and in Python?

How to iterate over each string in a list of strings and operate on it's elements

Deserialize XML list of elements of string element each

How to apply some changes to each element in List<List<>> structure using Java 8 approach

How to switch to another view by each element's onTapGesture of a list in SwiftUI?

How to print a list with each element's index? - Python

how to change color for each element in a group of identical elements in sequence with a color array

How to group elements of a nested list?

How to get all elements in a nested list where the first element of each list is a particular number?