博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pyextend库-merge可迭代对象合并函数
阅读量:6909 次
发布时间:2019-06-27

本文共 2143 字,大约阅读时间需要 7 分钟。

merge (iterable1, *args)

参数: 

iterable1: 实现 __iter__的可迭代对象, 如 str, tuple, dict, list

*args: 其他实现 __iter__的可迭代对象

返回值:

合并后的迭代对象

使用范例:

Example 1:        source = ['a', 'b', 'c']        result = merge(source, [1, 2, 3])        self.assertEqual(result, ['a', 'b', 'c', 1, 2, 3])        result = merge(source, [1, 2, 3], ['x', 'y', 'z'])        self.assertEqual(result, ['a', 'b', 'c', 1, 2, 3, 'x', 'y', 'z'])    Example 2:        source = 'abc'        result = merge(source, '123')        self.assertEqual(result, 'abc123')        result = merge(source, '123', 'xyz')        self.assertEqual(result, 'abc123xyz')    Example 3:        source = ('a', 'b', 'c')        result = merge(source, (1, 2, 3))        self.assertEqual(result, ('a', 'b', 'c', 1, 2, 3))        result = merge(source, (1, 2, 3), ('x', 'y', 'z'))        self.assertEqual(result, ('a', 'b', 'c', 1, 2, 3, 'x', 'y', 'z'))    Example 4:        source = {
'a': 1, 'b': 2, 'c': 3} result = merge(source, {
'x': 'm', 'y': 'n'}, {
'z': '1'}) self.assertEqual(result, {
'a': 1, 'b': 2, 'c': 3, 'x': 'm', 'y': 'n', 'z': '1'})

代码:

@(iterable1='__iter__')def merge(iterable1, *args):    """    Returns an type of iterable1 value, which merged after iterable1 used *args    :exception TypeError: if any parameter type of args not equals type(iterable1)    """    result_list = list(iterable1) if not isinstance(iterable1, dict) else eval('list(iterable1.items())')    for i, other in enumerate(args, start=1):        if not isinstance(other, type(iterable1)):            raise TypeError('the parameter type of index {} not equals type of index 0'.format(i))        if not isinstance(other, dict):            result_list[len(result_list):len(result_list)] = list(other)        else:            result_list[len(result_list):len(result_list)] = list(other.items())    if isinstance(iterable1, str):        return ''.join(result_list)    elif isinstance(iterable1, tuple):        return tuple(result_list)    elif isinstance(iterable1, dict):        return dict(result_list)    else:        return result_list

 

转载于:https://www.cnblogs.com/Vito2008/p/pyextned-merge.html

你可能感兴趣的文章
源码阅读:Masonry(六)—— MASViewConstraint
查看>>
Red Hat Enterprise Linux(RHEL)中yum的repo文件详解
查看>>
AI考拉技术分享会--Node.js并发模型
查看>>
NEO改进协议提案6(NEP-6)
查看>>
优化体系结构 - 混合运算实现 T+0查询
查看>>
java bean 对象属性复制框架BeanMapping-01-入门案例
查看>>
脑洞大开的翻转代码
查看>>
优化体系结构 - 数据外置减少中间表
查看>>
用ABAP代码读取S/4HANA生产订单工序明细
查看>>
海报推广神器:活码加多级加密跳转防封双重保护
查看>>
rabbitmq的基本使用
查看>>
深入 Nginx 之架构篇
查看>>
93. Restore IP Addresses
查看>>
环境变量python从版本2.x更改为3.x时,yum报错
查看>>
ant Table rowSelection勾选后更新数据无法清除缓存(无法取消勾选)
查看>>
【Linux系统编程】普通用户绑定(bind)特权端口
查看>>
代码编辑器Sublime_Text3的使用
查看>>
Docker Stack 部署web集群
查看>>
thinkphp源码分析(一)—开门篇
查看>>
猫叔产品读记 | 如何更好的玩转补贴、阿里入股B站商业化变现、儿童口腔市场怎么样?(3期)...
查看>>