[英]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
引用了列表中的每个元素,并且每个元素始终是一个元组,因此您可以在迭代器变量中对其进行扩展,例如此处使用的x
和y
:
>>> 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.