简体   繁体   English

我可以使用%f和%d将浮点数和整数格式化为列表中的字符串吗?

[英]Can I use %f and %d to format floats and ints to strings in a list?

I am a starter with python ,without any programming experience and here's my question 我是python的入门者,没有任何编程经验,这是我的问题

I have a list called n 我有一个叫n的清单

n = [ (1.1,5) , (2.4,7) , (5.4,6) , (9.8,14) , (10,4) ]

and I want to create a list which looks like 我想创建一个看起来像

k = [ ('1.1' , {'num' : '5'}) ,
  ('2.4' , {'num' : '7'}) ,
  ('5.4' , {'num' : '6'}) ,
  ('9.8' , {'num' : '14'}) ,
  ('10' , {'num' : '4'}) 
]

I've tried something like 我已经尝试过类似

for  i in range(len(n)):
    k.append(('%f', {'num' : '%d'})) % n[i][0] % n[i][1]

but I got a TypeError : unsupported operand type(s) for % : 'NoneType' and 'float' 但我收到TypeError : unsupported operand type(s) for % : 'NoneType' and 'float'

I'm not sure if I asked this question in a proper way but...hope someone can help me with this, thx T^T 我不确定我是否以适当的方式提出了这个问题,但是...希望有人可以帮我解决这个问题,例如T ^ T

The numbers are already floats and ints, respectively. 这些数字已经分别是浮点数和整数。 Your expected k looks like it converts them to strings. 您期望的k看起来像将它们转换为字符串。 So you can build it that way with a list comprehension: 因此,您可以通过列表理解来构建它:

>>> n = [ (1.1,5) , (2.4,7) , (5.4,6) , (9.8,14) , (10,4) ]
>>> k = [ (str(x) , {'num': str(y)}) for x, y in n]
>>> k
[('1.1', {'num': '5'}), ('2.4', {'num': '7'}), ('5.4', {'num': '6'}), ('9.8', {'num': '14'}), ('10', {'num': '4'})]
>>>

Wrt the code you were trying: 写出您正在尝试的代码:

for i in range(len(n)): - this is not a good way to iterate in Python. for i in range(len(n)): -这不是在Python中进行迭代的好方法。 You don't actually need the item index to reference the item in the current iteration. 您实际上不需要项目索引来引用当前迭代中的项目。 For example: 例如:

>>> for item in n:
...   print item
...
(1.1, 5)
(2.4, 7)
(5.4, 6)
(9.8, 14)
(10, 4)
>>>
>>> # let's get each part of the tuple separately:
... for item in n:
...   print 'tuple index 0:', item[0],
...   print 'tuple index 1:', item[1]
...
tuple index 0: 1.1 tuple index 1: 5
tuple index 0: 2.4 tuple index 1: 7
tuple index 0: 5.4 tuple index 1: 6
tuple index 0: 9.8 tuple index 1: 14
tuple index 0: 10 tuple index 1: 4

Now take a look at the part: 现在看一下该部分:
k.append(('%f', {'num' : '%d'})) % n[i][0] % n[i][1] - looks like you were trying to do the % substitution like in print '%s world!' % 'hello' k.append(('%f', {'num' : '%d'})) % n[i][0] % n[i][1] -似乎您正在尝试执行%替换,例如print '%s world!' % 'hello' print '%s world!' % 'hello' => hello world! print '%s world!' % 'hello' => hello world! . For that to work, you need to use the % operator immediately after the string, like so: 为此,您需要在字符串后立即使用%运算符,如下所示:

>>> k = []
>>> for item in n:
...   k.append(('%f' % item[0], {'num': '%d' % item[1]}))
...
>>> k
[('1.100000', {'num': '5'}), ('2.400000', {'num': '7'}), ('5.400000', {'num': '6'}), ('9.800000', {'num': '14'}), ('10.000000', {'num': '4'}
)]

Now, instead of doing the % format stuff, just cast your floats and ints to strings, using str() . 现在,不用执行%格式的工作,只需使用str()将浮点数和整数转换为字符串即可。 So adapting your version with these here: 因此,请在此处修改您的版本:

>>> k = []
>>> for item in n:
...   k.append((str(item[0]), {'num': str(item[1])}))
...

And since you know item refers to each element in the list, and each those is always a tuple, you can expand it in the iterator-variables, like x and y used here: 并且由于您知道item引用了列表中的每个元素,并且每个元素始终是一个元组,因此您可以在迭代器变量中对其进行扩展,例如此处使用的xy

>>> k = []
>>> for x, y in n:
...   print 'index 0:', x, '; index 1', y
...   k.append((str(x), {'num': str(y)}))
...

Lastly, the pattern of creating an empty list k and then appending items to it in a loop can be done in a more Pythonic way in list comprehensions: 最后,创建空列表k然后在循环中向其添加项目的模式可以通过列表理解的一种更Python化的方式来完成:

>>> k = [(str(x), {'num': str(y)}) for x, y in n]

(Note that k does not first need to be initialised to an empty list.) (请注意, k 不需要首先初始化为空列表。)

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

相关问题 如何将由整数和浮点数组成的列表中的字符串列表转换为整数和浮点数? - How do I convert a list of strings in a list composed of ints and floats to both ints and floats? python将列表中的字符串转换为int和float - python converting strings in list to ints and floats 如何遍历字符串列表并识别整数和浮点数,然后将它们添加到列表中? - How do I iterate over a list of strings and identify ints and floats and then add them to a list? 我为什么要用int而不是浮点数? - Why should I use ints instead of floats? 如何将字符串列表拆分为整数列表? - How can I split this list of strings to list of lists of ints? 如何格式化 json 以便我可以解析浮点数和字符串 - How to format json so I can parse floats and strings 如何在python中使用ints遍历字符串列表? - How can I iterate over a list of strings with ints in python? Python 将带有字符串的列表的格式更改为在列中浮动 - Python change format of a list with strings to floats in a column 如何从列表(txt 文件)中读取字符串并将它们打印为整数、字符串和浮点数? - How to get read strings from a list(a txt file) and print them out as ints, strings, and floats? 如何在python中将带有字符串的列表列表转换为带有int的列表列表? - How can I convert list of list with strings to list of list with ints in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM