2

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.

3
  • It's not 100% clear what you're asking - do you want to keep the original list-of-lists and also return a new, modified, copy? Commented May 12, 2016 at 12:58
  • It should be difficult to modify your inner lists in place - can you include enough code to actually replicate your problem? Commented May 12, 2016 at 13:00
  • Sorry, I want to get modified list-of-list. Commented May 12, 2016 at 13:01

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1
[[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 ]

Comments

1

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.

2 Comments

A bit of textual explanation would go a long way with other users looking for an answer.
@LaurIvan Thank you. I've added a few more lines of description
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.