Anastasia Romanova
Anastasia Romanova

Reputation: 23

operations with nested lists in python

I'm trying to iterate through a nested list and make some changes to the elements. After changing them I'd like to save results in the same nested list. For example, I have

text = [['I', 'have', 'a', 'cat'], ['this', 'cat', 'is', 'black'], ['such', 'a', 'nice', 'cat']]

I want to get a list of lists with elements slightly changed. For example:

text = [['I_S', 'have', 'a_A', 'cat'], ['this', 'cat_S', 'is', 'black_A'], ['such', 'a', 'nice', 'cat_S']]

Firstly, I go through each list, then go through each item in a list and then apply additional code to make changes needed. But how to return the nested list back after operations? This is what I do:

for tx in text:
    for t in tx:
        #making some operations with each element in the nested list.
        #using if-statements here
    result.append()

And what I've got the single list with all the changed elements from the nested list

result = ['I_S', 'have', 'a_A', 'cat', 'this', 'cat_S', 'is', 'black_A', 'such', 'a', 'nice', 'cat_S']

I need to keep the nested list because it's actually the sentences from the text.

Upvotes: 2

Views: 3052

Answers (4)

Aakash Goel
Aakash Goel

Reputation: 1030

To keep your modified results in same original list in nested form is possible. And single Line code will work for this.

You can try simply:

text = [['I', 'have', 'a', 'cat'], ['this', 'cat', 'is', 'black'], ['such', 'a', 'nice', 'cat']]

map(lambda x:map(function,x),text)

And this function definition you can write as per your requirement like:

def function(word):
  #modification/Operations on Word
  return word

Upvotes: 0

Moses Koledoye
Moses Koledoye

Reputation: 78556

To make your code readable, you can use a nested list comprehension to create the resulting list and define a function that appends the extra strings to the appropriate text.

result_text = [[word_processor(word) for word in word_cluster] for word_cluster in text]

You function will be of the form:

def word_processor(word):
     # use word lengths to determine which word gets extended
     if len(word) > 1 and isintance(word, str):
          return word + "_S"
     else:
          # Do nothing 
          return word

The function strictly depends on what you're trying to achieve.

Upvotes: 1

galaxyan
galaxyan

Reputation: 6111

[[item + change if needchange else item for item in lst ] for lst in test ]

or

def unc(item):
   #do something
   return res
[[func(item) for item in lst ] for lst in test ]

Upvotes: 1

ysearka
ysearka

Reputation: 3855

To create a nested list as output try this:

result = []
for i in range(len(text)):
    temp = []
    for t in text[i]:
        word_modified = t
        #making some operations with each element in the nested list.
        #using if-statements here
        temp.append(word_modified)
    result.append(temp)
result

If you just copy paste this code, result will be the equal to text. But as in the loop t represents each word separatly, you should be able to modifiy it as you wish.

Upvotes: 4

Related Questions