python equivalent to perl's qw()

前端 未结 3 582
清酒与你
清酒与你 2021-01-03 20:20

I do this a lot in Perl:

printf \"%8s %8s %8s\\n\", qw(date price ret);

However, the best I can come up with in Python is

p         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 20:53

    QW() is often used to print column headings using join() in Perl. Column heads in the real-world are sometimes long -- making join("\t", qw()) very useful because it's easier to read and helps to eliminate typos (e.g. "x","y" or "x\ty"). Below is a related approach in real-world Python:

        print("\t".join('''PubChemId Column ESImode Library.mzmed
          Library.rtmed Metabolite newID Feature.mzmed Feature.rtmed
          Count ppmDiff rtDiff'''.split()))
    

    The triple quote string is a weird thing because it doubles as a comment. In this context, however, it is a string and it frees us from having to worry about line breaks (as qw() would).

    Thanks to the previous replies for reveling this approach.

提交回复
热议问题