kamazeuci
kamazeuci

Reputation: 5

How to generate text using list values generated by permutations

I have:

N = a list of numbers from 0-29

A = a list of all alphabet letters.

I want to combine them in order to get all possible permutations of "NAN", from 0a0 up to 29z29

Then, I want to use each one of these "NAN" permutations and get them inside a URL, so I can get something like "http://www.geo.ma/NAN.txt"

import string
import itertools

#Generate the letter list
Alist=list(string.ascii_lowercase)
#Generate the number list
Nlist=list(range(30))

#Combine them in order to get "ABA"
CombinedList=(list((itertools.product(Nlist, Alist, Nlist))))

print(CombinedList)

I got the list, but now I try to get the permutations inside the URL:

for i in CombinedList:
    print('http://www.geo.ma/', i)

But i get http://www.geo.ma/ (29, z, 29) instead of what I'd like to get: http://geo.ma/29z29.txt

If I try to convert the List to string before trying to generate the URLS with StringedList = str(CombinedList), python just uses each one of the characters to generate the URLs, so I get things like http://geo.ma/].txt, http://geo.ma/9.txt, http://geo.ma/z.txt, http://geo.ma/).txt, http://geo.ma/).txt, etc.

Upvotes: 0

Views: 86

Answers (3)

ComplicatedPhenomenon
ComplicatedPhenomenon

Reputation: 4189

for i in CombinedList:
    print('http://www.geo.ma/'+str(i[0])+i[1]+str(i[2])+'.txt')

output is

http://www.geo.ma/0a0.txt
http://www.geo.ma/0a1.txt
http://www.geo.ma/0a2.txt
http://www.geo.ma/0a3.txt
http://www.geo.ma/0a4.txt
http://www.geo.ma/0a5.txt
http://www.geo.ma/0a6.txt
http://www.geo.ma/0a7.txt
http://www.geo.ma/0a8.txt
...

Upvotes: 0

ncica
ncica

Reputation: 7206

use .join()

for i in CombinedList:
    print('http://www.geo.ma/{}'.format(''.join(map(str,i))))

output:

...

http://www.geo.ma/29z22
http://www.geo.ma/29z23
http://www.geo.ma/29z24
http://www.geo.ma/29z25
http://www.geo.ma/29z26
http://www.geo.ma/29z27
http://www.geo.ma/29z28
http://www.geo.ma/29z29

Upvotes: 2

Jmonsky
Jmonsky

Reputation: 1519

baseURL = "http://www.geo.ma/"
for pair in CombinedList:
    N1, A, N2 = pair
    print(f"{baseURL}{N1}{A}{N2}.txt")

Upvotes: 2

Related Questions