Count Value with smaller than and Endswith condition

python split
python startswith
python 3 string methods
python endswith
python bytes
python types
python strip
python string methods

Problem: Find unitcode that endswith '1001' in the .txt file and count this unit if anyone getting marks below 50. If yes, count. If no, give a value of 0

My code:

skipped_header = True
counters_unit_code = {}
with open("inp.txt") as csv_file:
  for line in csv_file:
    if skipped_header:
      skipped_header = False
      continue
    record = line.rstrip().split(':')
    unit_code = record[2].endswith('1001')
    min_mark = int(record[4])
    if unit_code not in counters_unit_code:
      counters_unit_code[unit_code]=0
    if min_mark < 50:
      counters_unit_code[unit_code] += 1

for unit_code in counters_unit_code:
  print(str(unit_code),counters_unit_code[unit_code])

My output:

False 1
True 3

Expected output:

engl1001 3
math1001 0

txt file (inp.txt):

Name:unikey:unitcode:year:mark
Joe Smith:jsmi3031:chem1101:2016:40
Oleg Catem:ocat3031:chem1101:2016:79
Joe Smith:jsmi3031:engl1001:2015:0
Joe Smith:jsmi3031:engl1001:2016:45
Tim Gold:tgol2145:engl1001:2016:46
Paula Dong:pdon1234:engl1001:2016:91
Joe Smith:jsmi3031:engl1001:2017:54
Oleg Catem:ocat3031:engl1001:2017:87
Oleg Catem:ocat3031:math1001:2016:95
Joe Smith:jsmi3031:math1001:2016:59
Paula Dong:pdon1234:math1001:2016:99

unit_code = record[2].endswith('1001') This line gives you True or False and not substring.

for substring use

if record[2].endswith('1001'):
   unit_code = record[2][:-4]

Count cells that doesn't end with 1?, will count all non empty cells in the conditions of your question. Don't forget to press Ctrl+Shift+Enter Array formula for values less than 1000: In above formula, B3:B8 is the range you will count cells from, E2 and “>35” are the criteria you want to count cells based on. The press Enter key to get the counted result. Tip: If you want to count cells greater than or equal to a particular value, you can use this generic formula:


The problem is the line

unit_code = record[2].endswith('1001')

The .endswith() function returns a boolean value, which is why your output contains "True" and "False" instead of the unit codes. I think you meant to extract the unit code, and test whether it ends with '1001'. Something more like this:

skipped_header = True
counters_unit_code = {}
with open("inp.txt") as csv_file:
  for line in csv_file:
    if skipped_header:
      skipped_header = False
      continue
    record = line.rstrip().split(':')
    unit_code = record[2]
      if unit_code.endswith('1001'):
        min_mark = int(record[4])
        if unit_code not in counters_unit_code:
          counters_unit_code[unit_code] = 0
        if min_mark < 50:
          counters_unit_code[unit_code] += 1

for unit_code in counters_unit_code:
  print(str(unit_code), counters_unit_code[unit_code])

Ivor Horton's Beginning Visual C++ 2013, The element used to store a particular value is determined by count, which is 0 for The character entered is read into indicator and tested in the loop condition. 'y' or 'Y' is entered and count is less than the specified maximum value, MAX. the program ends with a suitable message because two odometer values are  For example, to count cells in the range D2:D9 with values greater than a value in cell D3, you use this formula =COUNTIF(D2:D9,">"&D3): If you want to count cells that contain an actual operator as part of the cell's contents, i.e. the characters ">", "<" or "=", then use a wildcard character with the operator in the criteria.


skipped_header = True
counters_unit_code = {}
with open("inp.txt") as csv_file:
  for line in csv_file:
    if skipped_header:
      skipped_header = False
      continue
    record = line.rstrip().split(':')
    unit_code = record[2]
    min_mark = int(record[4])
    if unicode.endswith('1001'):
      if unit_code not in counters_unit_code:
        counters_unit_code[unit_code]=0
      if min_mark < 50:
        counters_unit_code[unit_code] += 1

