Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中访问时在列表中重复使用字符串格式_Python_List_String Formatting - Fatal编程技术网

在python中访问时在列表中重复使用字符串格式

在python中访问时在列表中重复使用字符串格式,python,list,string-formatting,Python,List,String Formatting,在python中,当访问列表中的某个项时,是否可以执行重复的字符串格式化操作 例如: >>>from random import randint >>>a = ["The random number is: {0}".format(randint(0,10))] >>>print a[0] The random number is: 3 >>>print a[0] The random number is: 3 a = [

在python中,当访问列表中的某个项时,是否可以执行重复的字符串格式化操作

例如:

>>>from random import randint
>>>a = ["The random number is: {0}".format(randint(0,10))]
>>>print a[0]
The random number is: 3
>>>print a[0]
The random number is: 3
a = ["The some sweet string: {0}".format(someFunction),
     "Another {0} different string {1}".format(someFunctionTwo, someFunctionThree)]
显然,它是获取一个随机整数,格式化字符串,并在首次定义列表时将其保存在列表中。撇开性能不谈,我想知道是否有可能覆盖此行为

我知道如果我看到这个问题,我会回答类似“你做错了”,并会提供类似于下面答案的东西

>>>a = ["The random number is: {0}"]
>>>print a[0].format(randint(0,10))
但我们假设这不是这个问题的解决方案。我真的很想在列表中定义格式(如果可能的话)

另一个例子:

>>>from random import randint
>>>a = ["The random number is: {0}".format(randint(0,10))]
>>>print a[0]
The random number is: 3
>>>print a[0]
The random number is: 3
a = ["The some sweet string: {0}".format(someFunction),
     "Another {0} different string {1}".format(someFunctionTwo, someFunctionThree)]
其中someFunction*在每次调用时提供“随机”结果

我知道这有点牵强,我可能不得不依赖已经提供的方法(谢谢你的反馈),但是,我想我会尝试一下


再次感谢

最好为此使用函数:

In [1]: from random import randint

In [2]: def func():
   ...:     return "The random number is: {0}".format(randint(0,10))
   ...: 

In [3]: func()
Out[3]: 'The random number is: 7'

In [4]: func()
Out[4]: 'The random number is: 2'

In [5]: func()
Out[5]: 'The random number is: 3'

您可以创建一个类并重写
\uuuu str\uuu

>>> from random import randint
>>> class Foo(object):
...     def __str__(self):
...        return "The random number is: {0}".format(randint(0,10))
... 
>>> a = [Foo()]
>>> print a[0]
The random number is: 8
>>> print a[0]
The random number is: 10
>>> print a[0]
The random number is: 5 
但你是对的,我的第一个倾向是说你可能做错了


下面是另一个想法--在列表中保留格式字符串:

a = ["The some sweet string: {func1}",
     "Another {func2} different string {func3}"]

for item in a:
   print item.format(func1=func1(),func2=func2(),func3=func3())

显然,这是没有效率的(当您不一定需要函数时调用它们…),但它可以工作。

NB我不认为在这种情况下是a。或者,您可以将
list
子类化,并定义
def\uu getitem\uuuuuu(self,I):return list.\uu getitem\uuuuu(self,I).format(random.randint(0,10))
或其他内容,但这让我觉得写这篇文章很傻。@delnan——我同意。但我不知道OP还在找什么。。。在任何其他情况下(例如函数),您都需要显式调用OP试图避免的东西。@DSM——一个有趣的想法。这也会让我觉得自己很傻——(并不是说我在创建上面发布的代码后还没有感到有点脏)@RyanAdams——我还是有点不明白你在这里要做什么。你能用关键词吗<编码>['the random number is{rand}','the foo is{bar}']然后通过以下方式在列表上循环创建字符串:
item.format(rand=randint(0,10),bar=“baz is qux”)