简体   繁体   English

从2D数组python中删除引号

[英]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. 我有一个包含1000行的文件 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. 来自myLib的函数split()有两个参数,其中一个是列表,第二个是分割原始列表的元素数。 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: 这就是我的Array的样子:

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

I tried to find a way around the 2d array and tried: 我试图找到绕过2d阵列的方法并尝试:

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. 我真的很赞赏你的帮助,因为我对python和编程一般都很新。

Thank you for your time. 感谢您的时间。

You can avoid all of your problems by simply using numpy 's convenient loadtxt function: 只需使用numpy方便的loadtxt函数就可以避免所有问题:

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.])

This snippet will transform your array to 1D array of integers: 此代码段将您的数组转换为1D整数数组:

from itertools import chain

arr = [['519432,525806\n'], ['632382,518061\n']]

new_arr = [int(i.strip()) for i in chain.from_iterable(i[0].split(',') for i in arr)]
print(new_arr)

Prints: 打印:

[519432, 525806, 632382, 518061]

For 2D output you can use this: 对于2D输出,您可以使用:

arr = [['519432,525806\n'], ['632382,518061\n']]

new_arr = [[int(i) for i in v] for v in (i[0].split(',') for i in arr)]
print(new_arr)

This prints: 这打印:

[[519432, 525806], [632382, 518061]]

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. 这里, rstrip("\\n")将从行中删除尾随\\n字符,然后将字符串拆分,这样每个字符串将成为一个列表,该行中的所有整数将成为该列表的元素,但是一个字符串。 Then, we can call int() function on each list element to convert them into int data type. 然后,我们可以在每个列表元素上调用int()函数将它们转换为int数据类型。

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
new_list=[]
a=['519432,525806\n', '632382,518061\n', '78864,613712\n',]
for i in a:
    new_list.append(list(map(int,i.split(","))))

print(new_list)

Output: 输出:

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

In order to flatten the new_list 为了压扁new_list

from functools  import reduce
reduce(lambda x,y: x+y,new_list)
print(new_list)

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM