使用Python3.6将字典值打印成字符串
假设我有一本字典,如下所示:使用Python3.6将字典值打印成字符串,python,regex,dictionary,Python,Regex,Dictionary,假设我有一本字典,如下所示: {'keyid': ['foo', '1', 'bar', '2', '(FancyStrininParathesis)']} 如何提取字典值中的字符串并创建和打印一个长字符串,例如 print (finalstring) #Desired output: 'foo - 1 bar - 2 (FancyStrininParathesis)' 我已经能够很好地输出值,但是我不知道如何将数组作为一个长字符串输出。我也是python新手,我更熟悉PHP,但是我必
{'keyid': ['foo', '1', 'bar', '2', '(FancyStrininParathesis)']}
如何提取字典值中的字符串并创建和打印一个长字符串,例如
print (finalstring) #Desired output: 'foo - 1 bar - 2 (FancyStrininParathesis)'
我已经能够很好地输出值,但是我不知道如何将数组作为一个长字符串输出。我也是python新手,我更熟悉PHP,但是我必须为这个项目使用一个特殊的python库
谢谢你的帮助
dicttt = {'keyid': ['foo', '1', 'bar', '2', '(FancyStrininParathesis)']}
entr = dicttt["keyid"] # get the list from the dictionary
print( ' - '.join(entr)) # join each lists entry by ' - ' and print all
或者,如果您只是想输出它,您可以通过迭代列表并在不使用换行符的情况下打印来输出它:
for n in range(len(entr)): # get an index into the list
if (n > 0):
print(' - ',end="") # if index > 0 print seperator, no newline
print(entr[n] ,end="") # print list item, no newline
print() # print newline
或者,如果您只是想输出它,您可以通过迭代列表并在不使用换行符的情况下打印来输出它:
for n in range(len(entr)): # get an index into the list
if (n > 0):
print(' - ',end="") # if index > 0 print seperator, no newline
print(entr[n] ,end="") # print list item, no newline
print() # print newline
试一试
试一试
从你对我评论的回复中,我想我明白你想做什么。与
dictionnary = {'keyid': ['foo', '1', 'bar', '2', '(FancyStrininParathesis)']}
您可以使用dictionnary.items()
遍历这些值:
现在因为这里的value
是一个列表,我们可以遍历它并创建我们自己的输出字符串:
for key, value in dictionnary.items():
output = ""
for item in value:
output += item + " - "
output = output[:-3] # Remove the last three characters
print(output)
一种更具python风格的方法是使用str.join
:
for key, value in dictionnary.items():
output = str.join(" - ", value)
print(output)
从你对我评论的回复中,我想我明白你想做什么。与
dictionnary = {'keyid': ['foo', '1', 'bar', '2', '(FancyStrininParathesis)']}
您可以使用dictionnary.items()
遍历这些值:
现在因为这里的value
是一个列表,我们可以遍历它并创建我们自己的输出字符串:
for key, value in dictionnary.items():
output = ""
for item in value:
output += item + " - "
output = output[:-3] # Remove the last three characters
print(output)
一种更具python风格的方法是使用str.join
:
for key, value in dictionnary.items():
output = str.join(" - ", value)
print(output)
试试这个
dt ={'keyid': ['foo', '1', 'bar', '2', '(FancyStrininParathesis)']}
for x in dt.values():
s =' '.join(x)
s
print(s)
试试这个
dt ={'keyid': ['foo', '1', 'bar', '2', '(FancyStrininParathesis)']}
for x in dt.values():
s =' '.join(x)
s
print(s)
用逗号分隔的字符串是什么意思?key
keyid
的值是一个数组check out and@就像我说的,我是Python新手!我没想到那是一个数组!。不管是哪种方式,我将如何使用该数组中的值实现所需的最终字符串?@brent20好吧,您将像在PHP中一样迭代该数组;)!用逗号分隔的字符串是什么意思?keykeyid
的值是一个数组check out and@就像我说的,我是Python新手!我没想到那是一个数组!。不管是哪种方式,我将如何使用该数组中的值实现所需的最终字符串?@brent20好吧,您将像在PHP中一样迭代该数组;)!这太完美了!非常感谢你!我想我可以自己处理剩下的-如果我不想要a-在(FancyStringInParashesis)之前的最后一个字符串(本例中为2)之后,我怎么能不将其包含在那里?如果我想在所有字符串之间使用连字符,那么你的pythonic示例非常有意义,效果也非常好,但我如何处理最后一个呢?@brent20str.join
只打印列表项之间的字符,因此你不必担心,如果您想在所有项目之后添加最后一个项目,您只需将其附加到字符串,这是完美的!非常感谢你!我想我可以自己处理剩下的-如果我不想要a-在(FancyStringInParashesis)之前的最后一个字符串(本例中为2)之后,我怎么能不将其包含在那里?如果我想在所有字符串之间加上连字符,那么你的pythonic示例非常有意义,效果也非常好,但是我该如何处理最后一个呢?@brent20str.join
只打印列表项之间的字符,所以你不必担心,如果你想在所有项之后添加最后一个,你可以将它附加到字符串中