【发布时间】:2020-09-11 12:53:31
【问题描述】:
我有一本字典,其中的值是一个元组列表。
dictionary = {1:[('hello, how are you'),('how is the weather'),('okay
then')], 2:[('is this okay'),('maybe It is')]}
我想让每个键的值成为单个字符串。所以我做了一个函数来完成这项工作,但我不知道如何将它插入到原始字典中。
我的功能:
def list_of_tuples_to_string(dictionary):
for tup in dictionary.values():
k = [''.join(i) for i in tup] #joining list of tuples to make a list of strings
l = [''.join(k)] #joining list of strings to make a string
for j in l:
ki = j.lower() #converting string to lower case
return ki
我想要的输出: dictionary = {1:'你好,那天气好吗', 2:'这样好吗'}
【问题讨论】:
-
只需创建新字典并将所有元素放入其中。
-
for k in dictionary: dictionary[k] = " ".join(dictionary[k]).lower().strip()
标签: python list dictionary tuples