我如何将水平列表列入垂直列表

发布于 2025-02-13 18:06:46 字数 596 浏览 1 评论 0原文

我将如何做到这一点,以便我的元音计数器返回垂直列表,而不是水平列表。每次我使用\ n时,它只是给我一个错误,我不知道自己做错了什么,我是初学者编码员,仍然有问题解决这个问题。我尝试寻找答案,但没有找到任何答案。有人可以帮忙吗?因此,与其看起来像这样{'a':1,'e':1,'i':2,'o':1,'u':1}。

我如何使它看起来像这样垂直

'a': 1 
'e': 1 
'i': 2 
'o': 1 
'u': 1
def count_vowels(string, vowels):
 string = string.casefold()
 count = {}.fromkeys(vowels, 0)

# To count the vowels
 for character in string:
     if character in count:
         count[character] += 1
 return count
vowels = ("a", "e", "i", "o", "u")
string = "Counting all the strings"
print(count_vowels(string, vowels))

How would I make it so that my vowel counter returns a vertical list instead of a horizontal list. Every time I use \n it just gives me an error I have no clue what I am doing wrong, I am a beginner coder and still have problems solving this. I have tried looking for an answer and I haven't found any. Could anybody help? So instead of looking like this {'a': 1, 'e': 1, 'i': 2, 'o': 1, 'u': 1}.

How do I make it look vertical like this

'a': 1 
'e': 1 
'i': 2 
'o': 1 
'u': 1
def count_vowels(string, vowels):
 string = string.casefold()
 count = {}.fromkeys(vowels, 0)

# To count the vowels
 for character in string:
     if character in count:
         count[character] += 1
 return count
vowels = ("a", "e", "i", "o", "u")
string = "Counting all the strings"
print(count_vowels(string, vowels))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作妖 2025-02-20 18:06:46

如果您想要Python的默认格式以外的其他内容,那么您必须自己做:

vowels = ("a", "e", "i", "o", "u")
string = "Counting all the strings"
for k,v in count_vowels(string, vowels).items():
    print( f"{k}: {v}" )

If you want something other than Python's default formatting, then you have to do it yourself:

vowels = ("a", "e", "i", "o", "u")
string = "Counting all the strings"
for k,v in count_vowels(string, vowels).items():
    print( f"{k}: {v}" )
挽清梦 2025-02-20 18:06:46

通过将字典施放到字符串和切片(或剥离)中,支架应起作用:

...
string = "CoUnting All the strings"
counter = count_vowels(string)

print(*str(counter).strip('{}').split(', '), sep='\n')
#'a': 1
#'e': 1
#'i': 2
#'o': 1
#'u': 1

By casting the dictionary to string and slice (or strip) the brackets should work:

...
string = "CoUnting All the strings"
counter = count_vowels(string)

print(*str(counter).strip('{}').split(', '), sep='\n')
#'a': 1
#'e': 1
#'i': 2
#'o': 1
#'u': 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文