admin管理员组

文章数量:1336201

I have an assignment that's asking me to get count without a for loop and the test environment keeps returning: Expected count to be 0, but got Params(tuple=(1, 'a', True), expected=0).

Assume tpl to be a tuple containing elements of different types (int, string, float and bool). Write some code that counts how many elements have the same type as the first element. Store the result in a variable called count. Do not use a for loop.

# Get the type of the first element

first_type = type(tpl[0])

# Use list comprehension and the sum function to count matching types

count = sum(1 for elem in tpl if type(elem) == first_type)

# Output the count

print(count)

I have an assignment that's asking me to get count without a for loop and the test environment keeps returning: Expected count to be 0, but got Params(tuple=(1, 'a', True), expected=0).

Assume tpl to be a tuple containing elements of different types (int, string, float and bool). Write some code that counts how many elements have the same type as the first element. Store the result in a variable called count. Do not use a for loop.

# Get the type of the first element

first_type = type(tpl[0])

# Use list comprehension and the sum function to count matching types

count = sum(1 for elem in tpl if type(elem) == first_type)

# Output the count

print(count)
Share Improve this question edited Nov 20, 2024 at 1:49 InSync 10.9k4 gold badges17 silver badges56 bronze badges asked Nov 19, 2024 at 20:45 BoiledbiscuitBoiledbiscuit 11 8
  • 3 It seems like the expected output is actually "how many elements have the same type as the first element not including the first". Which is unclear language, but means you should be looping over tpl[1:], instead of the whole thing. – BTables Commented Nov 19, 2024 at 20:53
  • 1 You're still using a for loop, even if it's hidden in the generator expression. Recursive might work better. – Mark Ransom Commented Nov 19, 2024 at 21:51
  • 1 It is not very clear to me whether a comprehension list (even if it uses the keyword for) counts as a for loop. It is the same keyword, but not at all the same syntax. A bit like 1 if cond else 2 is a ternary operator, not a if statement. But sure, we lack some context about what that "don't use for loop" means. It usually means "you can't try to outsmart the teacher: use whatever thing we have just learned". For anybody following the lesson it is obvious. For us... – chrslg Commented Nov 19, 2024 at 23:02
  • 1 Maybe, as some suggested, this just follows course on recursion. In which case "don't use for loop" means "use recursion". Maybe it follows a lesson on while. Maybe if follows a lesson on lambdas, in which cas something like sum(map(lambda x: type(x)==type(tpl[0]), tpl)) is maybe expected (even tho, imho, the list comprehensin is the pythonic way to do that) – chrslg Commented Nov 19, 2024 at 23:05
  • 1 That's no list comprehension but a generator expression. – no comment Commented Nov 19, 2024 at 23:09
 |  Show 3 more comments

3 Answers 3

Reset to default 1

Two more ways:

tpl = 3, 'one', 4, True, 5, 9.

a, *b = map(type, tpl)
count = b.count(a)

print(count)

count = 0
ts = map(type, tpl)
t = next(ts)
while t in ts:
    count += 1

print(count)

Attempt This Online!

Assuming tpl a tuple of mixed types, you could write a recursive method as shown below.

def loop(tpl, target=None):
    if not tpl:
        return 0
    if target is None:
        target=type(tpl[0])
    return int(type(tpl[0]) == target) + loop(tpl[1:], target)

Subtract 1 from the answer if you want to exclude the first element.

Another way to do it without slicing is to pass an index i indicating the element to test against the target. I’ll leave that as an exercise for you.

Well, I finally figured out the answer to this. I had to use a while loop and not count the first iteration of the first type. I did that by setting the index to 1.

typeFirst = type(tpl[0])

index = 1
count = 0
    
while index < len(tpl):
    if type(tpl[index]) == typeFirst:
        count += 1
    index += 1
    
print(count)

本文标签: Iterate Tuples in Python Without a for loopStack Overflow