简体   繁体   English

在Python 3.x中,如何替换列表中的某些项目,然后将列表打印为字符串

[英]In Python 3.x, how do I replace certain items in a list then print list as a string

I'm making a small word game that makes gibberish out of a word. 我正在做一个小型的文字游戏,使一个单词变得乱七八糟。 User gives a syllable and a word (example: ug and astronaut) and the program replaces all vowels with the syllable, unless it's a double vowel. 用户给出一个音节和一个单词(例如:ug和宇航员),程序会用该音节替换所有元音,除非它是双元音。 Astronaut becomes Astugonugaut. 宇航员成为Astugonugaut。 If the syllable was ib and the word was demonstrate, the product would be dibemibonstribate. 如果该音节是ib且该单词被证明是单词,则该产品将是dibemibonstribate。

I know that I need to have some sort of loop that runs through each letter in the word and determines if it's a vowel, double vowel, first letter, yada yada and work with that. 我知道我需要某种循环,遍历单词中的每个字母,并确定它是元音,双元音,第一个字母,yada yada并使用它。 I can't seem to figure out how to do that though. 我似乎无法弄清楚该怎么做。

I have something like this 我有这样的东西

def gibberizer(word):
    for char in range(len(word)):
        print(char)
    word.insert(4,gib1)
    print(word)

the insert line will insert the syllable where it should be, but I can't figure out how to have it do it automatically. 插入行会将音节插入应有的位置,但我不知道如何让它自动完成。

I also don't know how to get it to print the list as a string. 我也不知道如何获取列表以字符串形式打印。 I tried using .join, but I'm a bit of an idiot and probably did it wrong. 我尝试使用.join,但是我有点白痴,可能是做错了。

You can use the replace method for strings and if conditions may be useful. if条件可能有用, if可以对字符串使用replace方法。

For join: ", ".join(aList) or whatever delimiter you want this will print the items in the list as comma delimited. 对于join: ", ".join(aList) 。join ", ".join(aList)或您想要的任何定界符,它将列表中的项目以逗号分隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM