0

So I wanna try to separate some lines of user input using space, that input can only handle int, I created a variable that removed the same number from the splitted user input

for item in separated:
  print(item)
  loopCount = noOccur.count(item)
  if loopCount == 0:
    noOccur += item

Some weird thing happened if I inputted a number that's more than 10 for example

userIn = input('Numbers:   ') # 0 8 89

but that function separated the last number into ['0', '8', '8', '9']

that function worked in single digits but it doesn't work in double digits

userIn = input('Please enter your numbers:  ') # 689 688 3405
separated = userIn.split()
noOccur = []

for item in separated:
  print(item)
  loopCount = noOccur.count(item)
  if loopCount == 0:
    noOccur += item

length = len(noOccur)
i = 0
print(noOccur) # ["6", "8", "9", "6", "8", "8", "3", "4", "0", "5"]
print(separated)
while i < length:
  tempVar = noOccur[i]
  print(str(tempVar) + ": " + str(separated.count(tempVar)))
  i += 1

I think my for loop is a little bit broken, because I tried split(" ") as mentioned in the answer but it still added it individually

11
  • 2
    How do you go from userIn to separated? Commented May 5, 2021 at 5:26
  • Could you add all the code here. Commented May 5, 2021 at 5:27
  • Are you tryna get a list of nums from the user? Commented May 5, 2021 at 5:27
  • And if you want to take something like "9 89 9 8" and change that to a list just do a x.split(" ") on it. Edit: this answer would change the input to accept strings and if the user and then you can check if they are all integers. Commented May 5, 2021 at 5:28
  • Try userIn = input('Numbers: ').split() Commented May 5, 2021 at 5:29

3 Answers 3

2

I checked the update of your function and if you don't have any restriction the best will be to use a dictionnary:

userIn = input('Numbers:   ')


separated=userIn.split()
print(separated)

noOccur = {}

for item in separated:
  if item in noOccur.keys():
      noOccur[item]+=1
  else:
      noOccur[item]=1

for k,v in noOccur.items():
    print(str(k) + ": " + str(v))

Result:

Numbers:   1 2 3 100 1 2
['1', '2', '3', '100', '1', '2']
1: 2
2: 2
3: 1
100: 1
Sign up to request clarification or add additional context in comments.

2 Comments

I think my for loop is broken, I tried your solution, it still doesn't work
I dont get it. Why does this not work for you? print(itertools.Counter(list(map, input().split()))) ??
2

Use collections.Counter, to count the number of occurences of a hashable element (like a string or a number) in an iterable. You can use a Counter object just like a dict for further processing.

from collections import Counter

userIn = input('Numbers:   ') # 689 688 3405 688
separated = userIn.split() # ['689', '688', '3405', '688']

noOccur = Counter(separated) # Counter({'689': 1, '688': 2, '3405': 1})

for k,v in noOccur.items():
    print(f'{k}: {v}')
# '689': 1
# '688': 2
# '3405': 1

Comments

0

You can try with append to the noOccur list like this:

userIn = input('Please enter your numbers:  ') # 689 688 3405
separated = userIn.split(' ')
noOccur = []

for i in separated:
    loopCount = noOccur.count(i)
    if loopCount == 0:
        noOccur.append(i)

print(noOccur)

length = len(noOccur)
items = 0

while items < length:
    tempVar = noOccur[items]
    print(str(tempVar) + ": " + str(separated.count(tempVar)))
    items += 1

Result:

Please enter your numbers:  689 688 3405
['689', '688', '3405']
688: 1
689: 1
3405: 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.