for unit_code in counters_unit_code:
  print(str(unit_code),counters_unit_code[unit_code])

Principles of Program Design: Problem-Solving with JavaScript, The body of the loop follows, and the loop ends with the keywords End For. Notice that the ending value is included in the count (as though to say “while the loop variable is less than or equal to the ending value”). A For loop that prints “​Happy Birthday! rights restrictions require it. Conditions, Counters, and Sentinel Values. In the Select Specific Cells dialog, (1) Check Cell option under Select type section, (2) Then select Greater than in first drop down list and type the number criterion in the next to box, and select Less than from second drop down list and type the number into beside box, (3) And check And option. 3.


I think you meant your expected value for eng1001 is 3 right?

The problem was your line

unit_code = record[2].endswith('1001')

endswith returns a boolean-value

This should do, what you are trying to achieve:

skipped_header = True
counters_unit_code = {}
unit_code = 0
with open("inp.txt") as csv_file:
    for line in csv_file:
        if skipped_header:
            skipped_header = False
            continue
        record = line.rstrip().split(':')
        if record[2].endswith('1001'):
            unit_code = record[2]
        min_mark = int(record[4])
        if unit_code not in counters_unit_code:
            counters_unit_code[unit_code] = 0
        if min_mark < 50:
            counters_unit_code[unit_code] += 1

for unit_code in counters_unit_code:
    print(str(unit_code), counters_unit_code[unit_code])

Python, The return value is binary. The functions returns True if the original Sentence starts with the search_string else False. endswith(). Syntax : str.endswith(  To get the 2nd smallest value, 3rd smallest value, 4th smallest value, and so on, where each value matches supplied criteria, you can use an array formula that uses the SMALL and IF functions. In the example shown, the formula in G7 is: {=


I would use re instead of basing it on :s

import re

pattern = r"(\w{4}1001):\d{4}:(\d+)"

with open("inp.txt") as csv_file:
    values = csv_file.read()
    subjects = {}
    for match in re.finditer(pattern, values):
        subject, score = match.groups()
        scores = subjects.setdefault(subject, [])
        if int(score) < 50:
            scores.append(score)

    for subject, scores in subjects.items():
        print("{}: {}".format(subject, len(scores)))

# Output
engl1001: 3
math1001: 0

5. Built-in Types, Any object can be tested for truth value, for use in an if or while condition or as Negative shift counts are illegal and cause a ValueError to be raised. Values of n less than 0 are treated as 0 (which yields an empty sequence of the same type Return True if the string ends with the specified suffix, otherwise return False  To count the number of cells that contain values less than a particular number, you can use the COUNTIF function. In the generic form of the formula (above) rng represents a range of cells that contain numbers, and X represents the threshold below which you want to count. In the example shown, G5 contains this formula:


Built-in Types, Any object can be tested for truth value, for use in an if or while condition or as operand not has a lower priority than non-Boolean operators, so not a == b is interpreted Negative shift counts are illegal and cause a ValueError to be raised. The following article demonstrates how to construct a formula that counts unique distinct values based on a condition. The image below shows a table in column B and C. Column B contains names and column C contains products.


Operators available for filters and queries, The system provides a set of operators for use with filters, condition builders, and encoded queries. ends with, [Short description][ends with][outage] Network storage unavailable, All records in which the value for the Short less than the number in the Reopen count field; equal to the number in the Reopen count field​  The second formula returns the count of numbers that are greater than the upper bound value (10 in this case). The difference between the first and second number is the result you are looking for. =COUNTIF(C2:C10,">5")-COUNTIF(C2:C10,">=10") - counts how many numbers greater than 5 and less than 10 are in the range C2:C10. This formula will


Use expressions with conditions., Use expressions in conditions to check multiple values and returns true if the first argument is less than or equal to the second argument. I have to count all the values in a matrix (2-d array) that are greater than 200. The code I wrote down for this is: za=0 p31 = numpy.asarray(o31) for i in range(o31.size[0]): for j in