1

次の2つのリストがあるとします。

column1 = ["Attribute:","Virtual machine:","Troll:"]
column2 = ["A value associated with an object which is referenced by name using dotted expressions. For example, if an object o has an attribute a it would be referenced as o.a.", 
           "A computer defined entirely in software. Python's virtual machine executes the bytecode emitted by the bytecode compiler.",
           "Someone who posts inflammatory, extraneous, or off-topic messages in an online community, such as a forum, chat room, or blog, with the primary intent of provoking readers into an emotional response or of otherwise disrupting normal on-topic discussion."]

このコード:

for c1, c2 in zip(column1, column2):
    print "%-16s %s" % (c1, c2)

このテキストを出力します:

Attribute:       A value associated with an object which is referenced by name u
sing dotted expressions. For example, if an object o has an attribute a it would
be referenced as o.a.
Virtual machine: A computer defined entirely in software. Python's virtual machi
ne executes the bytecode emitted by the bytecode compiler.
Troll:           Someone who posts inflammatory, extraneous, or off-topic messag
es in an online community, such as a forum, chat room, or blog, with the primary
 intent of provoking readers into an emotional response or of otherwise disrupti
ng normal on-topic discussion.

私はこれが欲しいですが:

Attribute:       A value associated with an object which is referenced by name 
                 using dotted expressions. For example, if an object o has an 
                 attribute a it would be referenced as o.a.
Virtual machine: A computer defined entirely in software. Python's virtual 
                 machine executes the bytecode emitted by the bytecode compiler.
Troll:           Someone who posts inflammatory, extraneous, or off-topic 
                 messages in an online community, such as a forum, chat room, or 
                 blog, with the primary intent of provoking readers into an 
                 emotional response or of otherwise disrupting normal on-topic
                 discussion.

どの端末サイズでもこの結果を得るにはどうすればよいですか?(誰かがクリントがこれを非常に簡単に達成できるかもしれないと私にアドバイスしました、誰かがこれをまだしましたか?)

注:単語が行の終わりまでに細かく切り刻まれないという要件は、私にとって二次的なものにすぎません。最も重要な要件は、リストの各文字列要素を、そのcolumn2リストの各要素の文字列が開始した場所と同じ水平方向の間隔で継続させることです。

4

2 に答える 2

1

これは私のために働いた:

import textwrap

for c1, c2 in zip(column1, column2):
    mc2 = "".join(textwrap.fill(c2, width=50, initial_indent="", subsequent_indent="\t\t ", break_long_words = False))
    print "%-16s %s" % (c1, mc2)

で同じ結果を得る人は誰でもClint、この答えの代わりに投票を受け取ります。

于 2012-08-24T11:01:05.933 に答える
1
from clint.textui import puts, columns

width = 20

for term, definition in zip(column1, column2):
    puts(columns([term, width], [definition, None]))

...コンテンツに基づいて列幅を選択するには、このプレフィックスを付けます。

#derive 1st column width from longest term
width = 0
for s in column1:
    if len(s) > width :
        width = len(s) + 1

clint \ examples\text_width.pyから適応。質問ありがとうございます。以前はこれを行う方法がわかりませんでした。これを使用します。

于 2012-11-17T08:14:11.460 に答える