Python3 遍历列表并按特定顺序/组合打印

问题描述 投票:0回答:4

我有一个单词列表,我想遍历并打印一个特定的顺序。

例如:

words = ['apple', 'banana', 'orange', 'pear', 'berry']

我希望序列是这样的:

apple.apple.apple.apple
apple.apple.apple.banana
apple.apple.banana.apple
apple.banana.apple.apple
banana.apple.apple.apple
banana.apple.apple.banana
banana.apple.banana.apple

我希望你明白它的要点。但它基本上会打印出上述四字模式中所有可能的组合。

words = []
with open('words') as my_file:
    words = my_file.read().splitlines()
for i in range(len(words)):
    if i+4 <= len(words):
        print(".".join(words[i:i+4]))

for j in range(1, len(words)):
    if j+4 <= len(words):
        print(".".join(words[j:j+4]))

这真的很接近,因为它确实按照我正在寻找的顺序打印了单词,但它并没有像我希望的那样经历每一个组合。

python list loops iteration sequence
4个回答
0
投票
from itertools import combinations_with_replacement

list(combinations_with_replacement(words, 4))

0
投票

正如Samwise所说,使用

product

from itertools import product

words = ["apple", "banana", "orange", "pear", "berry"]
for i in product(words,repeat=4):
    print(".".join(i))

0
投票

就指数而言,您的预期结果如下所示:

0000
0001
0010
0100
1000
1001
1010
1100
1101
1110
1111
...

words
为单词列表,
n
为每个序列
yield
的元素数量。我们需要三个循环:

  • One 从 1 迭代到
    n
    ;让它成为
    i
  • One 从
    n
    向下迭代到 1;让它成为
    j
  • One 从 1 迭代到
    j
    ;让它成为
    k

视觉解释:

table {
  font-family: monospace;
}

td[rowspan] {
  position: relative;
}

td[rowspan] > span {
  position: sticky;
  top: 0;
}

.j, td:nth-last-child(3) > span {
  background: #ddd;
}

.k, td:nth-last-child(2) > span {
  color: #f00;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<table class="table">
  <tbody>
    <tr>
      <th scope="col">i</th>
      <th scope="col"><span class="j">j</span></th>
      <th scope="col"><span class="k">k</span></th>
      <th scope="col">product</th>
    </tr>
    <tr>
      <td><span>0</span></td>
      <td><span>0</span></td>
      <td><span>0</span></td>
      <td><span>0000</span></td>
    </tr>
    <tr>
      <td rowspan="10"><span>1</span></td>
      <td rowspan="4"><span>4</span></td>
      <td><span>1</span></td>
      <td><span><span class="j">0</span>00<span class="k">1</span></span></td>
    </tr>
    <tr>
      <td><span>2</span></td>
      <td><span><span class="j">0</span>0<span class="k">1</span>0</span></td>
    </tr>
    <tr>
      <td><span>3</span></td>
      <td><span><span class="j">0</span><span class="k">1</span>00</span></td>
    </tr>
    <tr>
      <td><span>4</span></td>
      <td><span><span class="j k">1</span>000</span></td>
    </tr>
    <tr>
      <td rowspan="3"><span>3</span></td>
      <td><span>1</span></td>
      <td><span>1<span class="j">0</span>0<span class="k">1</span></span></td>
    </tr>
    <tr>
      <td><span>2</span></td>
      <td><span>1<span class="j">0</span><span class="k">1</span>0</span></td>
    </tr>
    <tr>
      <td><span>3</span></td>
      <td><span>1<span class="j k">1</span>00</span></td>
    </tr>
    <tr>
      <td rowspan="2"><span>2</span></td>
      <td><span>1</span></td>
      <td><span>11<span class="j">0</span><span class="k">1</span></span></td>
    </tr>
    <tr>
      <td><span>2</span></td>
      <td><span>11<span class="j k">1</span>0</span></td>
    </tr>
    <tr>
      <td><span>1</span></td>
      <td><span>1</span></td>
      <td><span>111<span class="j k">1</span></span></td>
    </tr>
    <tr>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
  </tbody>
</table>

实施:

def product(words, n):
  def to_words(indices):
    return tuple(words[index] for index in indices)

  # The (0, 0, 0, 0) case
  # Tuple does not support item assignment so we'll have to use list.
  current = [0] * n
  yield to_words(current)

  for i in range(1, n + 1):
    for j in range(n, 0, -1):
      # (0, 0, 0, 0) -> (0, 0, 0, 1)
      current[-1] += 1
      yield to_words(current)
      
      for k in range(1, j):
        # j = 4, k = 1
        # (0, 0, 0, 1) -> (0, 0, 1, 0)
        # ...
        # j = 3, k = 2
        # (1, 0, 1, 0) -> (1, 1, 0, 0)
        # j = 2, k = 1
        # (1, 1, 0, 1) -> (1, 1, 1, 0)
        # ...
        current[-k - 1], current[-k] = current[-k], current[-k - 1]
        yield to_words(current)

试试看:

words = ['apple', 'banana', 'orange', 'pear', 'berry']

print(*product(words, 4))

'''
('apple', 'apple', 'apple', 'apple')
('apple', 'apple', 'apple', 'banana')
('apple', 'apple', 'banana', 'apple')
('apple', 'banana', 'apple', 'apple')
('banana', 'apple', 'apple', 'apple')
('banana', 'apple', 'apple', 'banana')
('banana', 'apple', 'banana', 'apple')
('banana', 'banana', 'apple', 'apple')
('banana', 'banana', 'apple', 'banana')
('banana', 'banana', 'banana', 'apple')
('banana', 'banana', 'banana', 'banana')
...
'''

-1
投票

之前添加的评论给出了正确方向的想法。非常简单的代码:

from itertools import product

def prod(arr):
    a= list(product(arr, arr, arr, arr))
    for item in a:
        r = ""
        for w in item:
            r+=w+"."
        print(r[:-1])
© www.soinside.com 2019 - 2024. All rights reserved.