2008-10-05 18 views
16

मैं स्ट्रिंग मानों की एक सूची को 2 कॉलम प्रारूप में आउटपुट करने का प्रयास कर रहा हूं। "सामान्य पाठ" में तारों की सूची बनाने का मानक तरीका string.join विधि का उपयोग कर है। हालांकि, इसमें केवल 2 तर्क होते हैं इसलिए मैं केवल "\ n" का उपयोग कर एक ही कॉलम बना सकता हूं। मैंने सोचा कि एक लूप बनाने की कोशिश कर रहा है जो कॉलम के बीच एक टैब जोड़ देगा, लेकिन यह तर्क सही ढंग से काम नहीं करेगा।कॉलम में टेक्स्ट की एक सूची स्वरूपण

मुझे ActiveState page मिला जो कि इसे करने का एक काफी जटिल तरीका है लेकिन यह 4 साल पहले से है। क्या आजकल ऐसा करने का कोई आसान तरीका है?


संपादित यहाँ सूची है कि मैं का उपयोग करना चाहते है।

skills_defs = ["ACM:Aircraft Mechanic", "BC:Body Combat", "BIO:Biology", 
    "CBE:Combat Engineer", "CHM:Chemistry", "CMP:Computers", 
    "CRM:Combat Rifeman", "CVE:Civil Engineer", "DIS:Disguise", 
    "ELC:Electronics","EQ:Equestrian", "FO:Forward Observer", 
    "FOR:Forage", "FRG:Forgery", "FRM:Farming", "FSH:Fishing", 
    "GEO:Geology", "GS:Gunsmith", "HW:Heavy Weapons", "IF:Indirect Fire", 
    "INS:Instruction", "INT:Interrogation", "JP:Jet Pilot", "LB:Longbow", 
    "LAP:Light Aircraft Pilot", "LCG:Large Caliber Gun", "LNG:Language", 
    "LP:Lockpick", "MC:Melee Combat", "MCY:Motorcycle", "MEC:Mechanic", 
    "MED:Medical", "MET:Meterology", "MNE:Mining Engineer", 
    "MTL:Metallurgy", "MTN:Mountaineering", "NWH:Nuclear Warhead", 
    "PAR:Parachute", "PST:Pistol", "RCN:Recon", "RWP:Rotary Wing Pilot", 
    "SBH:Small Boat Handling","SCD:Scuba Diving", "SCR:Scrounging", 
    "SWM:Swimming", "TW:Thrown Weapon", "TVD:Tracked Vehicle Driver", 
    "WVD:Wheeled Vehicle Driver"] 

मैं बस इस सूची को अंतरिक्ष को कम करने के लिए एक साधारण, 2 कॉलम प्रारूप में आउटपुट करना चाहता हूं। आदर्श रूप से कॉलम के बीच एक मानक मात्रा होनी चाहिए लेकिन मैं इसके साथ काम कर सकता हूं।

ACM:Aircraft Mechanic  BC:Body Combat 
BIO:Biology   CBE:Combat Engineer 
CHM:Chemistry  CMP:Computers 
CRM:Combat Rifeman  CVE:Civil Engineer 
DIS:Disguise   ELC:Electronics 
EQ:Equestrian   FO:Forward Observer 
FOR:Forage   FRG:Forgery 
FRM:Farming    FSH:Fishing 
GEO:Geology    GS:Gunsmith 
HW:Heavy Weapons  IF:Indirect Fire 
INS:Instruction    INT:Interrogation 
JP:Jet Pilot   LB:Longbow 
LAP:Light Aircraft Pilot  LCG:Large Caliber Gun 
LNG:Language   LP:Lockpick 
MC:Melee Combat   MCY:Motorcycle 
MEC:Mechanic   MED:Medical 
MET:Meterology  MNE:Mining Engineer 
MTL:Metallurgy  MTN:Mountaineering 
NWH:Nuclear Warhead  PAR:Parachute 
PST:Pistol   RCN:Recon 
RWP:Rotary Wing Pilot  SBH:Small Boat Handling 
SCD:Scuba Diving  SCR:Scrounging 
SWM:Swimming  TW:Thrown Weapon 
TVD:Tracked Vehicle Driver WVD:Wheeled Vehicle Driver 
+0

क्या आप वास्तविक इनपुट मूल्य और अपेक्षित आउटपुट प्रारूप प्रदान कर सकते हैं? यह बहुत स्पष्ट नहीं है कि आपकी इनपुट सूची कैसी दिखती है। –

उत्तर

0
data = [ ("1","2"),("3","4") ] 
print "\n".join(map("\t".join,data)) 

ActiveState समाधान के रूप में के रूप में लचीला है, लेकिन कम :-)

