( 1)首先,我意识到集合和字典没有顺序,这个问题使我感到困惑,我正在努力解决这个问题,你可以从下面的努力中看出如何解决问题。2)我知道问题看上去很差
摘要:我的困难在于,这个问题要求我按一定的顺序制作一本有价值的字典,这样我就能得到特定值的答案。我可以不按它要求的顺序去做,除非我误解了。
这是我所指的课程问题,我尝试了一些根据所需答案未能成功的问题:
(如果您不想检查链接,我从代码中得到的答案如下:
{' ': 8, 'e': 8, 'i': 5, 'a': 4, 't': 4, 'l': 3, 'u': 3, 'h': 2, 'n': 2, 's': 2, 'r': 2, 'J': 1, 'c': 1, 'b': 1, 'd': 1, 'g': 1, 'f': 1, 'k': 1, 'm': 1, 'o': 1, 'q': 1, 'p': 1, 'w': 1, 'v': 1, 'y': 1, 'x': 1, 'z': 1})英文字母表的小写字母和大写字母应作为字符串变量字母表存储。
考虑一下“吉姆很快意识到漂亮的礼服是昂贵的”这句话。创建一个字典count_letters,其键由句子中的每个唯一字母组成,值由每个字母在这个句子中使用的次数组成。在字典中分别计算大写字母和小写字母。
这本词典的第三个价值是多少?提示:请记住Python是零索引的!
我的答案1,正确(YAY真的击中了多巴胺)
这本词典的第八价值是多少?提示:请记住Python是零索引的!
3正确(YAY我认为..。为什么这是经过大量的尝试之后,我的答案,这是没有意义的)
请注意,我意外地得到了第二个答案正确,并不知道为什么。
显然,字典中键的顺序必须是{J :value,i: value}我试过了计数器,如果您从我最初的尝试中看到没有提供所需答案的链接,我尝试了set(句子)和set(字母表),它提供了以下‘顺序’:{'J','a','b','c','d','e','f','g','h','i','k','l',M‘,'n','o','p','q','r','s','t','u','v','w','x','y','z'}
几天来,我不停地尝试了几个小时,但没有结果,当我几页前看到一个堆栈溢出的答案时,我充满了希望,下面的代码给了我希望.它确实在命令行中工作过一次(J,i,m.)但由于某种原因,它又回到贾布德.)
我也觉得这很有用,而且很短,但我仍然有一个问题,那就是“规则”和以前一样:
a = sentence
b = dict.fromkeys(a[i], 0)
for i in a[i]:
b[i] += 1Counting the occurrence of unique letters in a string in Python 3
这里还有更多的代码。
正如你所看到的,我正试图强制某种秩序,我认为这是不可能的关键和价值的问题和答案是快乐的。
这里有更多的代码:在我混乱的丛林中。
import string
alphabet = string.ascii_letters
alphabet
sentence = 'Jim quickly realized that the beautiful gowns are expensive'Python maintain order in intersection of lists
这使得列表与句子的顺序相同。我使用A作为字母表,B作为名单万岁!谢谢Kasramvd,但遗憾的是,当我调整订单时,它只在命令行中起作用(一次)。但给了我继续前进的希望。如您所见,我已经注释掉了代码,以防我想使用它。我使用的是python3冠层界面。
a = sentence
b = dict.fromkeys(a[i], 0)
for i in a[i]:
b[i] += 1
#A = set(alphabet)
#A = list(A)
#B = set(sentence)
#B = list(B)
#maybe = set(B)&set(A)
#letter_set = set(A) & set(B)
letter_list = list(letter_set)
common = sorted(set(A).intersection(set(B)) ,key=lambda x:B.index(x))
letter_list = common #it is already a list
count_letters = {}
cnt_lowercase = 0
cnt_uppercase = 0
count = 0
#iterate over the letter_list
for i in range (len(letter_list)-1):
#count matches for each letter of the sentence
for j in range(len(sentence)-1):
gr = letter_list[i] in sentence[j]
print i,j
print("sentence letter : " , letter_list[i], "sentence letter: " + sentence[j])
print gr
if gr == True:
count +=1;
if j == len(sentence)-1:
print("count= " , count)
count_letters[i] = count
print(count_letters)
if j > len(sentence):
print("reached the last letter of the sentence: " , sentence[j])
count = 0
print j
print ("length of the sentence is : " , len(sentence))
print count_letters
print letter_list
common = sorted(set(A).intersection(set(B)) ,key=lambda x:B.index(x)) 所以这基本上仍然是set(A) & set(B)
有什么主意吗??我太固执了,不能让这件事过去,在这之后,我只会把它变成一个函数,这样我就可以回答下一个问题,因为这是一个大得多的字符串超过5行所必需的。如果我再被困住的话我可能会回来的。
希望有足够的信息。
发布于 2020-06-19 21:58:36
我认为最简单的方法是使用计数器函数,然后按如下事实排序:
from collections import Counter
sentence = 'Jim quickly realized that the beautiful gowns are expensive'
counts = Counter(sentence)
out = dict(sorted(counts.items()))产出:
{' ': 8, 'J': 1, 'a': 4, 'b': 1, 'c': 1, 'd': 1, 'e': 8, 'f': 1, 'g': 1, 'h': 2, 'i': 5, 'k': 1, 'l': 3, 'm': 1, 'n': 2, 'o': 1, 'p': 1, 'q': 1, 'r': 2, 's': 2, 't': 4, 'u': 3, 'v': 1, 'w': 1, 'x': 1, 'y': 1, 'z': 1}我认为这实现了你想要的,但正如评论中提到的,我们看不到实际的问题。如果这有用,或者你需要进一步的帮助,请告诉我。
发布于 2020-06-20 07:51:45
字典是以键:值对的形式无序的项集合。我所做的问题是:
d={}
s="Jim quickly realized that the beautiful gowns are expensive"
for i in s:
d[i]=s.count(i)
print(d)我创建了一个空字典d,并将该字符串存储在变量的中。然后,我使用了for循环,将字符串的的每个元素都放在i中,然后通过为键:dict_name[key]=value赋值来添加键值对。这些值是由string方法string_var.count(element)添加的:它计算一个元素在string_var中发生的次数。通过这种方式,所有的键:值对被添加到空字典中。
这将打印所需的输出:
{'J': 1, 'i': 5, 'm': 1, ' ': 8, 'q': 1, 'u': 3, 'c': 1, 'k': 1, 'l': 3, 'y': 1, 'r': 2, 'e': 8, 'a': 4, 'z': 1, 'd': 1, 't': 4, 'h': 2, 'b': 1, 'f': 1, 'g': 1, 'o': 1, 'w': 1, 'n': 2, 's': 2, 'x': 1, 'p': 1, 'v': 1}希望能帮上忙!
发布于 2020-11-12 01:06:16
我认为正确的代码是:
s="Jim quickly realized that the beautiful gowns are expensive"
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
print(dict([[i, s.count(i)] for i in s if i in alphabet]))因此,在没有空白的情况下,字典的第3值是1,第8的值是3。产出如下:
{
'J': 1, 'i': 5, 'm': 1, 'q': 1, 'u': 3, 'c': 1, 'k': 1, 'l': 3,
'y': 1, 'r': 2, 'e': 8, 'a': 4, 'z': 1, 'd': 1, 't': 4, 'h': 2,
'b': 1, 'f': 1, 'g': 1, 'o': 1, 'w': 1, 'n': 2, 's': 2, 'x': 1,
'p': 1, 'v': 1
}https://stackoverflow.com/questions/62478832
复制相似问题