首页
\
问答
\
用特定键列表来总结字典值的pythonic方法?(A pythonic way to sum the values of a dict with a specified key list?)
用特定键列表来总结字典值的pythonic方法?(A pythonic way to sum the values of a dict with a specified key list?)
假设我有一个列表(或其他可迭代对象)
['a','b','c','d']
。 我有一个dict
X
我可以用天真的方式总结它们:s = 0 for k in ['a','b','c','d']: s += X[k]
但是,还有更多pythonic的方式?
Let's say I have a list (or other iterable object)
['a','b','c','d']
. I have adict
X
. I can sum them up in the naive way:s = 0 for k in ['a','b','c','d']: s += X[k]
But is there a more pythonic way?
原文:https://stackoverflow.com/questions/31083137
更新时间:2022-02-15 19:02
最满意答案
如果您只是寻找一个字符和第一个位置,那么您可以使用以下代码:
#include <stdio.h> #include <string.h> int main() { int i; char text1[] = "een stuk text"; char charYouLookFor = 'k'; for ( i = 0; i < strlen(text1); i++ ) { if (text1[i] == charYouLookFor) break; } printf("%d", i); return 0; }
如果要查找文本中文本的位置或char的第二个位置,则代码需要更复杂。
if you are only looking for a char and the first position then you can use the following code:
#include <stdio.h> #include <string.h> int main() { int i; char text1[] = "een stuk text"; char charYouLookFor = 'k'; for ( i = 0; i < strlen(text1); i++ ) { if (text1[i] == charYouLookFor) break; } printf("%d", i); return 0; }
If you are looking for the position of a text in a text or for the second position of the char the code needs to be more complex.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
你应该使用keydown事件(接近你提出的建议): var editor = CKEDITOR.instances.editor1; editor.document.getBody().on('keydown', function(event) { if (event.data.getKeystroke() === 65 /*a*/ && isFirstLetter()) { // insert 'A' instead of 'a' editor.insertTex ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
这段代码就可以了。 inputString是整个文件存储为单个字符串。 另请注意,如果任何单词以非字母字符开头,则会崩溃。 此外,它假定每个单词都用空格或新行分隔。 int[] charCounts = new int[26]; //Separate the string into individual words //The string " /n" tells it to look for spaces and newline characters "/n" Strin ...
-
这可能比你想要的更冗长,但希望它能够在你阅读它时培养理解力。 public static void Main() { //Make an array of the possible sentence enders. Doing this pattern lets us easily update // the code later if it becomes necessary, or allows us easily to move this to an input ...
-
如何在一个句子中的每个元音后检测并打印第一个字母?(How to detect and print first letter after each vowel in a sentence?)[2022-02-15]
您可以使用finditer和生成的匹配对象的start方法来查找每个匹配的索引,然后使用它来获取每个元音之后的字母: import re def find_after_vowel(word): for match in re.finditer(r"[ouieaOUIEA]", word): print word[match.start()+1] find_after_vowel("school year") 哪个会输出: o l a r 如果您希望它返回列表而不是打印结 ... -
我如何在一个句子中找到某个字母的位置?(how do i find on what place a certain letter is in a sentence? (c programming))[2023-05-08]
如果您只是寻找一个字符和第一个位置,那么您可以使用以下代码: #include#include int main() { int i; char text1[] = "een stuk text"; char charYouLookFor = 'k'; for ( i = 0; i < strlen(text1); i++ ) { if (text1[i] == charYouLookFor) break; } ... -
当你用sum求和时,你将int整除,这会截断(在这种情况下为0)。 将结果分配给双精度并不重要。 为了解决这个问题,在分区之前发生了两次: box[c8].prob = ((double)box[c8].occur)/sum; When you divide occur by sum, you are dividing an int by an int, which truncates (to 0 in this case). It doesn't matter that you are assignin ...
-
您可以使用NSStringEnumerationOptions.BySentences枚举每个句子的String 。 但只有当第一个字符是大写字母时才会检测到“句子”。 所以,这可能不完美,但你可以试试这个: import Foundation let text:String = "lorem ipsum dolor elit, sed aliqfuas. imfs enim ad veniam, quis nostrud consequat? duis aute irure dolor in paria ...
-
您可以使用函数来遍历单词列表的切片: >>> def position_of_ngram(words,hyp): ... lenght=len(words) ... for i,sublist in enumerate((hyp[i:i+lenght] for i in range(len(hyp)))): ... if words==sublist: ... return i ... return None DEMO: >>> positio ...