Home > Enterprise >  Extract words from list of lists and store them in a separate variable in python
Extract words from list of lists and store them in a separate variable in python

Time:06-18

I have this list of lists and I would like to extract each word from the list and store them in a separate variable

keywords=[[('tarifa',), ('mantenimiento',), ('mensual',)],
[('tasa',), ('anual',)],
[('tasa',)],
[('int ',)],
[('comision',)],
[('seguro',), ('bancaria',)],
[('seguro',), ('generales',)],
[('mi salud',), ('unific',)]]

So for each list you should have kw variables, for example for the first list: kw1='tarifa' kw2='mantenimiento' kw3 = 'mensual' For the second list kw1='tasa' kw2='anual' and so on

I have this code:

for i in range(len(keywords)):
    for j in range(len(keywords[i])):
        print("'" keywords[i][j][0] "'",end=",")
    print()

but it only returns this:

'tarifa','mantenimiento','mensual',
'tasa','anual',
'tasa',
'int ',
'comision',
'seguro','bancaria',
'seguro','generales',
'mi salud','unific',

CodePudding user response:

Not sure what you mean store them in a separate variable. You can use python dictionaries to associate a value with a key (or variable). Here's an example where we extract each list of tuples and convert to dictionary with key starting with 'kw' followed by its index and associated tuple as value.
Hope this helps.

>>> l = [[('tarifa',), ('mantenimiento',), ('mensual',)],
... [('tasa',), ('anual',)],
... [('tasa',)],
... [('int ',)],
... [('comision',)],
... [('seguro',), ('bancaria',)],
... [('seguro',), ('generales',)],
... [('mi salud',), ('unific',)]]
>>> z = []
>>> for x in l:
...   y = [item[0] for item in x]
...   z.append({f'kw{i}':v for i,v in enumerate(y)})
... 
>>> z
[{'kw0': 'mi salud', 'kw1': 'unific'}, {'kw0': 'mi salud', 'kw1': 'unific'}, {'kw0': 'mi salud', 'kw1': 'unific'}, {'kw0': 'mi salud', 'kw1': 'unific'}, {'kw0': 'mi salud', 'kw1': 'unific'}, {'kw0': 'mi salud', 'kw1': 'unific'}, {'kw0': 'mi salud', 'kw1': 'unific'}, {'kw0': 'mi salud', 'kw1': 'unific'}]
>>> 

Update:

Based on discussion. You dont need to convert list of tuples to variables. Simply convert to list of strings and iterate through each to match string from list to text from dataframe. Here's sample text that contains some of your words from list of lists.

sents =
['Lorem tarifa ipsum dolor sit amet consectetur adipiscing elit', 'Aenean eget neque feugiat sem porta lobortis.', 'Sed molestie mantenimiento nunc sagittis, tincidunt tortor eget tristique massa.', 'Proin imperdiet velit eu bibendum volutpat.', 'Cras ullamcorper quam non urna  mensual vestibulum gravida', 'Maecenas semper ante in placerat tristique']

We then convert list of tuples to list of string and iterate and search for words in above list of texts. You can tailor this to your needs. This is to provide an idea of solution.

>>> for z in [ [y[0] for y in x] for x in keywords ]:
...   for w in z:
...     for s in sents:
...       if w in s:
...         print(f"found '{w}' in '{s}'")
... 
found 'tarifa' in 'Lorem tarifa ipsum dolor sit amet consectetur adipiscing elit'
found 'mantenimiento' in 'Sed molestie mantenimiento nunc sagittis, tincidunt tortor eget tristique massa.'
found 'mensual' in 'Cras ullamcorper quam non urna  mensual vestibulum gravida'

CodePudding user response:

kw =[(' tarifa '), (' mantenimiento '), (' mensual ')]

kw1 = kw[1]

kw2 = kw[0]

print(kw2)

output: tarifa

You seemed new to python, so I would make it easier for you, please use the index function by sorting out the specific index number of the list your want to give a new variable. try out my examples and removed the commas you used inside the list it will create an error when you run it.

  • Related