Python Forum
list comprehension of tuples
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list comprehension of tuples
#1
Hi

Is it possible to have a list comprehension so that I can end up with the following result:
file_word_list=[('f1',[('you',0.4),('jump',0.12),('the',0.48)]), ('f2',[('hi',0.167),('at',0.333),('you',0.5)])]
based on this:
file_word_list=[('f1',[('you',10),('jump',3),('the',12)]), ('f2',[('hi',2),('at',4),('you',6)])]
I have a function, which I think gives me what I want, but I was wondering if I could use a comprehension instead. I am unsure how to split the inner tuple though within a comprehension. Also, as you can see from the code, wc_lst is iterated over twice, once to obtain the total word count, and a second time to generate the output and to calculate the term frequency. Would it be possible to only iterate over once?


This is the function:
def calculate_term_frequency(f_wc):
    f,wc_lst = f_wc
    qty = 0
    wctn = []

    for wc in wc_lst:
        w,c = wc    # splitting the inner tuple
        qty = qty + c

    for wc in wc_lst:
        w,c = wc
        wctn.append( (w,c/qty))

    return (f,wctn)
Reply
#2
Technically, you can:

file_word_freq = [(f, [(word, count / sum([wc[1] for wc in counts])) for word, count in counts]) for f, counts in file_word_list]
However, it's very confusing and rather inefficient. Maybe someone better than me with list comprehensions could fix the inefficiency, but I think it would still be a mess. What's wrong with using the function?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
It may look impressive at the end but more important is to be readable.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 506 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 470 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,246 Apr-02-2023, 06:31 PM
Last Post: tester_V
  Adding values with reduce() function from the list of tuples kinimod 10 2,636 Jan-24-2023, 08:22 AM
Last Post: perfringo
  list comprehension 3lnyn0 4 1,405 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List comprehension used differently coder_sw99 3 1,710 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,824 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,236 Jun-08-2021, 08:29 AM
Last Post: cametan
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,458 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,213 Jan-02-2021, 04:24 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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