counter

Python - keeping counter inside list comprehension

妖精的绣舞 提交于 2021-02-19 04:34:45
问题 Is it possible to write a list comprehension for the following loop? m = [] counter = 0 for i, x in enumerate(l): if x.field == 'something': counter += 1 m.append(counter / i) I do not know how to increment the counter inside the list comprehension. 回答1: You could use an itertools.count: import itertools as IT counter = IT.count(1) [next(counter)/i for i, x in enumerate(l) if x.field == 'something'] To avoid the possible ZeroDivisionError pointed out by tobias_k, you could make enumerate

Find the most common element in list of lists

一曲冷凌霜 提交于 2021-02-10 17:44:09
问题 This is my list a=[ ['a','b','a','a'], ['c','c','c','d','d','d']] I wanna find most common elemments. I have tried this from collections import Counter words = ['hello', 'hell', 'owl', 'hello', 'world', 'war', 'hello', 'war','aa','aa','aa','aa'] counter_obj = Counter(words) counter_obj.most_common() but it works just for simple list. my output should be like this [('a', 3), ('c', 3), ('d', 3), ('b', 1)] 回答1: Apply Counter().update() option on the elements of your list, Based on suggestion

Python: chemical elements counter

放肆的年华 提交于 2021-02-05 12:29:07
问题 I want to get the elements for a given mixture. For examples, for a mixsture of Air (O2 and N2) and Hexane (C6H14) given by the dict with their respectives mole numbers mix = {'O2': 1, 'N2': 3.76, 'C6H14': 0.01} I want to get the following: {O: 2, N: 7.52, C:0.06, H: 0.14} Another example: mix = {'C6H14': 1, 'C9H20': 1} must yields {H: 34, C: 15} enter code here The sequence of the dict it's not important. I was trying with the re.split, but I don't get any progress. If anyone can help me I

PHP counter increment in a while loop

你离开我真会死。 提交于 2021-02-05 12:19:17
问题 Im having a problem incrementing a counter in one of my while loops basically i just want to alternate between two image links that were fetched in my database but my counter wont increase and im not sure why can anyone help? while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $img_link = $row['Image']; $img_link_alt = $row['Image_alt']; $i = 0; echo '<div class="col-xs-6 col-sm-3 placeholder">'; $img = ( $i % 2 == 0 ) ? $img_link : $img_link_alt; echo $i; //'?' . date("h:i:sa").' echo '<img style

Why is there no symmetric difference for collections.Counter?

给你一囗甜甜゛ 提交于 2021-02-05 10:42:28
问题 So for sets you can do a symmetric difference (^) which is the equivalent of union minus intersection. Why is ^ an unsupported operand for Counter objects while union and intersection still work? 回答1: Expanding on my comment, turns out it was discussed at time, and rejected. Click the link for the full message (and its thread); I'll just quote the "high order bits" of Raymond Hettinger's reply: It's unlikely that I will add this [symmetric difference] method to the Counter API because the

How to replace text with a regex pattern and integrate a counter in the replacement text?

牧云@^-^@ 提交于 2021-02-05 10:00:18
问题 function parse($string){ $counter = 0; $string = preg_replace("_\[b\](.*?)\[/b\]_si", '<span class="b">'. $counter .'. $1</span>', $string, -1, $counter); return $string; } I'm trying to make a ubb parser, that parses tags and put the counting in front of it: [b]Hey[/b] [b]Hello[/b] Should return this: <span class="b">1. Hey</span> <span class="b">2. Hello</span> But is returning this: <span class="b">1. Hey</span> <span class="b">1. Hello</span> So beside the function above, I've tried this:

count the number of objects created by java

会有一股神秘感。 提交于 2021-02-05 07:57:06
问题 I'm trying to count the number of objects created but it always returns 1. public class Drivertwo { public static void main(String[] args) { Employee newEmp = new Employee(); Employee newEmp2 = new Employee(); Calculate newcal = new Calculate(); Clerk newclerk = new Clerk(); float x; int y; newEmp.setEmp_no(2300); newEmp.setEmp_name("W.Shane"); newEmp.setSalary(30000); newEmp.counter(); newEmp2.setEmp_no(1300); newEmp2.setEmp_name("W.Shane"); newEmp2.setSalary(50000); newEmp2.counter();

How to use scanf without stopping the program C language?

倖福魔咒の 提交于 2021-01-29 18:00:29
问题 I'm doing a counter where I'm showing how many seconds the user has to make the input. The problem is that when I use scanf(), the program will stop and will wait for the answer, but I don't want that. I'd like to keep running the counter even thought the user doesn't put anything. Example: for(int i=10;i>=0;i--){ i==0? printf("time over\n"):printf("%d seconds left\n",i); scanf("%s", decision); sleep(1); } What can i do to solve this? 回答1: Like mentioned in the comments one possibility would

Infinite counter with increment at two decimals

[亡魂溺海] 提交于 2021-01-29 12:21:02
问题 Trying to create an infinite counter that starts at 10.41 and increases by 10.41 every second. Have looked at various tutorials but I cannot find one that will account for the increase with a decimal number unit (10.41) every second. This is the code I am using: setTimeout(start, 0); var i = 100; var num = document.getElementById('number'); function start() { setInterval(increase, 100); } function increase() { if (i < 100000) { i++; num.innerText = i; } } 回答1: If I understand correctly, all

bot counting command discord

ぃ、小莉子 提交于 2021-01-29 04:56:05
问题 I have a private bot on discord, i've been trying to make him count a command and adding '+1' everytime I write that one command but it stays at 1 and can't go further : like this I think what I want to do is to make it save the number of time the command has been writen and add +1 to this number ; Should I do a loop or something ? Basically what I want is something like this in python for a discord bot : https://docs.nightbot.tv/commands/variables/count 回答1: You're resetting your counter