Edit a list that is inside another list

Asked

Viewed 46 times

-1

I need to split the list inside another list.

lista = [
    [Temperatura final = 250 k],
    [Temperatura Minima = 120 k],
    [Temperatura Maxima = 310 k]
]

Get the final result

listafinal = [
    [Temperatura final, 250, k],
    [Temperatura Minima, 120, k],
    [Temperatura Maxima, 310, k]
]
  • 2

    Good morning buddy, which programming language you are using in your code?

  • apparently python, to edit a value in the list is simple, use the following notation: lista[i][j] = novoValor

  • Anything, research the use of matrices in the language you are stirring

  • Yes is Python the language.

  • Your question is not clear enough. The code snippets you presented do not have Python-valid syntax, so it’s pretty hard to gauge what you’re trying to do. I could draw up a [mcve]?

  • Guy like Anderson said above, this syntax doesn’t exist in python, at most it would be lista[ ['Temperatura', 250, k]['Temperatura Maxima', 310, k]] and you can manipulate the values of this method lista[0] # traz toda a lista que esteje na posição 0 lista[0][1] # Traz o seguno valor da lista que esta na posição 0.

Show 1 more comment

1 answer

2

Good afternoon,

Your question was not clear enough, but by the tags and the type of data in the list I will assume python algorithm with a string list.

There are several ways you can turn your string into a list:

  • Using the split: this method of the string type itself takes a character and divides its string into the positions that the character is. In your case it would not be what you seek because the character that could be used would be the ' '(space) and would have the following output.

    'Temperatura final = 250 k'.split(' ') ['Temperatura', 'final', '=', '250', 'k']

  • Substring: A substring could be used, in which you would specify the positions of each group, but you should do the division for each group and if the string changed and its size changed, you would not get the correct information.

    'Temperatura final = 250 k'[:17] 'Temperatura final'

  • Regex: this would be the most convenient option in which you can assemble the desired group structure, defining the patterns of each group and extracting them at the same time.

    s = r'Temperatura final = 250 k' re.findall(r'(.*)\s+=\s+(\d+)\s+(\w+)',s) [('Temperatura final', '250', 'k')]

The only however is when the search for more than one simultaneous group is executed findall returns a tuple with the groups found within a list. So you should take the first position of the reply list, which will be a tuple and convert to a list.

What the end result would look like:

import re
listafinal = [
    ['Temperatura final, 250, k'],
    ['Temperatura Minima, 120, k'],
    ['Temperatura Maxima, 310, k']
]

padrao = r'(.*)\s+=\s+(\d+)\s+(\w+)'
resultado = list()

for texto in listafinal:
   resultado.append(list(re.findall(padrao, texto)[0]))
  • I created something like this to solve! Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.