gpt4 book ai didi

python - 在字符串的最后一个单词之前插入 'or '

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:06 24 4
gpt4 key购买 nike

我试图在字符串的最后一个单词之前插入“或”。

我希望它看起来像:

>>>test, test1, test2, or test3?

我尝试做什么以及我得到了什么:

i = input(', {}'.format('or ' if options[_][-1:] else '').join(options[_]) + '?')  
>>>test, or test1, or test2, or test3?

我的代码:

def main():
options = {
'o1': ['1', '2', '3'],
'o2': ['one', 'two', 'three'],
'o3': ['uno', 'dos', 'tres']
}

values = []

i = ''
for _ in options.keys():
while i not in options[_]:
i = input(', '.join(options[_]) + '?')
values.append(i)

print(values)

main()

最佳答案

这样怎么样:

def list_as_string(data):
return ", ".join(data[:-1]) + ", or " + data[-1]

a = ['2', 'one', 'tres']

list_as_string(a)

输出:

'2, one, or tres'

关于python - 在字符串的最后一个单词之前插入 'or ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48959918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com