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?
Thank you for editing my question @alandplm, I had a busy day and didn’t even have time to thank you.
– Juliana Maria