如何在字典中使用列表?

2024-04-24 10:35:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图编写一个程序,给我一个给定字符串中所有元音的count()和索引,并将其存储在字典中。到目前为止,这就是我所拥有的:

txt = "Text sample for vowel counting and indexing"

def ProcText(char,texto):
   lst_count=[]
   char_count = texto.count(str(char))
   lst_count.append(char_count)
   print(lst_count)

   lst_ind=[]
   char_ind = [i for (i,x) in enumerate(texto) if x == str(char)]
   lst_ind.append(char_ind)
   print(lst_ind)

ProcText("a",txt)
[2]
[[6, 31]]

所以在字典里,我希望每个元音都有这样的内容:

{'a count' : 2 , 'a index' : 6,31}

Python是否允许这种情况发生?使用Python 3.6


Tags: 程序txtfor字典countprint元音ind
3条回答

您可以使用以下两个步骤来完成列表理解:

indexes = [(v,[ i for i,c in enumerate(txt) if c == v]) for v in "aeiou" ]
vDict = dict( [("%s index"%v,a) for v,a in indexes] + [("%s count"%v,len(a)) for v,a in indexes] )

# dict == {'a index': [6, 31], 'e index': [1, 10, 19, 38], 'i index': [27, 35, 40], 'o index': [13, 17, 23], 'u index': [24], 'a count': 2, 'e count': 4, 'i count': 3, 'o count': 3, 'u count': 1}

但我相信你会发现,为你的字典选择键可能很难使用以后。我建议用字母作为键,索引列表作为值的字典。对于任何给定的字母,使用索引列表中的len()可以很容易地获得计数。你知道吗

vDict = { v:[ i for i,c in enumerate(txt) if c == v] for v in "aeiou" }

# vDict = {'a': [6, 31], 'e': [1, 10, 19, 38], 'i': [27, 35, 40], 'o': [13, 17, 23], 'u': [24]}

这样,即使字符串不包含任何元音,字典中的每个元音都会有一个条目(即索引的空列表)

如果只需要字符串中实际存在的字母的键,可以执行以下操作:

cDict = { k:[ i for i,c in enumerate(txt) if c == k] for k in set(txt) }

这将给你在字典中只为字母的实际存在条目。然后可以根据需要过滤元音(或辅音)上的键。你知道吗

感谢您的回复,但我能够找到一个解决办法,我将在这里分享,希望它能帮助其他人在未来。你知道吗

目的是计算一个字符串中的所有元音(大写和小写没有区别),并说出这些元音的索引。所有这些信息都必须存储在字典里。你知道吗

所以我定义了一个函数来计算一个字符串中的所有元音:

def ProcVogalCount(texto):
texto = texto.lower()
vogal = ["a","e","i","o","u"]
lst_count = []
for i in vogal:
    c = texto.count(i)
    lst_count.append(c)
return(lst_count)

后跟另一个函数,该函数指定每个元音的索引:

def ProcVogalInd(texto):
    texto = texto.lower()
    vogal = ["a","e","i","o","u"]
    lst_ind = []
    for i in vogal:
        indices = [a for (a,b) in enumerate(texto) if b == i]
        lst_ind.append(indices)
    return(lst_ind)

最后,我创建了最后一个函数,它接受其他两个函数返回的列表,并将所有内容压缩到两个单独的字典中:

def DicionarioVogais(texto):
    lst1 = ProcVogalCount(texto)
    keys1 = ['A cont:','E cont:','I cont:','O cont:','U cont:']
    dic1 = dict(zip(keys1,lst1))
    lst2 = ProcVogalInd(texto)
    keys2 = ['A ind:','E ind:','I ind:','O ind:','U ind:']
    dic2 = dict(zip(keys2,lst2))
    print(dic1)
    print(dic2)

因此,对字符串调用最后一个函数的最终结果如下:

{'A cont:': 6, 'E cont:': 13, 'I cont:': 5, 'O cont:': 4, 'U cont:': 3} {'A ind:': [0, 30, 35, 38, 44, 76], 'E ind:': [6, 9, 12, 18, 20, 24, 48, 53, 56, 58, 63, 70, 73], 'I ind:': [3, 45, 50, 65, 68], 'O ind:': [15, 27, 42, 77], 'U ind:': [2, 34, 62]}

除了已经收到的注释之外:count方法仍然会遍历您的句子,但是您可以用一种更简单的方法来做同样的事情,因为索引的数量会给出出现的次数。关于:

txt = "Text sample for vowel counting and indexing"
vowel = "a"
vowel_dict = {}

def proc_text(char,texto):
   char_ind = [i for (i,x) in enumerate(texto) if x == char]
   return (len(char_ind), char_ind)

result = (proc_text(vowel,txt))

vowel_dict[str(vowel) + " count"] = result[0]
vowel_dict[str(vowel) + " indices"] = result[1]

print(my_dict)

因为可以使用Python返回元组,所以可以返回多个对象。我相信您可以重构它,以更简洁的方式实现您想要的,正如abarnert所建议的那样。你知道吗

相关问题 更多 >