String format printing with python3: print from unpacked array *some* of the time

岁酱吖の 提交于 2019-12-10 14:54:36

问题


In my question a few minutes ago, I asked about how to print using python's str.format printing, when the strings are stored in an array.

Then answer was clearly unpack the list, like this:

# note that I had to play with the whitespace because the {} text is 2 characters, while its replacement is always one

hex_string = r'''
            _____
           /     \
          /       \
    ,----(    {}    )----.
   /      \       /      \
  /   {}    \_____/   {}    \
  \        /     \        /
   \      /       \      /
    )----(    {}    )----(
   /      \       /      \
  /        \_____/        \
  \   {}    /     \   {}    /
   \      /       \      /
    `----(    {}    )----'
          \       /
           \_____/
'''

letters = list('1234567')

print(hex_string.format(*letters))

But if I always want the center hexagon to have printed inside the first item in the array: letters[0], how can I mix unpacking the array with keeping the 4th printed string from the first array element?

I'm open to other printing types, like f-strings.

For example:

print(hex_string.format(letters[3], letters[1], letters[2], letters[0], letters[4], letters[5], letters[6]))

So that my output actually looks like this:

            _____
           /     \
          /       \
    ,----(    4    )----.
   /      \       /      \
  /   2    \_____/   3    \
  \        /     \        /
   \      /       \      /
    )----(    1    )----(
   /      \       /      \
  /        \_____/        \
  \   5    /     \   6    /
   \      /       \      /
    `----(    7    )----'
          \       /
           \_____/

回答1:


You can try something like this with .format():

a = '123'
print('{2}, {0}, {1}'.format(*a))

which would print 3, 1, 2

With this approach, your initial hex_string will "self document" where the letters from your array will go exactly.




回答2:


If you know the required order beforehand:

letters = list('1234567')
reordering = [4,2,3,1,5,4,7]

You can apply this to the letters:

new_letters = [letters[index-1] for index in reordering]

Output:

['4', '2', '3', '1', '5', '4', '7']

Now you can create your formatted string:

print(hex_string.format(*new_letters))



回答3:


I think the simplest approach for it to change your letters to list('4231567'), which allows you to mix unpacking as well as avoids the need to move around elements in the array

hex_string = r'''
            _____
           /     \
          /       \
    ,----(    {}    )----.
   /      \       /      \
  /   {}    \_____/   {}    \
  \        /     \        /
   \      /       \      /
    )----(    {}    )----(
   /      \       /      \
  /        \_____/        \
  \   {}    /     \   {}    /
   \      /       \      /
    `----(    {}    )----'
          \       /
           \_____/
'''

letters = list('4231547')

print(hex_string.format(*letters))

The output will be


            _____
           /     \
          /       \
    ,----(    4    )----.
   /      \       /      \
  /   2    \_____/   3    \
  \        /     \        /
   \      /       \      /
    )----(    1    )----(
   /      \       /      \
  /        \_____/        \
  \   5    /     \   6    /
   \      /       \      /
    `----(    7    )----'
          \       /
           \_____/


Or expanding on the idea suggested below, use str.format with indexes defined to unpack the original string, but assigning different items at different positions

hex_string = r'''
            _____
           /     \
          /       \
    ,----(    {3}    )----.
   /      \       /      \
  /   {1}    \_____/   {2}    \
  \        /     \        /
   \      /       \      /
    )----(    {0}    )----(
   /      \       /      \
  /        \_____/        \
  \   {4}    /     \   {5}    /
   \      /       \      /
    `----(    {6}    )----'
          \       /
           \_____/
'''

letters = list('1234567')

print(hex_string.format(*letters))

This also give you a better reference point in the sense that you know that {0} will contain the first item of the list, which is 1

The output will be same as above



来源:https://stackoverflow.com/questions/56092622/string-format-printing-with-python3-print-from-unpacked-array-some-of-the-tim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!