Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing quotes from 2D array python

I am currently trying to execute code that evaluetes powers with big exponents without calculating them, but instead logs of them. I have a file containing 1000 lines. Each line contains two itegers separated by a comma. I got stuck at point where i tried to remove quotes from array. I tried many way of which none worked. Here is my code:

function from myLib called split() takes two argumanets of which one is a list and second is to how many elemts to split the original list. Then does so and appends smaller lists to the new one.

import math
import myLib

i = 0
record = 0
cmpr = 0
with open("base_exp.txt", "r") as f:
    fArr  = f.readlines()
    fArr  = myLib.split(fArr, 1)
    #place get rid of quotes
    print(fArr)
    while i < len(fArr):
        cmpr = int(fArr[i][1]) * math.log(int(fArr[i][0]))
        if cmpr  > record:
            record = cmpr
            print(record)
        i = i + 1

This is how my Array looks like:

[['519432,525806\n'], ['632382,518061\n'], ... ['172115,573985\n'], ['13846,725685\n']]

I tried to find a way around the 2d array and tried:

i = 0
record = 0
cmpr = 0
with open("base_exp.txt", "r") as f:
    fArr  = f.readlines()
    #fArr  = myLib.split(fArr, 1)
    fArr = [x.replace("'", '') for x in fArr]
    print(fArr)
    while i < len(fArr):
        cmpr = int(fArr[i][1]) * math.log(int(fArr[i][0]))
        if cmpr  > record:
            record = cmpr
            print(i)
        i = i + 1

But output looked like this:

['519432,525806\n', '632382,518061\n', '78864,613712\n', ...

And the numbers in their current state cannot be considered as integers or floats so this isnt working as well...:

[int(i) for i in lst]

Expected output for the array itself would look like this, so i can pick one of the numbers and work with it:

[[519432,525806], [632382,518061], [78864,613712]...

I would really apreciate your help since im still very new to python and programming in general.

Thank you for your time.

like image 317
Matej Novosad Avatar asked Mar 28 '26 07:03

Matej Novosad


2 Answers

You can avoid all of your problems by simply using numpy's convenient loadtxt function:

import numpy as np
arr = np.loadtxt('p099_base_exp.txt', delimiter=',')
arr

array([[519432., 525806.],
       [632382., 518061.],
       [ 78864., 613712.],
       ...,
       [325361., 545187.],
       [172115., 573985.],
       [ 13846., 725685.]])

If you need a one-dimensional array:

arr.flatten()
# array([519432., 525806., 632382., ..., 573985.,  13846., 725685.])
like image 197
user3483203 Avatar answered Mar 29 '26 22:03

user3483203


This is your missing piece:

fArr = [[int(num) for num in line.rstrip("\n").split(",")] for line in fArr] 

Here, rstrip("\n") will remove trailing \n character from the line and then the string will be split on , so that each string will be become a list and all integers in that line will become elements of that list but as a string. Then, we can call int() function on each list element to convert them into int data type.

Below code should do the job if you don't want to import an additional library.

i = 0
record = 0
cmpr = 0
with open("base_exp.txt", "r") as f:
    fArr = f.readlines()
    fArr = [[int(num) for num in line.rstrip("\n").split(",")] for line in fArr] 
    print(fArr)
    while i < len(fArr): 
        cmpr = fArr[i][1] * math.log(fArr[i][0])
        if cmpr > record:
            record = cmpr
            print(i) 
        i = i + 1
like image 29
Kumar Avatar answered Mar 29 '26 21:03

Kumar