Python - two word delimited print tab

I have a set of words like:

mike dc car dc george dc jerry dc

Each word mike dc george dcis separated by a space. How can I create a two-word set and separate the two words given by the tab? I would like to print it to standard output stdout.

EDIT I tried using this: print '\t'.join(hypoth)but it really doesn't cut. All words here are simply limited to tabs. Ideally, I would like the first two words to be separated by a space, and each of the two language tabs is limited.

+4
source share
2 answers

Assuming you have

two_word_sets = ["mike dc", "car dc", "george dc", "jerry dc"]

using

print "\t".join(two_word_sets)

or, for Python 3:

print("\t".join(two_word_sets))

to print a tab-delimited list to standard output.

If you only have

mystr = "mike dc car dc george dc jerry dc"

a :

words = mystr.split()
two_word_sets = [" ".join(tup) for tup in zip(words[::2], words[1::2])]

, , zip(a_proto[::2], a_proto[1::2]) - [('mike', 'dc'), ('car', 'dc'), ('george', 'dc'), ('jerry', 'dc')]. .

, / izip [itertools], zip , izip .

+6

1-2 , , :

words = "mike dc car dc george dc jerry dc"
wlist = words.split()
mystr = ""
for i in range(0, len(wlist), 2):
    mystr = "%s%s %s\t" % (mystr, wlist[i], wlist[i+1])
print mystr
+2

Source: https://habr.com/ru/post/1523827/


All Articles