提问人:skywalker 提问时间:5/5/2023 最后编辑:InSyncskywalker 更新时间:6/29/2023 访问量:83
Python3 遍历列表并按特定顺序/组合打印
Python3 iterate through a list and print in a certain sequence/combination
问:
我有一个单词列表,我想遍历并打印特定顺序。
例如:
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]))
这真的很接近,因为它确实按照我正在寻找的顺序打印了单词,但它并没有像我希望的那样经历每一个组合。
答:
-1赞
InSync
5/5/2023
#1
在指数方面,您的预期结果如下所示:
0000
0001
0010
0100
1000
1001
1010
1100
1101
1110
1111
...
假设是单词列表,并且是我们函数中每个序列的元素数。我们需要三个迭代器:words
n
yield
- 1 从 1 迭代到 ;顺其自然吧
n
i
- 1 从向下迭代到 1;顺其自然吧
n
j
- 1 从 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')
...
'''
评论
0赞
skywalker
6/29/2023
所以这确实有效,直到某个点。它停得相对较短,我不知道为什么
0赞
InSync
6/29/2023
@CalebSchilling “相对短暂停止”是什么意思?我在本地尝试了它,它按照您需要的确切顺序打印了 41 个结果。
-1赞
911
5/5/2023
#2
正如 Samwise 所说,使用产品
。
from itertools import product
words = ["apple", "banana", "orange", "pear", "berry"]
for i in product(words,repeat=4):
print(".".join(i))
评论
0赞
Bibhav
5/5/2023
使用:而不是打字 4 次。for word in product(words,repeat=4)
words
0赞
911
5/5/2023
好的,谢谢,我对产品包装还不是很熟悉。
评论