0

I have a list of words i.e. [ cat, dog, bird, horse, pig, duck, frog, chicken, sheep, cow] I want to print say nine of them in a grid like this:

cat    dog    horse

bird   pig   duck

frog   chicken  sheep

is there anyway of doing this? Also when I get them to print out o a line I get the \n line break at the end of each item which is so annoying - is there a simple fix for this

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 2
    http://stackoverflow.com/questions/30870328/how-do-you-put-words-into-a-3x3-grid-using-python/30870561 – Avión Jun 18 '15 at 08:31
  • possible duplicate of [Using a text file to lay out a words into a grid form](http://stackoverflow.com/questions/23023403/using-a-text-file-to-lay-out-a-words-into-a-grid-form) – Delimitry Jun 18 '15 at 08:40
  • Is there any logic behind your output? and did you gave tried any thing by yourself? – Mazdak Jun 18 '15 at 08:41
  • Your input is not Python code. Is this a text file? –  Jun 18 '15 at 08:44
  • if i import a text file using readlines it adds \n at the end of each word why? – Alan Robinson Jun 19 '15 at 07:49

5 Answers5

2

Like this:

li = [ 
    'cat', 'dog', 'bird', 
    'horse', 'pig', 'duck', 
    'frog', 'chicken', 'sheep', 
    'cow'
]

lines = [" ".join(li[x:x+3]) for x in xrange(0, len(li), 3)]
for line in lines:
    print line
krasem
  • 21
  • 3
0

I'm assuming those are strings? In that case, you could use a for loop and print the entire string minus the \n characters, something like cat[:-2] for example.

Woody1193
  • 7,252
  • 5
  • 40
  • 90
0

You can do it by finding the longest word (animal name) and using that length as a padding between the words. The following will split the strings in to groups of 3. Any reminding elements after the split will be disregarded:

animals = ['cat', 'dog', 'bird', 'horse', 'pig', 'duck', 'frog', 'chicken', 'sheep', 'cow']
padding = max(map(len, animals)) + 2
groups = zip(*(iter(animals),) * 3)

for group in groups:
    print("".join(("{:%i}" % padding).format(a) for a in group))
veiset
  • 1,973
  • 16
  • 16
0

You can iterate through your list in steps, which is number of words per line.

Then I pick the part of the list I want with [start:end] range, and print using join()which will join all items in list with selected string in between them.

Then I move on next part of list.

words = ['a','b','c','d','e','f','g','h','i','j']

def printNum(n, lst):
    start = 0
    end = start + n
    for line in lst[::n]:
        print "\t".join(lst[start:end])
        start = end
        end = start + n
print "3 words per line:"
printNum(3, words)

print "2 words per line:"
printNum(2, words)

Output:

3 words per line:
a       b       c
d       e       f
g       h       i
j

2 words per line:
a       b
c       d
e       f
g       h
i       j
ThePavolC
  • 1,693
  • 1
  • 16
  • 26
  • Do you know why when you read a text file in to a list in python it adds \n at the end of each item. This makes it impossible to search for an item eg cat because it is now cat\n – Alan Robinson Jun 19 '15 at 08:16
  • You can use `strip()` to remove all special characters from start and end of string. – ThePavolC Jun 19 '15 at 08:40
  • When you're reading lines from files you can call `strip()` on each line to remove `\n`characters. – ThePavolC Jun 19 '15 at 09:05
  • ok but it wont modify the actual list which makes it difficult to find items as you have to look for cat\n not cat bizarre language is python v.annoying – Alan Robinson Jun 19 '15 at 09:10
  • Update your question and show the code you're struggling with. – ThePavolC Jun 19 '15 at 09:21
0

One more solution:

WORDS = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']

# how much words to print out in a table
word_count = 9
# number of words in a row
row_length = 3

words = WORDS[:word_count]
max_word_length = max(len(x) for x in words)

for index, word in enumerate(words):
    word = "{0:>{1}}".format(word, max_word_length + 1)
    if (index + 1) % row_length:
        print word,
    else:
        print word
Aleksandr K.
  • 528
  • 2
  • 12