#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Why tuples? I dont want tuples.


    I posted on here a couple days ago about concatenating lists and got the answer I was looking for, but now Ive hit another snag. I get the lists combined and I learned how to write them to a file, but now the file with the combined lists is in tuple form and has the \n after every value like this: (aaa\n , 111\n)
    Im not looking for a tuple, I just want the aaa111 aaa222 and so on. Im just learning all this so the way I got that to work was to piece together code that I either googled or was told about on here, I looked through it and Im not seeing it. I figured that it was a formatting issue with python writing the code instead of the string or something, like writing print('hello') instead of the desired hello to the file. Anyway. Here is the code, Im combining two txt files, one named testWords which has aaa bbb ccc on seperate lines(hence \n popping up). The second file is testNum and has 111 222 333 on seperate lines also. Im combining these and making a new file testMyList.

    import itertools

    words = open('testWords.txt','r')
    numbers = open('testNum.txt','r')

    x = words
    y = numbers

    product_list = list(itertools.product(x, y))
    print([''.join(a) for a in product_list])

    with open('testMyList.txt','w') as file:
    for item in product_list:
    file.write('{}\n'.format(item))


    On a side note, Im wondering how to just return something like
    print([''.join(a) for a in product_list]) without printing it. I tried putting return in its place and it gave me an error that it was outside of the function.

    So there it is, close but no cigar. I also know the problem shows up before the writing to the final file if that helps.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    29
    Rep Power
    0
    Well, for one, don't post your code like that on the forums, use the code tags. And to solve your problem, as you can read from your code, you made a product_list with itertools.product(), which, if you read the documentation on itertools, is "Equivalent to nested for-loops in a generator expression. For example, product(A, B) returns the same as ((x,y) for x in A for y in B)". In the next step, you printed the output you want, which is a list of strings made by joining the product_list tuple values. So, to write that output to your file, all you need to do is:

    Code:
    import itertools
    
    words = open('testWords.txt','r')
    numbers = open('testNum.txt','r')
    
    x = words
    y = numbers
    
    product_list = list(itertools.product(x, y))
    products = [''.join(a) for a in product_list]
    
    with open('test_list.txt','w') as file:
        for item in products:
            file.write('{}\n'.format(item))
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by Lucantrop
    Well, for one, don't post your code like that on the forums, use the code tags. And to solve your problem, as you can read from your code, you made a product_list with itertools.product(), which, if you read the documentation on itertools, is "Equivalent to nested for-loops in a generator expression. For example, product(A, B) returns the same as ((x,y) for x in A for y in B)". In the next step, you printed the output you want, which is a list of strings made by joining the product_list tuple values. So, to write that output to your file, all you need to do is:

    Code:
    import itertools
    
    words = open('testWords.txt','r')
    numbers = open('testNum.txt','r')
    
    x = words
    y = numbers
    
    product_list = list(itertools.product(x, y))
    products = [''.join(a) for a in product_list]
    
    with open('test_list.txt','w') as file:
        for item in products:
            file.write('{}\n'.format(item))
    This sort of works, for the first few letters the numbers get appended to the next line. For the last letter the number is appended to the letter, which was shat I was going for. It was just numbers and letters do that was right also. I think the fact that the values are on separate lines is the problem because when it doesn't go to a new line it writes it in the right day. Is there a way to get around this ? The way it writes to test_list is like so:
    aaaaaa
    111111
    aaaaaa
    222222

    Then the end of list prints like:
    gggggg111111
    gggggg222222
    This is what I'm going for.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    29
    Rep Power
    0
    Originally Posted by JonnyDoeInWisco
    This sort of works, for the first few letters the numbers get appended to the next line. For the last letter the number is appended to the letter, which was shat I was going for. It was just numbers and letters do that was right also. I think the fact that the values are on separate lines is the problem because when it doesn't go to a new line it writes it in the right day. Is there a way to get around this ? The way it writes to test_list is like so:
    aaaaaa
    111111
    aaaaaa
    222222

    Then the end of list prints like:
    gggggg111111
    gggggg222222
    This is what I'm going for.
    Oh, yeah, sorry, I couldn't really test it right now, but I know what the problem is. When you define x and y, you need to remove the newline symbol from every item in the words/numbers lists. The reason the last one worked is because there aren't any newlines after the last word.
    Code:
    x = [i.strip('\n') for i in words]
    y = [j.strip('\n') for j in numbers]
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    There we go, thank you very much

IMN logo majestic logo threadwatch logo seochat tools logo