获取文件中每一行的排列组合。

huangapple go评论62阅读模式
英文:

get permutations of each line in file

问题

1. El Paso Yakima Sacramento Riverside Fresno Rialto Denver Fordham
2. El Paso Yakima Sacramento Riverside Fresno Rialto Denver Fordham
3. El Paso Yakima Sacramento Riverside Fresno Rialto Denver Fordham
4. El Paso Yakima Sacramento Riverside Fresno Rialto Denver Fordham
5. El Paso Yakima Sacramento Riverside Fresno Rialto Denver Fordham
6. Westminster Austin Tiverton Greensboro Riverside Fresno Rialto Denver Fordham
7. Westminster Austin Tiverton Greensboro Riverside Fresno Rialto Denver Fordham
8. Westminster Austin Tiverton Greensboro Riverside Fresno Rialto Denver Fordham
9. Westminster Austin Tiverton Greensboro Riverside Fresno Rialto Denver Fordham
10. Westminster Austin Tiverton Greensboro Riverside Fresno Rialto Denver Fordham
11. Westminster Lakeview Miami Tiverton Riverside Fresno Rialto Denver Fordham
12. Westminster Lakeview Miami Tiverton Riverside Fresno Rialto Denver Fordham
13. Westminster Lakeview Miami Tiverton Riverside Fresno Rialto Denver Fordham
14. Westminster Lakeview Miami Tiverton Riverside Fresno Rialto Denver Fordham
15. Westminster Lakeview Miami Tiverton Riverside Fresno Rialto Denver Fordham
16. Lakeview Austin Tiverton Greensboro Riverside Fresno Rialto Denver Fordham
17. Lakeview Austin Tiverton Greensboro Riverside Fresno Rialto Denver Fordham
18. Lakeview Austin Tiverton Greensboro Riverside Fresno Rialto Denver Fordham
19. Lakeview Austin Tiverton Greensboro Riverside Fresno Rialto Denver Fordham
20. Lakeview Austin Tiverton Greensboro Riverside Fresno Rialto Denver Fordham
21. Easton Yakima Miami St. Louis Riverside Fresno Rialto Denver Fordham
22. Easton Yakima Miami St. Louis Riverside Fresno Rialto Denver Fordham
23. Easton Yakima Miami St. Louis Riverside Fresno Rialto Denver Fordham
24. Easton Yakima Miami St. Louis Riverside Fresno Rialto Denver Fordham
25. Easton Yakima Miami St. Louis Riverside Fresno Rialto Denver Fordham
英文:

I have a text file that looks like this

1. El Paso there Yakima Sacramento Riverside Fresno Rialto Denver Fordham
2. Westminster Austin Tiverton Greensboro Riverside Fresno Rialto Denver Fordham
3. Westminster Lakeview Miami Tiverton Riverside Fresno Rialto Denver Fordham
4. Lakeview Austin Tiverton Greensboro Riverside Fresno Rialto Denver Fordham
5. Easton Yakima Miami St. Louis Riverside Fresno Rialto Denver Fordham

I want to print the permutations of every line(n = 9, k= 9), so that I end up with 1814400 total lines (362880 x 5).Here is my code

import itertools

with open('12round.txt') as file:
> for line in file:
> perms = itertools.permutations(line, 9)
> for i in perms:
> print(' '.join(i))`

however it prints like letters and its just weird. Please help! I've been trying to find a solution and I'm getting a headache not finding anything thanks in advance.

this is as far as my script writing goes.

答案1

得分: 2

如果我正确理解您的问题,您读取了一行文本,需要获取该行的单词,然后进行排列组合操作。我猜您的问题可能在于未删除空格并提取单词,然后再运行itertools.permutations。以下是一个示例代码(我根据您的示例取了n=9,k=9):

import itertools
with open('file.txt') as file:
    for line in file:
        line = line.strip()
        perms = itertools.permutations(line.split(), 9) 
        for i in range(9):
           for perm in perms:
             print(' '.join(perm))

希望这能帮助您解决问题。

英文:

if I understood your problem correctly, you read a line, you need to get the words of this line, then you do your permutations accordingly,
I guess your problem is that you are not removing the white spaces and taking the words then running itertools.permutations.
Here is a sample code: (I took the n=9, k=9 according to your example)

import itertools
with open('file.txt') as file:
    for line in file:
        line = line.strip()
        perms = itertools.permutations(line.split(), 9) 
        for i in range(9):
           for perm in perms:
             print(' '.join(perm))

huangapple
  • 本文由 发表于 2023年5月7日 06:20:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定