简体   繁体   English

如何将字符串列表转换为浮动列表

[英]How to convert string list into float list

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. 1:用户在每个键入的数字之间输入一个“冒号”,然后将列表中的元素分开。 (This works atm) (这适用于ATM)

2: Every value has to be outputted in 1 decimal, which I have been trying to do with: print(f'{word:.1f}'). 2:每个值都必须以 1 位小数输出,我一直在尝试这样做:print(f'{word:.1f}')。 But haven't been able to do it correctly.但一直无法正确地做到这一点。

Input: 5:2:4 Output: [5.0, 2.0, 4.0]输入: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)

You can try,你可以试试,

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

This will convert each of the number to float这会将每个数字转换为浮点数

This line:这一行:

word = user_list.split(":")

Takes a string ( user_list ) and splits it into parts over occurrences of : .接受一个字符串 ( user_list ) 并将其拆分为多个部分: The result is a list of strings.结果是一个字符串列表 For example, 'alice:bob'.split(":") would become ['alice', 'bob'] .例如, 'alice:bob'.split(":")将变为['alice', 'bob']

You print all of word , so you're printing a list and that's why you should be seeing ['5.0', '2.0', '4.0'] , since that's the representation in text of the list, still in strings.您打印所有word ,因此您正在打印一个列表,这就是为什么您应该看到['5.0', '2.0', '4.0'] ,因为这是列表文本中的表示,仍然是字符串。

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.这将遍历word中的字符串,将它们转换为浮点数,然后格式化它们,收集所有它们,然后.join()将它们与', '放在一起。

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:您可以遍历数组word中的每个项目,转换为float ,然后使用round()和 append 将其舍入到小数点后到一个新数组:

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 .请注意,如果您希望用户在其输入中包含更长的十进制浮点数(例如3.432 ),并希望将它们输出为3.4 ,那么这就是您想要的。 If you are assuming the user will only include ints in the input,如果您假设用户只会在输入中包含整数,

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)

You can pass a map function into a list function.您可以将 map function 传递到列表 function 中。 Your word variable is already being split by ':'.您的 word 变量已经被 ':' 分割。 Try list(map(float, word))试试 list(map(float, word))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM