是否有任何函数或方法可以从输出(python列表/元组)中删除方括号和引号?

2024-05-19 00:22:35 发布

您现在位置:Python中文网/ 问答频道 /正文

有谁能帮助我了解python代码如何从python输出(列表/元组)中删除方括号和引号吗

    def sdgraph():
    pass


if __name__ == '__sdgraph__':
    sdgraph()


# Fungsi untuk mencari jalur
def find_path(graph, start, end, path=[]):
    path = path + [start]
    for node in graph[start]:
        if not node in path:
            newpath = find_path(graph, node, end, path)
            if newpath:
                return newpath
    if start == end:
        return path
    if not start in graph:
        return None
    return None

# fungsi untuk mencari semua jalur tersedia
def all_path(graph, start, end, path=[]):
    path = path + [start]
    if start == end:
        return [path]
    if not start in graph:
        return []
    paths = []
    for node in graph[start]:
        if not node in path:
            newpaths = all_path(graph, node, end, path)
            for newpath in newpaths:
                paths.append(newpath)
    return paths


# fungsi untuk mencari jalur tercepat
def shortest_path(graph, start, end, path=[]):
    path = path + [start]
    if start == end:
        return path
    if not start in graph:
        return None
    shortest = None
    for node in graph[start]:
        if node not in path:
            newpath = shortest_path(graph, node, end, path)
            if newpath:
                if not shortest or len(newpath) < len(shortest):
                    shortest = newpath
    return shortest

# fungsi untuk mencari jalur terlama/terpanjang
def longest_path(graph, start, end, path=[]):
    path = path + [start]
    if start == end:
        return path
    if not start in graph:
        return None

    longest = None
    for node in graph[start]:
        if node not in path:
            newpath = longest_path(graph, node, end, path)
            if newpath:
                if not longest or len(newpath) > len(longest):
                    longest = newpath
    return longest

# fungsi untuk mencari path alternatif jika tercepat macet
def alternate_path(graph, start, end, path=[]):
    path = path + [start]
    if start == end:
        return path
    if not start in graph:
        return None
    shortest = None
    for node in graph[start]:
        if node not in path:
            newpath = shortest_path(graph, node, end, path)
            if newpath:
                if not shortest or len(newpath) == len(shortest):
                    shortest = newpath
    return shortest


# fungsi untuk mencari semua edge/penghubung antar 2 node
def all_edge(graph):
    edges = []
    for node in graph:
        for val in graph[node]:
            edges.append((node, val))
    return edges


# deklarasi pemetaan graph pada kota
graph = {'JAKARTA': ['DEPOK', 'TANGERANG', 'BEKASI'],
         'BOGOR': ['DEPOK', 'BANDUNG'],
         'DEPOK': ['JAKARTA', 'BOGOR', 'TANGERANG', 'BEKASI'],
         'TANGERANG': ['JAKARTA', 'DEPOK'],
         'BEKASI': ['JAKARTA', 'DEPOK', 'BANDUNG'],
         'BANDUNG': ['BOGOR', 'BEKASI'],
         'KOTA': []}

# variabel untuk input asal kota atau start
print("========================================================================================")
asal = input("Masukkan Kota Asal   : ")

print("========================================================================================")
# variabel untuk input kota tujuan atau end
tujuan = input("Masukkan Kota Tujuan : ")

print("========================================================================================")
print('Kota Asal   = ', asal)
print('Kota Tujuan = ', tujuan)
print("========================================================================================")
# pemanggilan fungsi semua jalur tersedia
print('Semua Jalur Normal = ', end="")
print(all_path(graph, asal, tujuan))
print("========================================================================================")
# pemanggilan fungsi mencari jalur tercepat
print('Jalur Tercepat     = ', end="")
print(shortest_path(graph, asal, tujuan))
print("========================================================================================")
# pemanggilan fungsi untuk jalur alternatif jika jalur tercepat macet
print('Jalur Alternatif   = ', end="")
print(alternate_path(graph, asal, tujuan))
print("========================================================================================")
# pemanggilan fungsi untuk mencari semua edge/penghubung 2 node
print('Semua Edge         = ', end="")
print(all_edge(graph))
print("========================================================================================")

输出是

========================================================================================

Masukkan Kota Asal   : TANGERANG
========================================================================================

Masukkan Kota Tujuan : BEKASI
========================================================================================
Kota Asal   =  TANGERANG
Kota Tujuan =  BEKASI
========================================================================================
Semua Jalur Normal = [['TANGERANG', 'JAKARTA', 'DEPOK', 'BOGOR', 'BANDUNG', 'BEKASI'], ['TANGERANG', 'JAKARTA', 'DEPOK', 'BEKASI'], ['TANGERANG', 'JAKARTA', 'BEKASI'], ['TANGERANG', 'DEPOK', 'JAKARTA', 'BEKASI'], ['TANGERANG', 'DEPOK', 'BOGOR', 'BANDUNG', 'BEKASI'], ['TANGERANG', 'DEPOK', 'BEKASI']]
========================================================================================
Jalur Tercepat     = ['TANGERANG', 'JAKARTA', 'BEKASI']
========================================================================================
Jalur Alternatif   = ['TANGERANG', 'DEPOK', 'BEKASI']
========================================================================================
Semua Edge         = [('JAKARTA', 'DEPOK'), ('JAKARTA', 'TANGERANG'), ('JAKARTA', 'BEKASI'), ('BOGOR', 'DEPOK'), ('BOGOR', 'BANDUNG'), ('DEPOK', 'JAKARTA'), ('DEPOK', 'BOGOR'), ('DEPOK', 'TANGERANG'), ('DEPOK', 'BEKASI'), ('TANGERANG', 'JAKARTA'), ('TANGERANG', 'DEPOK'), ('BEKASI', 'JAKARTA'), ('BEKASI', 'DEPOK'), ('BEKASI', 'BANDUNG'), ('BANDUNG', 'BOGOR'), ('BANDUNG', 'BEKASI')]
========================================================================================

我想从输出中删除引号“”和方括号[],这里有人能帮我吗


Tags: pathinnodereturnifstartgraphend
2条回答

可以使用strip()从该字符中删除字符串

您可以使用print(*all_edge(graph))

如果要用逗号和空格分隔元素,请使用print(*all_edge(graph), sep=', ')

对于嵌套结构,您需要一个函数来展平数据:

def flatten(container):
    for i in container:
        if isinstance(i, (list, tuple)):
            for j in flatten(i):
                yield j
        else:
            yield i

然后 print(*flatten(all_edge(graph)))

相关问题 更多 >

    热门问题