str.join(iterable)
返回一个字符串,它是可迭代迭代中字符串的连接。 如果iterable中有非字符串值,包括字节对象,则会引发TypeError。 元素之间的分隔符是提供此方法的字符串。
实例
Temp = " ".join(ipt[i:i+n])
operator.itemgetter(item)
operator.itemgetter(*items)
使用操作数的__getitem __()
方法返回从其操作数中提取项目的可调用对象。 如果指定了多个项目,则返回一个查找值的元组。
这些项可以是操作数的__getitem __()
方法接受的任何类型。 字典接受任何可变数值。 列表,元组和字符串接受索引或切片:
例如:
After f = itemgetter(2), the call f(r) returns r[2].
After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3]).
等价于
def itemgetter(*items):
if len(items) == 1:
item = items[0]
def g(obj):
return obj[item]
else:
def g(obj):
return tuple(obj[item] for item in items)
return g
>>>
>>> itemgetter(1)('ABCDEFG')
'B'
>>> itemgetter(1,3,5)('ABCDEFG')
('B', 'D', 'F')
>>> itemgetter(slice(2,None))('ABCDEFG')
'CDEFG'
>>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
>>> getcount = itemgetter(1)
>>> list(map(getcount, inventory))#tuple组里每个的第二个(从0开始)
[3, 2, 5, 1]
>>> sorted(inventory, key=getcount)#按第二个值的大小顺序对整个list排序
[('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]