0

Aim it to concatenate multiple lists to one list within a for loop. At the moment the output prints map objects rather than all the content merged in one list.

Directory

enter image description here

Content of each sample file

aa - http://cc
bb - http://bb
cc - http://aa
aa - aa - http://cc
bb - bb - http://bb
cc - cc - http://aa
aa - aa - aa - http://cc
bb - bb - bb - http://bb
cc - cc - cc - http://aa

Code

import os

cwd = os.getcwd()

for fn in os.listdir(cwd):
    with open(fn, "r+") as f, open(cwd + "\\all.txt", "r+") as f2:
        lines = f.readlines()

        my_list = [fn + " - " + x for x in lines]

        concatenated_list = map(" ".join, zip(my_list))
        print(concatenated_list)

Outcome

Current

C:\case.py
<map object at 0x0000000002CB7BE0>
<map object at 0x0000000002CB7B00>
<map object at 0x0000000002CB79E8>
<map object at 0x0000000002CB7E10>
<map object at 0x0000000002CB7A90>
<map object at 0x0000000002CB78D0>

Expected

C:\Users\r\Desktop\test>C:\Users\r\Desktop\case.py
[]
['sample.txt - aa - http://cc\n', 'sample.txt - bb - http://bb\n', 'sample.txt -
 cc - http://aa\n', 'sample.txt - aa - aa - http://cc\n', 'sample.txt - bb - bb
- http://bb\n', 'sample.txt - cc - cc - http://aa\n', 'sample.txt - aa - aa - aa
 - http://cc\n', 'sample.txt - bb - bb - bb - http://bb\n', 'sample.txt - cc - c
c - cc - http://aa\n','sample2.txt - aa - http://cc\n', 'sample2.txt - bb - http://bb\n', 'sample2.tx
t - cc - http://aa\n', 'sample2.txt - aa - aa - http://cc\n', 'sample2.txt - bb
- bb - http://bb\n', 'sample2.txt - cc - cc - http://aa\n', 'sample2.txt - aa -
aa - aa - http://cc\n', 'sample2.txt - bb - bb - bb - http://bb\n', 'sample2.txt
 - cc - cc - cc - http://aa\n'...
1
  • I may not be understanding what you're doing, but it sure looks to me like the zip, join and map calls are all completely unnecessary. Get rid of them all and just use print(my_list) and you'll get the desired result. Commented Aug 6, 2014 at 21:55

3 Answers 3

3

In Python 3.x, map() returns an iterator, not a list. Try this instead:

concatenated_list = []
# Then in your for loop...
  concatenated_list += [" ".join(x) for x in zip(my_list)]

The += will concatenate all of the lists together.

Also don't overload the list keyword. You don't want to overwrite the name of a built-in function. If you used the method in Padraic's answer, it would fail because Python thinks you're trying to call your list as a function, instead of the builtin list() function.

Sign up to request clarification or add additional context in comments.

4 Comments

The lists are still separated C:\>C:\case.py [] ['sample.txt - aa - http://cc\n', 'sample.txt - bb - http://bb\n', 'sample.txt - cc - http://aa\n', 'sample.txt - aa - aa - http://cc\n', 'sample.txt - bb - bb - http://bb\n', 'sample.txt - cc - cc - http://aa\n', 'sample.txt - aa - aa - aa - http://cc\n', 'sample.txt - bb - bb - bb - http://bb\n', 'sample.txt - cc - c c - cc - http://aa'] ['sample2.txt - aa - http://cc\n', 'sample2.txt - bb - http://bb\n', 'sample2.tx t - cc - http://aa\n', 'sample2.txt - aa - aa - http://cc\n', 'sample2.txt - bb - bb -
To turn something into a list, you can simply use list(...) -- no need for the awkward [x for x in ...]. In this particular case, you could also replace the map() call by a list comprehension: [" ".join(x) for x in zip(my_list)].
@SvenMarnach I figured it could be simplified, but wasn't 100% sure what he was doing so I didn't want to replace something that seemed to be working alright. I've edited my answer with your suggestion though. Thanks!
Final code: import os cwd = os.getcwd() concatenated_list = [] for fn in os.listdir(cwd): with open(fn, "r+") as f, open(cwd + "\\all.txt", "r+") as f2: lines = f.readlines() my_list = [fn + " - " + x for x in lines] concatenated_list += [" ".join(x) for x in zip(my_list)] lines = sorted(concatenated_list, key=lambda text: text.rsplit('-', 1)[::-1]) f2.write("\n".join(lines))
1

Use list(concatenated_list) map and zip are iterators in python3 , also don't use list as a variable name it shadows the builtin list

           my_list =  [fn + " - " + x for x in lines]
           concatenated_list = map(" ".join, [x for x in zip(my_list)])
           print (list(concatenated_list))

Comments

-1

concatenated_list = list(map(" ".join, zip(my_list))) I agree with Padraic, put list in front of map

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.