python ^ and or >> <<

1.简单说一下python中^, and, or的区别

  • 按位异或:^
    (1)任何整数与0按位异或都等于自身
    (2)满足交换律,举个非常实用的例子:3^2^2=3^(2^2)=3^0=3, 这样就可以很简单地记录出只出现了一次的数啦
    (3)还可以判断bool变量 TrueFalse, 分别看成是1和0就行

    (4)相同的两个数的异或结果直接是0, 只有当两个数不同的时候才会转换为二进制进行异或运算

  • 条件判断and
    (1)主要注意10000 and 3的结果为33 and 10000的结果是10000,这说明了是有顺序的
    (2)更特殊的, 0 and 10000的结果为0,而不是10000,这是因为0是作为了False

  • 条件判断or
    (1)主要注意10000 or 3的结果为100003 or 10000的结果是3,这说明了是有顺序的

  • [2020.10.02补]:
    (1)<<位运算是将整个二进制向左移动几位,末尾补
    (2)>>位运算是将整个二进制向右移动几位

以下是使用字符串分析的方式解析给定的 XML,并将解析结果存储为多级数组的代码: ```python def parse_xml(xml_string): result = [] stack = [] curr_node = None for char in xml_string: if char == '<': if curr_node and curr_node['tag'] != '': stack.append(curr_node) curr_node = {'tag': '', 'attributes': {}, 'text': '', 'children': []} elif char == '>': if curr_node['tag'] != '': stack[-1]['children'].append(curr_node) curr_node = stack.pop() elif char == '/': if xml_string.startswith('/>', curr_index): if curr_node['tag'] != '': stack[-1]['children'].append(curr_node) curr_node = stack.pop() elif xml_string.startswith('</', curr_index): curr_index += 2 end_index = xml_string.find('>', curr_index) tag = xml_string[curr_index:end_index] if curr_node['tag'] == tag: curr_node = stack.pop() elif char == '=': key = curr_attribute curr_attribute = '' elif char == '"': if not curr_attribute: curr_attribute = xml_string[curr_index+1:] else: curr_node['attributes'][key] = xml_string[curr_attribute_start_index+1:curr_index] curr_attribute_start_index = None elif char == ' ': if not curr_attribute: continue if not curr_attribute_start_index: curr_attribute_start_index = curr_index elif char.isalpha(): if not curr_node['tag']: curr_node['tag'] += char else: curr_attribute += char elif char.isnumeric() or char in [':', '.', '-']: curr_node['text'] += char return curr_node xml_string = '''<xml> <node type="user" id="10034" realname="张三"><permission id="19001" status="1">access</permission></node> <node type="user" id="10035" realname="李四"><permission id="19002" status="1">denied</permission></node> <node type="user" id="10036" realname="王五"><permission id="19003" status="1">access</permission></node><node type="log" userid="10034" url="/index"></node> <node type="log" userid="10034" url="/index/1"> <node type="action" value="on"><node type="script" value="off"><node type="script" value="click">result:ok</node></node></node> </node> <node type="log" userid="10036" url="/index/2"><node type="script" value="off"><node type="script" value="click"></node></node></node> <node type="log" userid="10035" url="/index/3"><node type="script" value="off"><node type="script" value="click"></node></node></node> <node type="log" userid="10036" url="/index/4"><node type="script" value="off"><node type="script" value="click"></node></node></node> </xml>''' parsed_data = parse_xml(xml_string) print(parsed_data) ``` 运行以上代码,将打印出解析后的多级数组。请注意,代码中的解析函数只能解析给定的 XML 结构,如果 XML 结构发生变化,可能需要对代码进行适当的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值