How to use the . replace() function in a list of lists or in a list of tuples?

Asked

Viewed 601 times

0

I am new in programming and new here on the site. I have a problem that I can not solve and luckily you exist. I’ve tried everything in my power and I’ve almost found the solution, only it was just about.

I have the following codes that I wrote with the help of answers here from the site:

A=[[3, 6, 3, 8, 9, 5, 5], [8, 6, 6, 4, 7, 10, 5, 3, 3, 3],[8, 6, 4, 5, 7, 3, 3, 2], [5, 4, 4, 10,3, 3, 0], [9, 6, 3, 6, 8, 8, 1]]

def chr_remove(antigo, remover):
    nova_string = antigo
    for x in remover:
        nova_string = nova_string.replace(x, '')
    return nova_string

remove1 = chr_remove(str(A), ",")

remove2 =chr_remove(remove1," ")

remove3 =remove2.replace("[","")

remove4 = remove3.replace("]",",")

remove_final = remove4.replace(",,","")

print(remove_final)

Code 2:

A=[(3, 6, 3, 8, 9, 5, 5), (8, 6, 6, 4, 7, 10, 5, 3, 3, 3), (8, 6, 4, 5, 7, 3, 3, 2), ( 5, 4, 4, 10,3, 3, 0), (9, 6, 3, 6, 8, 8, 1)]

def chr_remove(antigo, remover):
    nova_string = antigo
    for x in remover:
        nova_string = nova_string.replace(x, '')
    return nova_string

remove1 = chr_remove(str(A), ",")

remove2 =chr_remove(remove1," ")

remove3 =remove2.replace("(","")

remove4 = remove3.replace(")",",")

remove5 = remove4.replace("[","")

remove6 = remove5. replace(",]","")

remove_final= remove6.replace(",,","")

print(remove_final)

So far so good, the unwanted characters have been removed but the first problem appears when I read the items of the variable A and read the variable remove_final:

print(len(A))
>>>5

print(len(remove_final))
>>>46

And the second problem appears when I loop for the remove_final variable:

I’d like the result to come out like this:

73638955                                 
86647105333                              
86457332                                 
954410330                                
9636881

But the result comes out like this:

3                                        
6                                        
3                                        
8                                        
9                                        
5                                        
5                                       
,                                        
8                                        
6                                        
6                                        
4                                        
7                                        
1                                        
0
5
3
3
3
,
8
6
4
5
7
3
3
2
,
5
4
4
1
0
3
3
0
,
9
6
3
6
8
8
1

I tried to convert the string to list or tuple but nothing changes and if I ask to print as list or tuple the result also does not come as expected:

print(list(remove_final))
>>>['3', '6', '3', '8', '9', '5', '5', ',', '8', '6', '6', '4', '7', '1', '0', '5', '3', '3', '3', ',', '8', '6', '4', '5', '7', '3', '3', '2', ',', '5', '4', '4', '1', '0', '3', '3', '0', ',', '9', '6', '3', '6', '8', '8', '1']                     [Program finished]

print(tuple(remove_final))
>>>('3', '6', '3', '8', '9', '5', '5', ',', '8', '6', '6', '4', '7', '1', '0', '5', '3', '3', '3', ',', '8', '6', '4', '5', '7', '3', '3', '2', ',', '5', '4', '4', '1', '0', '3', '3', '0', ',', '9', '6', '3', '6', '8', '8', '1')                  [Program finished]

If I copy and paste the result in another editor I will be able to use normally as a tuple and be able to count the exact items and make a loop with the function .count() usually, only I have a few million data to analyze and there will be no printout, copy and paste in another editor.

Is there any module that has a function .replace() to list lists and list of tuples? Could someone help me solve this problem?

  • 1

    Thank you for editing my question @alandplm, I had a busy day and didn’t even have time to thank you.

1 answer

1


If I understand the goal, you want, from list, to iterate over all its elements, without comma. For this, just correct the line:

remove_final= remove6.replace(",,","")

for

remove_final= remove6.split(",")

This will return you a tuple with the values of concatenated sublists.


However, converting an iterable to a string, removing separators and delimiters from the string, and then working with the modified string is not the most appropriate way to iterate over lists and tuples, because it is difficult to understand by third parties and is inefficient, especially having a large data set.

The proper way to iterate over list lists depends on the structure that this data is

  • Whether the algorithm will only iterate over list lists:
for sublista in A:
   texto = "".join([str(elemento) for elemento in sublista])
   print(texto)
  • If it is not known how many levels of lists/tuples will be found, as in
A1 = [1, 5]
A2 = [[1,2],[5,6]]
A3 = [[[1,3],[1,2]],[[1,2],[5,6]]]

can do as follows

A1 = [1, 5]
A2 = [[1,2],[5,6]]
A3 = [[[1,3],[1,2]],[[1,2],[5,6]]]

def pegaValoresRecursivamente(entrada):
    #Testa se a entrada é lista ou tupla
    if type(entrada) in (list, tuple):
        for elemento in entrada:
            for elementoInterno in pegaValoresRecursivamente(elemento):
                yield elementoInterno
    else:
        yield entrada

print("A1")
for elem in pegaValoresRecursivamente(A1):
    print(elem)

print("\nA2")
for elem in pegaValoresRecursivamente(A2):
    print(elem)

print("\nA3")
for elem in pegaValoresRecursivamente(A3):
    print(elem)
A1
1
5

A2
1
2
5
6

A3
1
3
1
2
1
2
5
6

with the difference that in this case it will no longer print "line by line".

  • thank you so much for trying to help me! Sorry for the delay in responding. I just tested your answer and unfortunately that’s still not what I need to solve my problem. When I use the function . split() and then use the function Len() the number of positions do not match. I converted the list into string pq was the only way I could find to remove the unwanted characters. What I want as the end result is a tuple where I can use in addition to the . Count() function, the set() function as well.

  • My language is poor and I didn’t squeeze myself properly, I’m sorry for wasting your time. You think I should edit the question or ask a new one?

  • This list of lists is the result of a "slice" made in the result of an intersection. If I put a summary of my code would it be easier for you to understand? What I want as a final result is a tuple with the same number of positions in the sequence in which they were only without the comma, parentheses and brackets so I can use the function set() and know the exact number of positions that are repeated and then make a loop for and use the function . Count() and a conditional so that if the result is greater than such a condition, print result and thus save resources.

  • I will wait your answer to know if I ask another question or edit this. Thank you very much!

  • No problem! I think the recommended in these cases is to edit the question. Code summaries as well as description of the general purpose of the program are welcome. If possible also add the initial format/type of the input data that will be processed

  • Thanks, I’ll do it.

  • give a look at this other question I asked and if you can help me pfv https://answall.com/questions/392513/como-usar-a-fun%C3%A7%C3%a3o-set-in-a-list-or-in-a-list-of-tuples? noredirect=1#comment772911_392513

Show 2 more comments

Browser other questions tagged

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