如何为字符串赋值?

时间:2017-01-20 14:26:31

标签: python

我收到一个包含元组的列表

例如:

a=[('bp', 46), ('sugar', 98), ('fruc', 56), ('mom',65)]

和树状结构中的嵌套列表

    tree= [
    [
        'a',
        'bp',
        [78, 25, 453, 85, 96]
    ],
    [
        ['hi', ['no', ['ho', 'sugar', 3]], ['not', 'he', 20]],
        [['$', 'fruc', 7185], 'might', 'old'],
        'bye'
    ],
    [
        ['not', ['<', 'mom', 385]],
        [
            ['in', 'Age', 78.5],
            [['not', ['and', 'bp', 206]], 'life', [['or', ['not', ['\\', 'bp', 5]], ['p', 'sugar', 10]], 'ordag',[['perhaps', ['deal', 'mom', 79]],
            'helloo',[['or', ['pl', 'mom', 25]], 'come', 'go']]]],
            'noway'
        ],
        [['<', 'bp', 45], 'falseans', 'bye']
    ]
]

如何指定元组的第一个元素,它是一个字符串,旁边的值。因为例如当涉及到树中的“妈妈”时,我想要使用它的价值。我想创建一个字典,但它只是将我的列表放在一个不同的形式,不帮助我将它分配给树中的值。我没有为字符串赋值,也可以使用其值替换字符串,但替换函数只能在字符串中使用,并用不同的字符串替换字符串。

提前致谢

2 个答案:

答案 0 :(得分:1)

快速入侵,在简单的情况下工作。

(注意:此处的字符串不正确:'\'应为'\\'

  • 将结构转换为字符串
  • 使用单引号作为分隔符执行替换,因此可以安全地防止其他更大词中的单词包含
  • 使用ast.literal_eval解析带有替换的字符串,这可以解决繁重的问题(将有效的文字结构文本解析回有效的python结构)

代码:

tree= [['a', 'bp', [78, 25, 453, 85, 96]],
[['hi', ['no', ['ho', 'sugar', 3]], ['not', 'he', 20]],
[['$', 'fruc', 7185], 'might', 'old'],
'bye'],[['not', ['<', 'mom', 385]],
[['in', 'Age', 78.5],[['not', ['and', 'bp', 206]],
'life',[['or', ['not', ['\\', 'bp', 5]], ['p', 'sugar', 10]],
'ordag',[['perhaps', ['deal', 'mom', 79]],
'helloo',[['or', ['pl', 'mom', 25]], 'come', 'go']]]],
'noway'],[['<', 'bp', 45], 'falseans', 'bye']]]
a=[('bp', 46), ('sugar', 98), ('fruc', 56), ('mom',65)]

str_tree = str(tree)

for before,after in a:
    str_tree = str_tree.replace("'{}'".format(before),str(after))

new_tree = ast.literal_eval(str_tree)
print(type(new_tree),new_tree)

结果:

<class 'list'> [['a', 46, [78, 25, 453, 85, 96]], [['hi', ['no', ['ho', 98, 3]], ['not', 'he', 20]], [['$', 56, 7185], 'might', 'old'], 'bye'], [['not', ['<', 65, 385]], [['in', 'Age', 78.5], [['not', ['and', 46, 206]], 'life', [['or', ['not', ['\\', 46, 5]], ['p', 98, 10]], 'ordag', [['perhaps', ['deal', 65, 79]], 'helloo', [['or', ['pl', 65, 25]], 'come', 'go']]]], 'noway'], [['<', 46, 45], 'falseans', 'bye']]]

所以这是一个黑客,但它能够处理包含集合,列表,字典,元组的数据,而不会有太多麻烦。

答案 1 :(得分:1)

我建议树的递归转换:

a=[('bp', 46), ('sugar', 98), ('fruc', 56), ('mom',65)]
d = dict(a)
tree= [
    [
        'a',
        'bp',
        [78, 25, 453, 85, 96]
    ],
    [
        ['hi', ['no', ['ho', 'sugar', 3]], ['not', 'he', 20]],
        [['$', 'fruc', 7185], 'might', 'old'],
        'bye'
    ],
    [
        ['not', ['<', 'mom', 385]],
        [
            ['in', 'Age', 78.5],
            [['not', ['and', 'bp', 206]], 'life', [['or', ['not', ['\\', 'bp', 5]], ['p', 'sugar', 10]], 'ordag',[['perhaps', ['deal', 'mom', 79]],
            'helloo',[['or', ['pl', 'mom', 25]], 'come', 'go']]]],
            'noway'
        ],
        [['<', 'bp', 45], 'falseans', 'bye']
    ]
]



def replace(node):
    if isinstance(node, str):
        return d.get(node, node)
    elif isinstance(node, list):
        return [replace(el) for el in node]
    else:
        return node

replace(tree)
相关问题