Place items in lists

3

I would like to have two lists: one with the name of the fruit and another with the colors.

lista = ['banana [amarela]\n', 'uva [vinho]\n', 'laranja [laranjado]\n', 'kiwi [verde]\n', 'framboesa [vermelho]\n', 'coco [marrom]\n']

lines = lista.split(" ")

fruits = []
colors = []

for line in lines:
    fruits.append(line[0])
    #colors.append(line[1]) #IndexError: string index out of range

print(fruits)
#print(colors) #IndexError: string index out of range 

Script output:

  

['' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' a ', a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' 'a', 'a', 'a', 'a', 'n', 'j', 'a', '', '' '' lololol, lolololol, lolololol, lololololol, lololololol, lololololol, lololololol, lololololol, lololololol, lololololol, lololololol, '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ' 'e', '' '', '', '', '', '', '', '', '', '', '', '', '', '', '' ' ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee h ',' o ',' '' '' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ' ]

Desired output:

  

fruits = ['banana', 'grape', 'orange', 'kiwi', 'raspberry', 'coconut']

     

colors = ['yellow', 'wine', 'orange', 'green', 'red', 'brown']

    
asked by anonymous 13.09.2017 / 15:32

4 answers

2

Breaking text content in white space does not seem to be a good solution, since there may be names of fruits and colors that have spaces, such as fruta do conde [rosa choque] ( throw away, it's rotten! / em>), or something like that. If the pattern is always fruta [cor] , I believe it's more viable to use a regular expression:

(.+) \[(.+)\]\n?
  • (.+) creates a catch group for any sequence of characters;
  • Blank space, indicating that the previous group must necessarily be followed by a space;
  • \[(.+)\] creates another catch group for any string within brackets;
  • \n? defines that the line may or may not end in a \n character;
  • In this way, the fruit name will match any sequence of characters, provided that it is followed by a blank space and any other sequence of characters enclosed in brackets, and may or may not be terminated with the \n character. The first group will be the name of the fruit and the second group will be its color. In Python it would look like:

    import re
    
    lista = ['banana [amarela]\n', 'uva [vinho]\n', 'laranja [laranjado]\n', 'kiwi [verde]\n', 'framboesa [vermelho]\n', 'coco [marrom]\n']
    
    pattern = re.compile(r"(.+) \[(.+)\]\n?")
    
    frutas = []
    cores = []
    
    for item in lista:
        if pattern.match(item):
            fruta, cor = pattern.search(item).groups()
            frutas.append(fruta)
            cores.append(cor)
    
    print(frutas)
    print(cores)
    

    Producing the result:

    ['banana', 'uva', 'laranja', 'kiwi', 'framboesa', 'coco']
    ['amarela', 'vinho', 'laranjado', 'verde', 'vermelho', 'marrom']
    
      

    See working at Ideone .

    The re.compile function will generate a Python structure to handle the above regular expression. The item list is then traversed and, with the pattern.match method, checks to see if the content of the item conforms to the expected pattern, and if it does, it extracts the values of the catch groups using the pattern.search method. The groups method will return a tuple with the values of the catch groups, in this case a tuple of two values: the name of the fruit and its color. Thus, the unpacking technique of tuples values is used in two variables:

    fruta, cor = ("banana", "amarela")
    

    Making fruta receive banana and cor receive amarela , storing values in their lists.

    If these values are stored in a text file, you can do this:

    import re
    
    pattern = re.compile(r"(.+) \[(.+)\]\n?")
    
    frutas = []
    cores = []
    
    with open("lista.txt") as file:
        for line in file:
            if pattern.match(line):
                fruta, cor = pattern.search(line).groups()
                frutas.append(fruta)
                cores.append(cor)
    
    print(frutas)
    print(cores)
    
      

    See working at Repl.it .

        
    13.09.2017 / 16:18
    1

    If it is as constant as shown in your example you can do:

    lista = ['banana [amarela]\n', 'uva [vinho]\n', 'laranja [laranjado]\n', 'kiwi [verde]\n', 'framboesa [vermelho]\n', 'coco [marrom]\n']
    colors = []
    fruits = []
    for ele in lista:
        fruit, color = ele.strip().split(' ')
        colors.append(color[1:-1]) # retirar parentesis retos da cor
        fruits.append(fruit)
    
    print(fruits) # ['banana', 'uva', 'laranja', 'kiwi', 'framboesa', 'coco']
    print(colors) # ['amarela', 'vinho', 'laranjado', 'verde', 'vermelho', 'marrom']
    

    DEMONSTRATION

        
    13.09.2017 / 16:14
    1

    It can be done as follows:

    lista = ['banana [amarela]\n', 'uva [vinho]\n', 'laranja [laranjado]\n', 'kiwi [verde]\n', 'framboesa [vermelho]\n', 'coco [marrom]\n']
    
    fruits, color = []
    
    for element in lista:
        fc = element.split(" ")
        fruits += [fc[0]]
        color += [fc[1][1:-3]]
    
    print "lista de frutas =" + str(fruits)
    print "lista de cores =" + str(color)
    
        
    13.09.2017 / 16:21
    1
    >>> lista = ['banana [amarela]\n', 'uva [vinho]\n', 'laranja [laranjado]\n', 'kiwi [verde]\n', 'framboesa [vermelho]\n', 'coco [marrom]\n']
    >>> l1, l2 = zip(*(l.strip().replace('[','').replace(']','').split() for l in lista))
    >>> l1
    ('banana', 'uva', 'laranja', 'kiwi', 'framboesa', 'coco')
    >>> l2
    ('amarela', 'vinho', 'laranjado', 'verde', 'vermelho', 'marrom')
    

    Or if you need a list

    >>> list(l1)
    ['banana', 'uva', 'laranja', 'kiwi', 'framboesa', 'coco']
    >>> list(l2)
    ['amarela', 'vinho', 'laranjado', 'verde', 'vermelho', 'marrom']
    
        
    13.09.2017 / 16:27