【问题标题】:How do I make a string split by every letter如何使字符串按每个字母拆分
【发布时间】:2021-04-14 08:05:47
【问题描述】:

我想制作一个使用 termcolor 制作文字彩虹的程序,但我不知道如何将字符串变成字母

代码:

from termcolor import colored

def rainbow(a):
    alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
              ,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

    a.split(str(alphabet))

    print(colored(a, 'red'), colored(a, 'yellow'), colored(a, 'green'), colored(a, 'blue'), colored(a, 'magenta'),
          colored(a, 'red'), colored(a, 'yellow'), colored(a, 'green'), colored(a, 'blue'))

rainbow("text")

【问题讨论】:

  • 你的问题是关于拆分的,你有几个答案,但是,在 cmets 上,你说你要的是颜色的东西。请澄清你在问什么。颜色?分裂?什么不起作用 - 粘贴错误消息或输出 - 在这种情况下,可能需要屏幕截图,而不仅仅是文本 - 您希望看到什么?欢迎登机。
  • 另外,你的a.split 没有分配给任何东西,所以基本上没有做任何事情。 colored(a...) 因此适用于您的整个 a 变量,即“文本”。从修复它开始。

标签: python string termcolor


【解决方案1】:

实际上,字符串就像一个字母列表。你可以这样做:

string="text"
for letter in string:
    print(letter)

如果你想为一个字符串中的字母着色,试试这个:

# Make sure the library is OK, I don't know it, just copy your code.
from termcolor import colored

# fill the dict for all letters yourself
lettercolors = {'a':'red','b':'blue','t':'yellow','e':'blue','x':'green'}
string="text"
for letter in string:
    print(colored(letter,lettercolors(letter)),end='')
print('');

【讨论】:

  • 谢谢,但我想改变每个字母的颜色,有什么办法吗?
  • 你的意思是固定还是随机给这26个字母上色?
【解决方案2】:
def split(word): 
  return [char for char in word]  
    
print(split('text'))
#['t', 'e', 'x', 't']

我认为你需要这样一个简单的函数。 你的问题不是很清楚。

【讨论】:

    【解决方案3】:

    使用list() 包装器:

    print(list("text"))
    

    输出:

    ['t', 'e', 'x', 't']
    

    【讨论】:

      【解决方案4】:
      import itertools
      import sys
      
      def colored(ch, color):
          # fake function as I dont have termcolor
          sys.stdout.write(f"{ch}.{color} ")
      
      def rainbow(a):
      
          colors = ['red', 'yellow','green','blue','magenta']
      
          #double it 1 2 3 => 1 2 3 2 1
          colors2 = colors + list(reversed(colors))[1:]
      
          #zip brings items of 2 lists together. cycle will just cycle through your
          #colors until a is done.  `a` itself is split merely by iterating over it 
          #with a for loop
          for ch, color in zip(a, itertools.cycle(colors2)):
              # print(f"{ch=} {color=}")
              colored(ch, color)
      
      rainbow("text is long enough to cycle")
      

      部分输出:

      t.red e.yellow x.green t.blue  .magenta i.blue s.green  .yellow l.red o.red n.yellow g.green  .blue e.magenta n.blue o.green u.yellow g.red h.red  .yellow t.green o.blue  .magenta c.blue y.green c.yellow l.red e.red 
      

      附言

      看起来colors2 = colors + list(reversed(colors))[1:-1] 可能会在循环重新开始颜色时避免将 red red 输出加倍。这可能需要一些仔细的测试。

      我偷了cycle

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-16
        • 1970-01-01
        • 1970-01-01
        • 2021-02-12
        相关资源
        最近更新 更多