在已分配给新值Python的文本中查找字符串列表

问题描述 投票:0回答:1

我是python的新手(我从上周开始学习)。我正在尝试编写执行以下操作的python脚本:1-插入文字,例如“ Hello world!” (或文件中的较大文本)2-创建所有英语字母(或任何其他语言的字母)的列表,其中每个字母都分配给新的字符串值,例如,字母k ='cv-'。3-然后写一个语句:如果文本中存在'h',则打印出它分配的新字符串(从步骤2开始)。4-最终输出可能是这样的:cv-cvv-cv(对于文本中的每个单词)。

总而言之,我有'a'(文本),'b'(字母列表),'c'(每个字母的新值),'d'(代码,如果有陈述或类似内容) ),“ e”,输出(类似于cv-cvc-cv ..ete)。

运行a,然后使用通过b和c的d给出e。

这是我的尝试(当然没有运气)。我将不胜感激。

text = 'a test'
letters = ['a', 't', 'e', 's']

if letters in text:
 print (a_val, t_val, e_val, s_val)

python
1个回答
0
投票
    import string
    letters = list(string.ascii_lowercase)
    print(letters)
    output: ['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']
    text = "hope this will help"
    splited_text = text.split(" ")
    print(splited_text)
    output: ['hope', 'this', 'will', 'help']
    your_dictionary = {} # use dictionary, it suits your problem

    for i in range(len(splited_text)):
        your_dictionary[letters[i]] = splited_text[i]

    print(your_dictionary)
    output: {'d': 'help', 'c': 'will', 'a': 'hope', 'b': 'this'}

您可以根据需要对字典进行排序。

    For k in your_dictionary.keys():
        If(your_dictionary[k] is in some_values):
            #do something
© www.soinside.com 2019 - 2024. All rights reserved.