Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 包含单个元素的zip列表_Python_List_Python 3.x - Fatal编程技术网

Python 包含单个元素的zip列表

Python 包含单个元素的zip列表,python,list,python-3.x,Python,List,Python 3.x,我有一些元素的列表,例如[1,2,3,4]和单个对象,例如'a'。我想生成一个元组列表,其中列表元素位于第一个位置,单个对象位于第二个位置:[(1,'a'),(2,'a'),(3,'a'),(4,'a')] 我可以用zip这样做: def zip_with_scalar(l, o): # l - the list; o - the object return list(zip(l, [o] * len(l))) 然而,这给了我一种创建不必要的重复元素列表的感觉 另一种可能性是 def

我有一些元素的列表,例如
[1,2,3,4]
和单个对象,例如
'a'
。我想生成一个元组列表,其中列表元素位于第一个位置,单个对象位于第二个位置:
[(1,'a'),(2,'a'),(3,'a'),(4,'a')]

我可以用
zip
这样做:

def zip_with_scalar(l, o): # l - the list; o - the object
    return list(zip(l, [o] * len(l)))
然而,这给了我一种创建不必要的重复元素列表的感觉

另一种可能性是

def zip_with_scalar(l, o):
    return [(i, o) for i in l]
这确实是非常干净的,而且是python式的,但在这里,我是“手动”完成整个过程的。在哈斯凯尔,我会做类似的事情

zipWithScalar l o=zip l$repeat o

是否有任何内置函数或技巧,无论是用于标量压缩还是用于使我能够使用普通压缩的东西,即某种无限列表?

这是Haskell解决方案中最简单的一种:

import itertools

def zip_with_scalar(l, o):
    return zip(l, itertools.repeat(o))
您还可以使用生成器,以避免创建类似理解的列表:

def zip_with_scalar(l, o):
    return ((i, o) for i in l)
您还可以将zip_longest与fillvalue一起使用
o

from itertools import zip_longest

def zip_with_scalar(l, o): # l - the list; o - the object
    return zip_longest(l, [o], fillvalue=o)

print(list(zip_with_scalar([1, 2, 3, 4] ,"a")))

请注意,无论是使用zip_longest还是repeat,都不会复制用于
o
的任何可变值。

您可以使用内置的
map
功能:

>>> elements = [1, 2, 3, 4]
>>> key = 'a'
>>> map(lambda e: (e, key), elements)
[(1, 'a'), (2, 'a'), (3, 'a'), (4, 'a')]

这对全班来说是一份完美的工作

演示:


只需使用无限迭代器定义一个类,该类使用要注入列表中的单个元素进行初始化:

class zipIterator:
    def __init__(self, val):
        self.__val = val

    def __iter__(self):
        return self

    def __next__(self):
        return self.__val
然后从此类和您拥有的列表中创建新列表:

elements = [1, 2, 3, 4]
key = 'a'
res = [it for it in zip(elements, zipIterator(key))]
结果将是:

>>res
[(1, 'a'), (2, 'a'), (3, 'a'), (4, 'a')]
>l=[1,2,3,4]
>>>列表(zip(l,“a”*len(l)))
[(1,'a'),(2,'a'),(3,'a'),(4,'a')]

jonrsharpe所说的,但您可能想将其重写为生成器,而不是常规函数。@因为OP使用的是3.x,所以它们可以
返回zip(l,repeat(o))
;这是一个迭代器,不需要
产生
。很好,我错过了标记。只需小心o的可变值,否则你可能会得到一些惊喜
循环
的参数必须是一个iterable,不是吗?它与
'a'
一起工作,因为它是字符串,但不能与任意对象一起工作。@zegkljan yes它必须是一个iterable。只有当要映射到的对象是长度为1的iterable时,
cycle
才会给出这个问题所要求的结果。如果您对字符串
'ab'
而不是
'a
”执行相同的操作,您会得到以下结果:
[(1,'a'),(2,'b'),(3,'a'),(4,'b')]
字符串是可编辑的。如果您希望无限期地返回字符串,您应该将其放入容器中<代码>列表就可以了<代码>列表(zip(范围(20),循环([“ab”]))@Swier
class zipIterator:
    def __init__(self, val):
        self.__val = val

    def __iter__(self):
        return self

    def __next__(self):
        return self.__val
elements = [1, 2, 3, 4]
key = 'a'
res = [it for it in zip(elements, zipIterator(key))]
>>res
[(1, 'a'), (2, 'a'), (3, 'a'), (4, 'a')]