Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.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 - Fatal编程技术网

如何在python中修改列表的内容

如何在python中修改列表的内容,python,Python,我有一份清单,其中包含以下项目: my_data_list = ['1.7.45.67.3', '\'$10$This is the first team in the league\', "$12$b\'This team is at location 132.45.67 on the grid\'"'] 我希望能够将第一个索引之后的列表内容转换为如下内容。列表在第一个索引之后可以有几个项目,每个项目只需要多几个{} my_final_result = ['1.7.45.67.3', '{{

我有一份清单,其中包含以下项目:

my_data_list = ['1.7.45.67.3', '\'$10$This is the first team in the league\', "$12$b\'This team is at location 132.45.67 on the grid\'"']
我希望能够将第一个索引之后的列表内容转换为如下内容。列表在第一个索引之后可以有几个项目,每个项目只需要多几个
{}

my_final_result = ['1.7.45.67.3', '{{10, This is the first team in the league}, {12, This team is at location 132.45.67 on the grid}}']

我对如何有效地做这件事感到困惑。我对python非常陌生,希望您能帮助我有效地解决这个问题。

我的解决方案

def convert_data_list(data_list):
    data_list = [s.translate({ord(c): None for c in '\'\"'}) for s in data_list]
    result = []
    for data in data_list:
        pairs = []
        for int_text_string in data.split(','):
            integer, text = [d for d in int_text_string.split('$') if d.strip() != '']
            text = text.lstrip('b')
            pairs.append('{' + integer + ', ' + text + '}')
        result.append('{' + ', '.join(pairs) + '}')
    return result
样本使用

my_data_list = [
    '1.7.45.67.3', 
    '\'$10$This is the first team in the league\', "$12$b\'This team is at location 132.45.67 on the grid\'"',
    '\'$114$Sample data\', "$78$b\'Hello world\'", "$48$b\'A third pair\'"',
    ]
my_final_result = convert_data_list(my_data_list[1:])    # do not include zeroth index
my_final_result = [my_data_list[0]] + my_final_result    # add zeroth index back
print(result)
输出

['1.7.45.67.3', '{{10, This is the first team in the league}, {12, This team is at location 132.45.67 on the grid}}', '{{114, Sample data}, {78, Hello world}, {48, A third pair}}']

解释

def convert_data_list(data_list):
    data_list = [s.translate({ord(c): None for c in '\'\"'}) for s in data_list]
    result = []
    for data in data_list:
        pairs = []
        for int_text_string in data.split(','):
            integer, text = [d for d in int_text_string.split('$') if d.strip() != '']
            text = text.lstrip('b')
            pairs.append('{' + integer + ', ' + text + '}')
        result.append('{' + ', '.join(pairs) + '}')
    return result
此函数接受字符串列表。仅传入第一个索引后的字符串

def convert_data_list(data_list):
1。删除所有单引号和双引号

data_list = [s.translate({ord(c): None for c in '\'\"'}) for s in data_list]
integer, text = [d for d in int_text_string.split('$') if d.strip() != '']
所以这个示例字符串

'\'$10$This is the first team in the league\', "$12$b\'This team is at location 132.45.67 on the grid\'"'
变成

'$10$This is the first team in the league, $12$bThis team is at location 132.45.67 on the grid'
在python中还有其他方法可以做到这一点

2。循环遍历每个字符串

result = []
for data in data_list:
临时结果存储在
result

3。用逗号分割每个字符串,然后循环遍历每个字符串

pairs = []
for int_text_string in data.split(','):
从步骤1开始

'$10$This is the first team in the league, $12$bThis team is at location 132.45.67 on the grid'
成为字符串列表

['$10$This is the first team in the league', ' $12$bThis team is at location 132.45.67 on the grid']
4。提取整数和文本值

data_list = [s.translate({ord(c): None for c in '\'\"'}) for s in data_list]
integer, text = [d for d in int_text_string.split('$') if d.strip() != '']
给定
“$12$b该团队位于网格上的132.45.67位置”
,按
“$”拆分得出:

[' ', '12', 'bThis team is at location 132.45.67 on the grid']
由于可能存在只包含空格的字符串,因此不要将其包含在给出最终结果的列表中:

['12', 'bThis team is at location 132.45.67 on the grid']
所以0-index是整数,1-index是文本。然后通过列表解包将其分配给变量
integer
text

5。从左侧删除第一个字符“b”

text = text.lstrip('b')
        pairs.append('{' + integer + ', ' + text + '}')
    result.append('{' + ', '.join(pairs) + '}')
return result
如果字符串不是以a
'b'开头,则此操作无效

6。格式化结果

text = text.lstrip('b')
        pairs.append('{' + integer + ', ' + text + '}')
    result.append('{' + ', '.join(pairs) + '}')
return result


我不能说这有多高效,但它确实有效。

您是如何获得这些带有符号的字符串的?这是我从udp包收到的字符串。我只对美元符号中的整型字段和与之相关的字符串感兴趣。你能给出一些关于这些字符代表什么的上下文吗?获取整数是可行的,因为它们是由美元符号分隔的。然而,我不知道为什么其余的文本部分是不同的。
10
旁边的一个字符串只是一个简单的字符串,但另一个字符串用引号括起来,外部有一个
b
。这是因为b'是编码字符串,有些字符串没有编码。我对那些特殊的人物不感兴趣。我可以忽略诸如b'和/或\之类的字符,而专注于实际字符。希望能说清楚。对我来说,只有美元是单独的利息。请务必随时通知我。非常感谢你的帮助。我很感激,我会调查一下,然后告诉你。非常感谢,这对我来说意义重大。我会随时通知你。我目前正在研究解决方案。我可能有一些问题。谢谢我不确定“结果”在
my\u final\u result=[my\u data\u list[0]]+result
中是什么。你是说
my_final_result=[my_data_list[0]]+my_final_result
?@Anna我编辑了
result
类型。很抱歉。关于查看单个字符,您可能传递的是原始字符串,而不是字符串列表。也许您使用了
my\u data\u list[1]
而不是
my\u data\u list[1://code>。