Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 替换并删除字符串中的项_Python_Python 3.x_String_Replace_Formatting - Fatal编程技术网

Python 替换并删除字符串中的项

Python 替换并删除字符串中的项,python,python-3.x,string,replace,formatting,Python,Python 3.x,String,Replace,Formatting,我想更换我的_字符串 my_string = '[[4.6, 4.3, 4.3, 85.05], [8.75, 5.6, 6.6, 441.0], [4.6, 4.3, 4.3, 85.9625]]' 这样它就会得到这样的结果 my_string = '4.6x4.3x4.3 8.75x5.6x6.6 4.6x4.3x4.3' last_item = '85.05 441.0 85.9625' 列表中的最后一项没有固定长度 这是我写的第一个目标 mystring = mystring.re

我想更换我的_字符串

my_string = '[[4.6, 4.3, 4.3, 85.05], [8.75, 5.6, 6.6, 441.0], [4.6, 4.3, 4.3, 85.9625]]'
这样它就会得到这样的结果

my_string = '4.6x4.3x4.3 8.75x5.6x6.6 4.6x4.3x4.3'

last_item = '85.05 441.0 85.9625'
列表中的最后一项没有固定长度 这是我写的第一个目标

mystring = mystring.replace("[","").replace("]","").replace(", ","x")
但是我需要一种更具Python风格的方式。

如果我的字符串=“[[4.6,4.3,4.3,85.05],[8.75,5.6,6.6441.0],[4.6,4.3,4.3,85.9625]”,请尝试以下方法:

更新:

在你的问题中,我的字符串=“[[4.6,4.3,4.3,85.05],[8.75,5.6,6.6441.0][4.6,4.3,4.3,85.9625]”。如果要将其用作my_字符串,请尝试以下操作:

my_string = " ".join(["x".join([str(k) for k in i[:-1]]) for i in [i.replace(",","").split() for i in my_string.replace("[","").split("]") if len(i) >1]])
last_item = " ".join([str(k[-1]) for k in [i.replace(",","").split() for i in my_string.replace("[","").split("]") if len(i) >1
在第二种情况下,我们首先执行我的_string.replace[,结果是'4.6,4.3,4.3,85.05],8.75,5.6,6.6,441.0]4.6,4.3,4.3,85.9625]'。现在我们用.split]将它们拆分,结果是['4.6,4.3,4.3,85.05','8.75,5.6,6.6441.0','4.6,4.3,4.3,85.9625',]。我们需要删除这里的项目,它们只是空字符串,所以我们将if leni>1部分合并到列表中。现在我们得到了['4.6,4.3,4.3,85.05','8.75,5.6,6.6441.0','4.6,4.3,4.3,85.9625']。我们需要删除列表中字符串项中的逗号。因此,对于此列表中的每一项,我们都要进行。替换、,然后再加上。split,现在结果是['4.6','4.3','4.3','85.05'],['8.75','5.6','6.6','441.0'],['4.6','4.3','85.9625']。现在,对于每个子列表,我们将项目带到最后一个,然后执行x.join并分配给my_字符串。使用该列表,我们可以添加所有最后的项。加入并将其分配给最后的项变量。

如果我的项字符串=“[[4.6,4.3,4.3,85.05],[8.75,5.6,6.6,441.0],[4.6,4.3,4.3,85.9625]”,请尝试此操作:

更新:

在你的问题中,我的字符串=“[[4.6,4.3,4.3,85.05],[8.75,5.6,6.6441.0][4.6,4.3,4.3,85.9625]”。如果要将其用作my_字符串,请尝试以下操作:

my_string = " ".join(["x".join([str(k) for k in i[:-1]]) for i in [i.replace(",","").split() for i in my_string.replace("[","").split("]") if len(i) >1]])
last_item = " ".join([str(k[-1]) for k in [i.replace(",","").split() for i in my_string.replace("[","").split("]") if len(i) >1
在第二种情况下,我们首先执行我的_string.replace[,结果是'4.6,4.3,4.3,85.05],8.75,5.6,6.6,441.0]4.6,4.3,4.3,85.9625]'。现在我们用.split]将它们拆分,结果是['4.6,4.3,4.3,85.05','8.75,5.6,6.6441.0','4.6,4.3,4.3,85.9625',]。我们需要删除这里的项目,它们只是空字符串,所以我们将if leni>1部分合并到列表中。现在我们得到了['4.6,4.3,4.3,85.05','8.75,5.6,6.6441.0','4.6,4.3,4.3,85.9625']。我们需要删除列表中字符串项中的逗号。因此,对于此列表中的每一项,我们都要进行。替换、,然后再加上。split,现在结果是['4.6','4.3','4.3','85.05'],['8.75','5.6','6.6','441.0'],['4.6','4.3','85.9625']。现在,对于每个子列表,我们将项目带到最后一个,然后执行x.join并分配给my_字符串。通过该列表,我们可以添加所有最后的项。加入并将其分配给最后的项变量。

尝试使用dict

尝试使用dict


您的字符串看起来像有效的json字符串。也许是这样:

import json
def to_str(lst):
    return 'x'.join([str(e) for e in lst])
my_string = '[[4.6, 4.3, 4.3, 85.05], [8.75, 5.6, 6.6, 441.0], [4.6, 4.3, 4.3, 85.9625]]'
temp = json.loads(my_string)
first_items = ' '.join([to_str(x[:-1]) for x in temp])
last_items = ' '.join([str(x[-1]) for x in temp])
print(first_items)
print(last_items)

您的字符串看起来像有效的json字符串。也许是这样:

import json
def to_str(lst):
    return 'x'.join([str(e) for e in lst])
my_string = '[[4.6, 4.3, 4.3, 85.05], [8.75, 5.6, 6.6, 441.0], [4.6, 4.3, 4.3, 85.9625]]'
temp = json.loads(my_string)
first_items = ' '.join([to_str(x[:-1]) for x in temp])
last_items = ' '.join([str(x[-1]) for x in temp])
print(first_items)
print(last_items)

我尝试运行您的代码,但它给出了以下错误:ValueError:格式错误的节点或字符串:使用信息更新,请检查!很抱歉但请你详细说明一下好吗?@RahulSharma现在检查一下!我尝试运行您的代码,但它给出了以下错误:ValueError:格式错误的节点或字符串:使用信息更新,请检查!很抱歉但请你详细说明一下好吗?@RahulSharma现在检查一下!