+0

यह समस्या को हल नहीं करता है, केवल टैब वर्ण डालने से कॉलम अलग-अलग हो जाएंगे जब सामग्री अलग-अलग चौड़ाई (जैसे 'खराब' आउटपुट के ओपी के उदाहरण में) – sidewinderguy

+1

@sidewinderguy लेकिन यह छोटा है :-) –

+1

वास्तव में यह बहुत कम है :) – sidewinderguy

11

दो कॉलम नहीं, टैब द्वारा अलग, लाइनों में शामिल हो गए। अंतरिक्ष-कुशल समाधान प्राप्त करने के लिए, इटरेटर समकक्षों के लिए itertools देखें।

import string 
def fmtpairs(mylist): 
    pairs = zip(mylist[::2],mylist[1::2]) 
    return '\n'.join('\t'.join(i) for i in pairs) 

print fmtpairs(list(string.ascii_uppercase)) 

A B 
C D 
E F 
G H 
I J 
... 

ओह ... एसएलॉट द्वारा पकड़ा गया (धन्यवाद)।

एक और सामान्य समाधान, कॉलम और विषम सूचियों की संख्या को संभालता है। अंतरिक्ष बचाने के लिए जेनरेटर का उपयोग करके, S.lott से थोड़ा संशोधित किया गया।

def fmtcols(mylist, cols): 
    lines = ("\t".join(mylist[i:i+cols]) for i in xrange(0,len(mylist),cols)) 
    return '\n'.join(lines) 
+0

अफसोस की बात है, यह तत्वों की एक विषम संख्या के लिए अच्छी तरह से काम नहीं करता है। –

+0

इसका दूसरा भाग बहुत अच्छा है! – Richard

3

यह लंबी हवा वाली है, इसलिए मैं इसे दो हिस्सों में तोड़ दूंगा।

def columns(skills_defs, cols=2): 
    pairs = [ "\t".join(skills_defs[i:i+cols]) for i in range(0,len(skills_defs),cols) ] 
    return "\n".join(pairs) 

यह स्पष्ट रूप से, एक लूंग स्टेटमेंट के रूप में किया जा सकता है।

यह कौशल की एक विषम संख्या के लिए भी काम करता है।

+0

वाह, यह सही है।
'Traceback (सबसे हाल कॉल पिछले): – Richard

0

format_columns समारोह आप क्या चाहते हैं करना चाहिए:

from __future__ import generators 
try: import itertools 
except ImportError: mymap, myzip= map, zip 
else: mymap, myzip= itertools.imap, itertools.izip 

def format_columns(string_list, columns, separator=" "): 
    "Produce equal-width columns from string_list" 
    sublists= [] 

    # empty_str based on item 0 of string_list 
    try: 
     empty_str= type(string_list[0])() 
    except IndexError: # string_list is empty 
     return 

    # create a sublist for every column 
    for column in xrange(columns): 
      sublists.append(string_list[column::columns]) 

    # find maximum length of a column 
    max_sublist_len= max(mymap(len, sublists)) 

    # make all columns same length 
    for sublist in sublists: 
     if len(sublist) < max_sublist_len: 
      sublist.append(empty_str) 

    # calculate a format string for the output lines 
    format_str= separator.join(
     "%%-%ds" % max(mymap(len, sublist)) 
     for sublist in sublists) 

    for line_items in myzip(*sublists): 
     yield format_str % line_items 

if __name__ == "__main__": 
    skills_defs = ["ACM:Aircraft Mechanic", "BC:Body Combat", "BIO:Biology", 
     "CBE:Combat Engineer", "CHM:Chemistry", "CMP:Computers", 
     "CRM:Combat Rifeman", "CVE:Civil Engineer", "DIS:Disguise", 
     "ELC:Electronics","EQ:Equestrian", "FO:Forward Observer", 
     "FOR:Forage", "FRG:Forgery", "FRM:Farming", "FSH:Fishing", 
     "GEO:Geology", "GS:Gunsmith", "HW:Heavy Weapons", "IF:Indirect Fire", 
     "INS:Instruction", "INT:Interrogation", "JP:Jet Pilot", "LB:Longbow", 
     "LAP:Light Aircraft Pilot", "LCG:Large Caliber Gun", "LNG:Language", 
     "LP:Lockpick", "MC:Melee Combat", "MCY:Motorcycle", "MEC:Mechanic", 
     "MED:Medical", "MET:Meterology", "MNE:Mining Engineer", 
     "MTL:Metallurgy", "MTN:Mountaineering", "NWH:Nuclear Warhead", 
     "PAR:Parachute", "PST:Pistol", "RCN:Recon", "RWP:Rotary Wing Pilot", 
     "SBH:Small Boat Handling","SCD:Scuba Diving", "SCR:Scrounging", 
     "SWM:Swimming", "TW:Thrown Weapon", "TVD:Tracked Vehicle Driver", 
     "WVD:Wheeled Vehicle Driver"] 

    for line in format_columns(skills_defs, 2): 
     print line 

