简体   繁体   English

如何从 python 中的数组中删除引号

[英]How to remove quotation marks from array in python

How to remove quotation marks from array.如何从数组中删除引号。 I am using np.array我正在使用 np.array

my code我的代码

 c1=np.array([w])
    print(c1)

I tried the replace but it didn't work我尝试了替换,但它没有用

a=str(c1).replace("'",'')

my code reads data from a text file and edits it.我的代码从文本文件中读取数据并对其进行编辑。 Subsequently, I will store them in an array and count the combinations from them my full code随后,我将它们存储在一个数组中,并计算它们的组合我的完整代码

import sys
from itertools import combinations 
import numpy as np
import pandas as pd
import time
import re


    f = open('nacdata.txt', 'r')
    lines = f.readlines()
    
    for i in lines:
        if i[0] != '<' and i[0] != '>' and i[0] != '=':
            p = str(' '.join(i.split()))
            #print(p)
            
        else:
            w = i[3:]
            w = ', '.join(w.split())
            #print(w)
            time.sleep(1)
            y = i[2]
            c1=np.array([w])
            print(c1)

output output

['1, 4, 7']
['2, 5, 8']
['3, 6, 9']
['1, 2, 3']
['4, 5, 6']
['7, 8, 9']
['10, 11, 12, 13, 14, 15, 16, 17, 18']
['-10, -11, -12, -13, -14, -15, -16, -17, -18']
['10, 11, 12, 13, 14, 15, 16, 17, 18']
['-10, -11, -12, -13, -14, -15, -16, -17, -18']
['19, 20, 21, 22, 23, 24, 25, 26, 27']
['-19, -20, -21, -22, -23, -24, -25, -26, -27']
['10, 19, 28']
['11, 20, 29']
['12, 21, 30']
['13, 22, 31']
['14, 23, 32']
['15, 24, 33']
['16, 25, 34']
['17, 26, 35']
['18, 27, 36']

You can try:你可以试试:

data = "['1, 4, 7']"

data = data.replace("'","")
print(data)
print(type(data))

list_data = data.replace("[","").replace("]","").split(", ")
print(list_data)

list_data = [int(d) for d in list_data]
print(list_data)

Output: Output:

[1, 4, 7]
<class 'str'>
['1', '4', '7']
[1, 4, 7]

I think you are trying to convert the data from c1 into a list of integers?我认为您正在尝试将c1中的数据转换为整数列表? For this, you can do:为此,您可以执行以下操作:

c1 = [int(i) for i in c1[0].replace(" ", "").split(",")]

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

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