Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
29th July To 4th August 2019
#6
Congratulation fishhook Dance your post is this weeks winner.

(Jul-29-2019, 01:21 PM)fishhook Wrote: 1. Don't use range(len(a)), python cycle iterates over values, not indexes, just write
for item in a:
2. Your code works well
a = [100, 100, 50, 20, 100, 30]
x = 100
c = 0
for i in range(len(a)):
    if x == a[i]:
        c = c + 1

print(c)
>>> 3
3. Better code
a = [100, 100, 50, 20, 100, 30]
x = 100
c = sum(1 for i in a if i == x)

print(c)
Reply


Messages In This Thread
29th July To 4th August 2019 - by Yoriz - Jul-29-2019, 04:29 PM
RE: 29th July To 4th August 2019 - by Yoriz - Jul-29-2019, 04:40 PM
RE: 29th July To 4th August 2019 - by Yoriz - Jul-29-2019, 06:02 PM
RE: 29th July To 4th August 2019 - by Yoriz - Aug-04-2019, 09:44 AM
RE: 29th July To 4th August 2019 - by Yoriz - Aug-04-2019, 09:48 AM
RE: 29th July To 4th August 2019 - by Yoriz - Aug-05-2019, 04:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  5th August To 11th August 2019 Yoriz 2 3,362 Aug-21-2019, 04:34 PM
Last Post: nilamo
  22nd July To 28th July 2019 Yoriz 2 2,986 Jul-29-2019, 04:35 PM
Last Post: Yoriz
  15th to 21st July 2019 Yoriz 2 3,077 Jul-22-2019, 04:56 PM
Last Post: Yoriz
  8th to 14th July 2019 Yoriz 3 3,370 Jul-17-2019, 05:15 PM
Last Post: Yoriz
  Week of 7/5/2019 metulburr 8 4,987 Jul-05-2019, 09:58 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020