यह मानता है कि आप उपलब्ध जनरेटर के साथ एक अजगर की है।

3

यहाँ gimel, जो समान रूप से स्थान दिया गया है कॉलम प्रिंट करने देता द्वारा प्रदान समाधान का एक विस्तार रहा है।

def fmtcols(mylist, cols): 
    maxwidth = max(map(lambda x: len(x), mylist)) 
    justifyList = map(lambda x: x.ljust(maxwidth), mylist) 
    lines = (' '.join(justifyList[i:i+cols]) 
      for i in xrange(0,len(justifyList),cols)) 
    print "\n".join(lines) 

जो इस

ACM:Aircraft Mechanic BC:Body Combat
BIO:Biology CBE:Combat Engineer
CHM:Chemistry CMP:Computers
CRM:Combat Rifeman CVE:Civil Engineer
DIS:Disguise ELC:Electronics की तरह कुछ देता है ... ... `

+0

मैं जब मैं तार की मेरी सूची पर अपनी विधि का प्रयास यह त्रुटि प्राप्त प्रिंट fmtcols में फ़ाइल" tars.py ", रेखा 45, (रोजगार, 3) फ़ाइल "tars.py", रेखा 10, fmtcols में maxwidth = अधिकतम (मानचित्र (लैम्ब्डा एक्स: लेन (x), MyList)) फ़ाइल "tars.py", रेखा 10, में maxwidth = अधिकतम (मानचित्र (लैम्ब्डा एक्स: लेन (एक्स), मेरी सूची)) टाइप एरर: प्रकार 'int' के ऑब्जेक्ट में कोई लेन नहीं है() ' – YouHaveaBigEgo

0

मुझे लगता है कि इन समाधानों में से कई दो conflating कर रहे हैं अलग चीजें int ओ एक

आप करना चाहते हैं:

:

  1. एक निश्चित चौड़ाई
  2. प्रिंट एक मेज

यहां ऐसा करने के बारे में एक बहुत सरल ले है होना करने के लिए एक स्ट्रिंग के लिए मजबूर करने में सक्षम हो

import sys 

skills_defs = ["ACM:Aircraft Mechanic", "BC:Body Combat", "BIO:Biology", 
"CBE:Combat Engineer", "CHM:Chemistry", "CMP:Computers", 
"CRM:Combat Rifeman", "CVE:Civil Engineer", "DIS:Disguise", 
"ELC:Electronics","EQ:Equestrian", "FO:Forward Observer", 
"FOR:Forage", "FRG:Forgery", "FRM:Farming", "FSH:Fishing", 
"GEO:Geology", "GS:Gunsmith", "HW:Heavy Weapons", "IF:Indirect Fire", 
"INS:Instruction", "INT:Interrogation", "JP:Jet Pilot", "LB:Longbow", 
"LAP:Light Aircraft Pilot", "LCG:Large Caliber Gun", "LNG:Language", 
"LP:Lockpick", "MC:Melee Combat", "MCY:Motorcycle", "MEC:Mechanic", 
"MED:Medical", "MET:Meterology", "MNE:Mining Engineer", 
"MTL:Metallurgy", "MTN:Mountaineering", "NWH:Nuclear Warhead", 
"PAR:Parachute", "PST:Pistol", "RCN:Recon", "RWP:Rotary Wing Pilot", 
"SBH:Small Boat Handling","SCD:Scuba Diving", "SCR:Scrounging", 
"SWM:Swimming", "TW:Thrown Weapon", "TVD:Tracked Vehicle Driver", 
"WVD:Wheeled Vehicle Driver"] 

# The only thing "colform" does is return a modified version of "txt" that is 
# ensured to be exactly "width" characters long. It truncates or adds spaces 
# on the end as needed. 
def colform(txt, width): 
    if len(txt) > width: 
     txt = txt[:width] 
    elif len(txt) < width: 
     txt = txt + (" " * (width - len(txt))) 
    return txt 

# Now that you have colform you can use it to print out columns any way you wish. 
# Here's one brain-dead way to print in two columns: 
for i in xrange(len(skills_defs)): 
    sys.stdout.write(colform(skills_defs[i], 30)) 
    if i % 2 == 1: 
     sys.stdout.write('\n') 
+0

पायथन 2.7.6 का उपयोग करके परीक्षण किया गया है, मेरे लिए काम करता है :) – sidewinderguy

संबंधित मुद्दे