Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help!
#1
This small issue took me 3 days and i stil did not fix it. I was assigned to Create a program that counts the digits(0-9) from a string as well as other characters(not defined[probably all]). Your program will output a 11character string,that will count how many numbers it came across in the input. The first character would be the number of ZEROS python read in the string,the second one will do the same job but will show how many ONES it saw and ETC. The 11th character will be how many NON numbers are inside my string. If a number exceeds the 9time you should mark it with an ASTERISK. For example. INPUT=01710 OUTPUT=22000001000
My code
user_data_list=[]
user_data=input()
while user_data!="end" and user_data!="END":
  met_data_list=[0,0,0,0,0,0,0,0,0,0,0]
  user_data_list=list(user_data)
  for i in range (len(user_data_list)):
    if user_data_list[i]=="0":   
      met_data_list[0]=met_data_list[0]+1
      
    elif user_data_list[i]=="1":
      met_data_list[1]=met_data_list[1]+1
      
    elif user_data_list[i]=="2":
     met_data_list[2]=met_data_list[2]+1
     
    elif user_data_list[i]=="3":
      met_data_list[3]=met_data_list[3]+1
      
    elif user_data_list[i]=="4":
       met_data_list[4]=met_data_list[4]+1
       
    elif user_data_list[i]=="5":
      met_data_list[5]=met_data_list[5]+1
      
    elif user_data_list[i]=="6":
      met_data_list[6]=met_data_list[6]+1
      
    elif user_data_list[i]=="7":
     met_data_list[7]=met_data_list[7]+1
     
    elif user_data_list[i]=="8":
     
     met_data_list[8]=met_data_list[8]+1
     
    elif user_data_list[i]=="9":
      
      met_data_list[9]=met_data_list[9]+1
     
    elif "0123456789" not in user_data_list[i]:
      met_data_list[10]=met_data_list[10]+1

    for i in range(len(met_data_list)):
      if met_data_list[i]>9:
       met_data_list.pop(i)
       met_data_list.insert(i,"*")
    
    

  print ("{}{}{}{}{}{}{}{}{}{}{}".format(met_data_list[0],met_data_list[1],met_data_list[2],met_data_list[3],met_data_list[4],met_data_list[5],met_data_list[6],met_data_list[7],met_data_list[8],met_data_list[9],met_data_list[10]))

  user_data=input()
I will break my head.
Reply
#2
First of all, you should loop directly over the characters in the string: for char in user_data:

Next, note that the index of met_data_list that you are updating is the same as the character you are counting. So you can get rid of the ten ifs, and make it much simpler. Check if char is a digit (isdigit() method or in '0123456789'). If it is, convert it to an int, and add one to met_data_list[that_int]. If it's not a digit, then add one to to met_data_list[10].

Then you convert it to a string. But that needs to be after the loop where you count everything. So you want to unindent that last for loop. As it is now, the second for loop is run for every character you count. Also, rather than popping and inserting, you can just assign to that index (met_data_list[i] = '*'). I would suggest redoing that loop to convert everything to strings:

text_list = []
for count in met_data_list:   # Again, loop over the list directly.
    if count < 10:
        text_list.append(str(count))
    else:
        text_list.append('*')
Then your print is much simpler: print(''.join(text_list))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
counts = [0] * 11

# for testing use following
user_data = 'hjhjhu755399952210887340123456789'
# user_data = user_data=input('Enter string of numbers and characters: ')

for x in user_data:
    if x.isdigit():
        idx = int(x)
        counts[idx] += 1
    else:
        counts[10] += 1
print(counts)
result:
Output:
[2, 2, 3, 3, 2, 4, 1, 3, 3, 4, 6]
create comment on current user_data and remove on input to input data.
you can simplify more with list comprehension
Reply
#4
(Oct-30-2018, 07:47 PM)Larz60+ Wrote:
counts = [0] * 11

# for testing use following
user_data = 'hjhjhu755399952210887340123456789'
# user_data = user_data=input('Enter string of numbers and characters: ')

