基于另一个元素的一个列表中的Python重复元素

时间:2016-06-02 15:00:33

标签: python python-3.x repeat

鉴于以下清单:

a = [0, 5, 1]
b = [1, 2, 1]

我想用[b]中相应位置的数字重复[a]的每个元素来产生这个:

[0, 5, 5, 1]

即。 0发生1次,5发生2次,1发生1次。

4 个答案:

答案 0 :(得分:5)

zip() functionitertools.repeat()itertools.chain.from_iterable()

一起使用
try:
    # use iterator zip on Python 2 too
    from future_builtins import zip
except ImportError:
    pass
from itertools import repeat, chain

list(chain.from_iterable(repeat(value, count) for value, count in zip(a, b)))

(我为那些无法切换到Python 3的人添加了Python 2 forward-compatible import

演示:

>>> from itertools import repeat, chain
>>> a = [0, 5, 1]
>>> b = [1, 2, 1]
>>> list(chain.from_iterable(repeat(value, count) for value, count in zip(a, b)))
[0, 5, 5, 1]

另一种方法是使用列表理解;由于重复元素是在字节码而不是C:

中完成的,因此速度较慢
[value for value, count in zip(a, b) for _ in range(count)]

答案 1 :(得分:2)

In [7]: a = [0, 5, 1]

In [8]: b = [1, 2, 1]

In [9]: list(itertools.chain(*(itertools.repeat(elem, n) for elem, n in zip(a, b))))
Out[9]: [0, 5, 5, 1]

In [10]: b = [2, 3, 4]

In [11]: list(itertools.chain(*(itertools.repeat(elem, n) for elem, n in zip(a, b))))
Out[11]: [0, 0, 5, 5, 5, 1, 1, 1, 1]

这里的各个部分如下:

  • itertools.repeat(elem, n) - 重复elem n次
  • zip(a, b)列出两个列表中的2元组列表,将每个元素与另一个列表中的相应元素配对。这为您提供了在您的用例中传递给itertools.repeat的确切内容。
  • itertools.chain - 将生成的迭代器列表展平为单个值列表。您可以像我一样chain(*iterable)或像Martijn Peters那样chain.from_iterable(iterable)

答案 2 :(得分:2)

这可以使用enumerate()直接完成:

a = [0, 5, 1]
b = [1, 2, 1]
[ele for i, ele in enumerate(a) for j in range(b[i])]

答案 3 :(得分:0)

使用list comprehension,你想获得的是:

version: "3.3"

services:
  traefik:
    image: "traefik:latest"
    restart: always
    container_name: "traefik"
    command:
      - "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=true"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--certificatesresolvers.letsencrypt.acme.tlschallenge=true"
      - "--certificatesresolvers.letsencrypt.acme.email=myemail" # Let's Encrypt email
      - "--certificatesresolvers.letsencrypt.acme.storage=/letsencrypt/acme.json"

    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - "./letsencrypt:/letsencrypt"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  db:
    image: mysql:latest
    volumes:
       - db_data:/var/lib/mysql
    restart: always
    container_name: "db"
    environment:
       MYSQL_ROOT_PASSWORD: mypassword
       MYSQL_DATABASE: mydb
       MYSQL_USER: myuser
       MYSQL_PASSWORD: mypassword
    ports:
      - "32769:3306"
  api:
    image: "myregistry/myimage:latest"
    restart: always
    container_name: "api"
    labels:
      - "traefik.enable=true"

      - traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https
      - traefik.http.routers.web.middlewares=redirect-to-https
      - traefik.http.routers.web.rule=Host(`mydomainapi`)
      - traefik.http.routers.web.entrypoints=web

      - "traefik.http.routers.websecure.rule=Host(`mydomainapi`)"
      - "traefik.http.routers.websecure.entrypoints=websecure"
      - "traefik.http.routers.websecure.tls.certresolver=letsencrypt"

    depends_on:
      - db
  frontend:
    image: "myregistry/myimagefrontend:latest"
    restart: always
    container_name: "frontend"
    labels:
      - "traefik.enable=true"
      - traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https
      - traefik.http.routers.web.middlewares=redirect-to-https
      - traefik.http.routers.web.rule=Host(`mydomainfrontend`)
      - traefik.http.routers.web.entrypoints=web
      
      - "traefik.http.routers.websecure.rule=Host(`mydomainfrontend`)"
      - "traefik.http.routers.websecure.entrypoints=websecure"
      - "traefik.http.routers.websecure.tls.certresolver=letsencrypt"

    depends_on:
      - api

volumes:
  db_data:
    driver: local

对于短列表,使用 [2021-01-27T11:51:18,838][INFO ][logstash.setting.writabledirectory] Creating directory {:setting=>"path.dead_letter_queue", :path=>"C:\\Pippo\\logstash-7.6.1\\data\\dead_letter_queue"}

可以方便地实现 <块引用>

What does the built-in function sum do with sum(list, [])?

因为对于短列表,它可能比 [a[0]]*b[0] + [a[1]]*b[1] + [a[2]]*b[2] + sum(list, []) 更快(也更直接)。

itertools.chain

对于长列表,坚持使用 itertools.repeatsum([ [a_i]*b_i for a_i, b_i in zip(a, b) ], [])