0

i have one list with some coordinates in it, when i am printing it like that:

for i in range(0,len(List)):
    print("".join(["(%d, %d) \n" % (y[i], y[i+1]) for y in (List)]))

the output is this:

(0, 3) 
(0, 2) 
(0, 1) 
(1, 1) 
(1, 2) 
(2, 2) 
(2, 1) 
(3, 1) 
(3, 0) 
(2, 0) 
(1, 0) 
(0, 0)

i want to save the output in a .txt, but that is not a problem, my problem is that the .txt must be formmated like this:

(0, 3), (0, 2)
(0, 2), (0, 1)
(0, 1),(1, 1)
(1, 1),(1, 2)
(1, 2),(2, 2)
.....

i've tried many things but nothing worked.. it must be easy, but i am new to python thank you in advance

euler87
  • 31
  • 1
  • 8
  • 2
    Looks like you want to use [this answer](http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python) and group into 4's, then format – Jon Clements Mar 29 '15 at 16:50
  • Try not to do complex expressions like that because it isn't very pythonic - at least try to use comments. – noɥʇʎԀʎzɐɹƆ Mar 29 '15 at 17:04

5 Answers5

3

This does the trick:

l = [(0, 3), (0, 2), (0, 1), (1, 1), (1, 2), (2, 2)]

for i in range(0, len(l), 2):
    print(', '.join([str(l[i]), str(l[i+1])]))

# (0, 3), (0, 2)
# (0, 1), (1, 1)
# (1, 2), (2, 2)
maahl
  • 547
  • 3
  • 17
  • thanks, this worked, but instead of (0, 3), (0, 2) i have [0, 3], [0, 2] – euler87 Mar 29 '15 at 17:15
  • It means you get lists earlier in your code. You can turn the lists into tuples with `tuple([0, 3]) # returns (0, 3)`. – maahl Mar 29 '15 at 17:22
  • This doesn't work. The latter number remains the same. – Zizouz212 Mar 29 '15 at 17:26
  • when i print my list is like that [[0, 3], [0, 2],......], it's not like the above example of viod.The viod answer is working but the output is with brackets not parenthesis, and i tried this thing with the tuple – euler87 Mar 29 '15 at 17:33
  • @user2911293: this is because you have a list of lists, and not a list of tuples. To turn your whole list of lists into a list of tuples, you can use the following: `l = [tuple(x) for x in l]` where `l` is your list. Then you can use my answer above exactly as it is. – maahl Mar 29 '15 at 17:36
  • another minor problem, the code works and the output is correct, but i am getting this error: `print(', '.join([str(L[i]), str(L[i+1])]), file=fo)` `IndexError: list index out of range` ,am i doing something wrong? – euler87 Mar 29 '15 at 18:30
  • This is very probably because you have an odd number of elements in your list. You will have to handle the last element separately, for example by iterating only in `range(0, len(l)-1, 2)`, and then accessing the last element with `l[-1]`. – maahl Mar 29 '15 at 19:24
1

You can use zip.

mylist = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
# That doesn't have to be the numbers though, you can use your own.

for a, b in zip(mylist, mylist[1:]):
    print("{}, {}".format(a, b), file = myfile) # Will print to myfile.

That's assuming that you are printing to an open file. Leave the file argument out if you don't want to print anywhere else, but the default screen.

in myfile.txt:

(0, 0), (0, 1)
(0, 1), (0, 2)
(0, 2), (1, 0)
(1, 0), (1, 1)
(1, 1), (1, 2)
(1, 2), (2, 0)
(2, 0), (2, 1)
(2, 1), (2, 2)    

The output is in tuples, not lists.

Zizouz212
  • 4,908
  • 5
  • 42
  • 66
0

Well, besides other answers playing with indices, you can also use zip:

for a, b, c, d in zip(List, List[1:], List[2:], List[3:]):
    print('({}, {}), ({}, {})'.format(a, b, c, d))
Francis Colas
  • 3,459
  • 2
  • 26
  • 31
0

Regarding expected output:

l = [(0, 3), (0, 2), (0, 1), (1, 1), (1, 2), (2, 2)]

for i,j in zip(l,l[1:]):
    print str(i) + "," + str(j) # or print ",".join([str(i),str(j)])

#output
(0, 3),(0, 2)
(0, 2),(0, 1)
(0, 1),(1, 1)
(1, 1),(1, 2)
(1, 2),(2, 2)

If you want list chunking with 2 elements,Try this

def chunks(l, n):
    for i in xrange(0, len(l), n):
        yield l[i:i+n]


for i,j in list(chunks(l,2)):
    print ",".join([str(i),str(j)])

#output
(0, 3),(0, 2)
(0, 1),(1, 1)
(1, 2),(2, 2)
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
-2

Your code:

for i in range(0,len(List)):
    print("".join(["(%d, %d) \n" % (y[i], y[i+1]) for y in (List)]))

Turning this into a list comp.

t = ["".join(["(%d, %d) \n" % (y[i], y[i+1]) for y in (List)]) for i in range(0,len(List))]

(equivalent to)

s = []
for i in range(0,len(List)):
    s.append("".join(["(%d, %d) \n" % (y[i], y[i+1]) for y in (List)]))

Then:

first = True
other = None
r = ""
for i in t:
    if not first:
        r += other+", "+i+"\n"
        first = True
    else:
        other = i
        first = False
f = open("out.txt","w")
f.write(t)
f.close() #go to notepad
noɥʇʎԀʎzɐɹƆ
  • 9,967
  • 2
  • 50
  • 67
  • 1
    `/n` **isn't** a new line... you're attempting to write to a file that's not opened in *write* mode... as for the rest of it... needs some work :) (the output won't match what the OP wants, even if the logic is correct - did you test this at all?) – Jon Clements Mar 29 '15 at 17:06
  • Bud, what happened to setting your reading options in `open` and your wrong newline tokens. – Zizouz212 Mar 29 '15 at 17:34