0

I am currently trying to make a program that converts the elements in a string list into a float list. Though a few adjustments have to be made.

  1. The user enters a "colon" between each typed number, which will then divide the elements in the list. (This works atm)

  2. Every value has to be outputted in 1 decimal, which I have been trying to do with:

    print(f'{word:.1f}')
    

    But haven't been able to do it correctly.

Input: 5:2:4

Output: [5.0, 2.0, 4.0]

Would appreciate some recommendations.

user_list = input("Enter values:")
word = user_list.split(":")
print(word)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Ertu
  • 53
  • 2
  • 8
  • 1
    You've got the basic idea down, now you can do a `float()` conversion, perhaps even list comprehension with it to write tighter code, and you'd have your list. Where exactly are you having your issues? – Frontear Nov 25 '21 at 22:30
  • @Frontear I struggle with converting the list to float, specifically because of the ":" is a string and can't be converted into a float in this case. Kinda stuck in that way, hopefully, I could share my issue. – Ertu Nov 25 '21 at 22:35
  • Welcome to Stack Overflow! In the future, please start with your own research. I found that question just by googling "python" plus your question title. As well, please provide a [mre], meaning remove anything that's not relevant to the problem like the `input()` and `.split()` that are working correctly already. For more tips, see [ask]. – wjandrea Nov 25 '21 at 22:54
  • @Ertu `word` doesn't contain any colons, so what do you mean exactly? (sorry, I should have asked this first, but I didn't read the comments until now) – wjandrea Nov 25 '21 at 22:57
  • @wjandrea Hello, I had checked that question before, but didn't find it helpful in my case. I also did the research regarding this, but failed in coding the program correctly. That is why I asked here on Stackoverflow. The program itself has to contain input(), that's why I included it. Split() was also added to show others the method I used in this case. If this was not relevant, then I apologize on my part. – Ertu Nov 25 '21 at 23:04
  • @ertu How didn't you find it helpful? To me, it addresses your problem directly: `[float(i) for i in word]` from the [top answer](/a/1614247/4518341) – wjandrea Nov 25 '21 at 23:18

5 Answers5

3
list(map(float, word.split(':')))

This will convert each of the number to float

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Sakshi Sharma
  • 133
  • 1
  • 8
2

You can iterate through each item in array word, convert to float, then round to one decimal using round() and append it to a new array:

user_list = input("Enter values:")
word = user_list.split(":")
newlist = []
for num in word:
    newlist.append(round(float(num), 1))
print(newlist)

Note that this is what you want if you are expecting the user to include longer decimal floats such as 3.432 in their input, and wish them to be outputted as 3.4. If you are assuming the user will only include ints in the input, Sakshi's answer is likely the most compressed solution to do it

Ryan Millares
  • 525
  • 4
  • 16
2

Assuming you have validated the inputs, a nice way to do this is through a list comprehension

input = "5:2:4"
user_list = input.split(":")

float_list = [float(x) for x in user_list]
print(float_list)
wombat
  • 614
  • 3
  • 18
2

You can pass a map function into a list function. Your word variable is already being split by ':'. Try:

list(map(float, word))
RoiMinuit
  • 404
  • 4
  • 17
1

This line:

word = user_list.split(":")

Takes a string (user_list) and splits it into parts over occurrences of :. The result is a list of strings. For example, 'alice:bob'.split(":") would become ['alice', 'bob'].

You print all of word, so you're printing a list and that's why you should be seeing ['5', '2', '4'], since that's the representation in text of the list, still in strings.

If you want to print the numbers separated by commas and spaces (for example), with a single digit of precision, you could do:

print(', '.join(f'{float(w):.1f}' for w in word))

This loops over the strings in word, turns them into floating point numbers, then formats them, collects all of them and then .join() puts them all together with ', ' in between.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Grismar
  • 27,561
  • 4
  • 31
  • 54
  • Appreciate the help and information, would it be possible if you could further explain on why you used ', ' ? – Ertu Nov 25 '21 at 23:08
  • 1
    Just as a suggestion, your question didn't make it explicit how you expect the numbers to be separated when you print them, so I decided to separate them with `', '`, so that the result looks like "5.0, 2.0, 4.0" - if you need each number on a new line, you'd separate then with `'\n'`, and you can pick anything else of course. – Grismar Nov 25 '21 at 23:18