简体   繁体   English

Python在PHP中是否有类似引用的内容?

[英]Does the Python have something like reference in PHP?

I want to get some data from XML and return it in the same variable, so i do like this before return: 我想从XML中获取一些数据并以相同的变量返回它,所以在返回之前我喜欢这样做:

for element in response:
    document = parseString(element)

    try:
        element = {
            'scopes':  document.getElementsByTagNameNS('*', 'Scopes')[0].firstChild.nodeValue,
            'address': document.getElementsByTagNameNS('*', 'XAddrs')[0].firstChild.nodeValue
        }
    except:
        element = False

return response

But the response still contains raw xml data instead of parsed results... Basically I want that element = ... value returned to response value. 但是响应仍然包含原始xml数据而不是解析结果...基本上,我希望那个element = ... value返回到response值。

Edit: Improving the answer according delnan and jonrsharpe. 编辑:根据delnan和jonrsharpe改善答案。

The problem with your code is that, when you loop 代码的问题是,当您循环时

for element in response

in each iteration element will be pointing to a new created object couse the lines: 在每个迭代element都将指向以下行:

element = {
            'scopes':  document.getElementsByTagNameNS('*', 'Scopes')[0].firstChild.nodeValue,
            'address': document.getElementsByTagNameNS('*', 'XAddrs')[0].firstChild.nodeValue
        }

and the object it was pointing before still the same. 和它之前指向的对象仍然相同。 You can try this. 你可以试试看

for index,element in enumerate(response):
    document = parseString(element)

    try:
        response[index]= {
            'scopes':  document.getElementsByTagNameNS('*', 'Scopes')[0].firstChild.nodeValue,
            'address': document.getElementsByTagNameNS('*', 'XAddrs')[0].firstChild.nodeValue
        }
    except:
         response[index]= False

return response

Assuming that response is a list of strings and you want to replace those strings with the parsed elements dict..., that's easy. 假设响应是一个字符串列表,并且您想将这些字符串替换为已解析的元素dict ...,那很容易。 Since lists are mutable containers, you can replace the elements as you go. 由于列表是可变容器,因此您可以随时替换元素。 No need to return the list... the one you pass in is changed. 无需返回列表...您传递的列表已更改。

def convert_response_to_elements(response):
    for index, element_str in enumerate(response):
        document = parseString(element_str)
        try:
            element = {
                'scopes':  document.getElementsByTagNameNS('*', 'Scopes')[0].firstChild.nodeValue,
                'address': document.getElementsByTagNameNS('*', 'XAddrs')[0].firstChild.nodeValue
        }
        except:
            element = False
        response[index] = element

Since response is a list, I would use pythons list comprehension . 由于response是一个列表,因此我将使用pythons list comprehension This will create new list without modifying old one. 这将创建新列表,而无需修改旧列表。

new_response = [modify_element(element) for element in response]

Later if you want to remove elements that equal to False you can use filter function: 以后,如果要删除等于False元素,则可以使用过滤器功能:

without_false = filter(lambda element: bool(element), new_response)

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

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