val = "{:d}".format(byte_stream[pos], 4)
时间: 2024-05-30 08:08:39 浏览: 12
这行代码使用了 Python 的字符串格式化函数 `format()`,将 `byte_stream[pos]` 转换为一个 4 位的十进制整数字符串并赋值给变量 `val`。其中,`{:d}` 表示格式化参数为十进制整数,而 4 则表示要输出的数字位数,不足 4 位时会在左侧补 0。如果 `byte_stream[pos]` 不是整数类型,则会抛出类型错误。如果 `byte_stream[pos]` 超出了字符串长度,也会抛出索引错误。
相关问题
在图的邻接表存储结构下(基于顶点列表和单链表实现),本题要求图类里实现2个方法函数 def addVertex(self, vex_val): def addEdge(self, f, t, cost=0): 函数接口定义: 在这里描述函数接口。例如: def addVertex(self, vex_val): def addEdge(self, f, t, cost=0): 在这里解释接口参数。例如:其中 f和t分别是构成边的顶点在列表中的序号。 裁判测试程序样例: 在这里给出函数被调用进行测试的例子。例如: class arcnode: def __init__(self,adjvex,weight,link=None): self.adjvex = adjvex self.weight = weight self.link=link class vexnode: def __init__(self,data,first_arc=None): self.data = data self.first_arc = first_arc class Graph: def __init__(self): self.vex_list=[] self.vex_num=0 self.edge_num=0 # 请在这里填写答案 # 请在这里填写答案 def print_graph(self): for i in range(self.vex_num): print(self.vex_list[i].data,end="->") cur = self.vex_list[i].first_arc while cur: print("adj:{},weight:{}".format(cur.adjvex,cur.weight),end="->") cur = cur.link print('None') if __name__ =="__main__": g = Graph() s =input() for vertex in s: g.addVertex(vertex) g.addEdge(0,1,11) g.addEdge(0,2,55) g.addEdge(2,3,88) g.addEdge(0,3,33) g.addEdge(1,2,44) g.print_graph()
class arcnode:
def __init__(self, adjvex, weight, link=None):
self.adjvex = adjvex
self.weight = weight
self.link = link
class vexnode:
def __init__(self, data, first_arc=None):
self.data = data
self.first_arc = first_arc
class Graph:
def __init__(self):
self.vex_list = []
self.vex_num = 0
self.edge_num = 0
def addVertex(self, vex_val):
self.vex_list.append(vexnode(vex_val))
self.vex_num += 1
def addEdge(self, f, t, cost=0):
node = arcnode(t, cost)
node.link = self.vex_list[f].first_arc
self.vex_list[f].first_arc = node
self.edge_num += 1
def print_graph(self):
for i in range(self.vex_num):
print(self.vex_list[i].data, end="->")
cur = self.vex_list[i].first_arc
while cur:
print("adj:{},weight:{}".format(cur.adjvex, cur.weight), end="->")
cur = cur.link
print('None')
if __name__ == "__main__":
g = Graph()
s = input()
for vertex in s:
g.addVertex(vertex)
g.addEdge(0, 1, 11)
g.addEdge(0, 2, 55)
g.addEdge(2, 3, 88)
g.addEdge(0, 3, 33)
g.addEdge(1, 2, 44)
g.print_graph()
改写代码风格并保持变量名不变class LinkedList: class Node: def __init__(self, val, prior=None, next=None): self.val = val self.prior = prior self.next = next def __init__(self): self.head = LinkedList.Node(None) # sentinel node (never to be removed) self.head.prior = self.head.next = self.head # set up "circular" topology self.length = 0
下面是改写后的代码风格:
```
class LinkedList:
class Node:
def __init__(self, val, prior=None, next=None):
self.val = val
self.prior = prior
self.next = next
def __init__(self):
self.head = LinkedList.Node(None) # Sentinel node (never to be removed)
self.head.prior = self.head.next = self.head # Set up "circular" topology
self.length = 0
```
注意,变量名没有改变,只是对代码进行了一些格式化和注释。
相关推荐
![pth](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)