我有此函数返回一个字符串变量:
partition_ = partition(population)
此输出: "[{'3', '4'}, {'1', '2', '5', '6', '7', '8', '9'}]"
我想将此输出分配给此功能:
B = nx_comm.modularity(F1, [{'3', '4'}, {'1', '2', '5', '6', '7', '8', '9'}])
但是由于它是一个字符串,所以模块化功能不接受它,因为我猜是引号。谁能告诉我如何解决这个问题?
提前致谢。
您需要一个函数将输出字符串解析为所需的格式。我做的这是一个简单的解析器,可以创建所需的输出。让我知道是否有效。
partition_ = "[{'3', '4'}, {'1', '2', '5', '6', '7', '8', '9'}]"
def custom_parser(string):
list_of_sets = []
temp = []
begin_set = False
for char in string:
if begin_set:
if char == '}':
begin_set = False
temp2 = set(temp)
list_of_sets.append(temp2)
temp = []
elif char not in ['\'', ',', ' ']:
temp.append(char)
if char == '{':
begin_set = True
return list_of_sets
new_partition_ = custom_parser(partition_)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句