for x in user_data:
    if x.isdigit():
        idx = int(x)
        counts[idx] += 1
    else:
        counts[10] += 1
print(counts)
result:
Output:
[2, 2, 3, 3, 2, 4, 1, 3, 3, 4, 6]
create comment on current user_data and remove on input to input data.
you can simplify more with list comprehension

Thanks everybody for the help. I am studying your answers right now. Can you please explain what does
 
counts = [0] * 11
 ###AND BELOW!
if x.isdigit():
        idx = int(x)
        counts[idx] += 1
    else:
        counts[10] += 1
I am not used to this syntax and i keep missing your point! Thank you both!
Reply
#5
(Oct-30-2018, 07:45 PM)ichabod801 Wrote: First of all, you should loop directly over the characters in the string: for char in user_data:

Next, note that the index of met_data_list that you are updating is the same as the character you are counting. So you can get rid of the ten ifs, and make it much simpler. Check if char is a digit (isdigit() method or in '0123456789'). If it is, convert it to an int, and add one to met_data_list[that_int]. If it's not a digit, then add one to to met_data_list[10].

Then you convert it to a string. But that needs to be after the loop where you count everything. So you want to unindent that last for loop. As it is now, the second for loop is run for every character you count. Also, rather than popping and inserting, you can just assign to that index (met_data_list[i] = '*'). I would suggest redoing that loop to convert everything to strings:

text_list = []
for count in met_data_list:   # Again, loop over the list directly.
    if count < 10:
        text_list.append(str(count))
    else:
        text_list.append('*')
Then your print is much simpler: print(''.join(text_list))
I got your point up until a certain level. I got more confused then. Can you tell me where will i adjust this code? Basically the logical steps,and i will most likely solve the puzzle. The whole story is the INT and the str errors. Thats what i dont understand
Reply
#6
The first paragraph goes in line 6. The second paragraph goes in lines 7-40. The third paragraph and the code snippet go in line 42 to 45. The last paragraph goes on line 49.

You need to try this stuff out. Take what Larz and I have shown you, implement it to the best of your understanding, show us your revised code, and tell us exactly how it's not working.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
DONE! Thank you all! I will keep studying it till i figure it our completely

text_list = []
user_data=input()
while user_data!="end" and user_data!="END":
  met_data_list=[0,0,0,0,0,0,0,0,0,0,0]

  for char in user_data:
    if char.isdigit():
        idchar=int(char)
        met_data_list[idchar] += 1
    else:
        met_data_list[10] += 1

  for i in range(len(met_data_list)):
    if met_data_list[i]>9:
     met_data_list.pop(i)
     met_data_list.insert(i,"*")
       
    
    

  print ("{}{}{}{}{}{}{}{}{}{}{}".format(met_data_list[0],met_data_list[1],met_data_list[2],met_data_list[3],met_data_list[4],met_data_list[5],met_data_list[6],met_data_list[7],met_data_list[8],met_data_list[9],met_data_list[10]))
  #print(''.join(text_list))
  user_data=input()
Reply
#8
FYI
met_data_list = [0] * 11
is the same as:
met_data_list=[0,0,0,0,0,0,0,0,0,0,0]
Also, if you are using python 3.6 or newer, you chan change:
print ("{}{}{}{}{}{}{}{}{}{}{}".format(met_data_list[0],met_data_list[1],met_data_list[2],met_data_list[3],met_data_list[4],met_data_list[5],met_data_list[6],met_data_list[7],met_data_list[8],met_data_list[9],met_data_list[10]))
to:
for item in met_data_list:
    print(f'{item} ', end = ''
print()
Reply
#9
In any case, you can change:

print ("{}{}{}{}{}{}{}{}{}{}{}".format(met_data_list[0],met_data_list[1],met_data_list[2],met_data_list[3],met_data_list[4],met_data_list[5],met_data_list[6],met_data_list[7],met_data_list[8],met_data_list[9],met_data_list[10]))
to:

print('{}{}{}{}{}{}{}{}{}{}{}'.format(*met_data_list))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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