edge_ori[0].append(i) edge_ori[1].append(j) edge_ori[0].append(j) edge_ori[1].append(i)
时间: 2024-05-31 17:08:14 浏览: 6
These four lines of code are adding an edge to a graph represented as a list of edges.
- The first line appends the index of the first vertex of the edge to `edge_ori[0]`.
- The second line appends the index of the second vertex of the edge to `edge_ori[1]`.
- The third line appends the index of the second vertex of the edge to `edge_ori[0]`.
- The fourth line appends the index of the first vertex of the edge to `edge_ori[1]`.
In other words, these lines of code are adding the edge (i,j) to the graph, as well as the edge (j,i) since the graph is undirected.
相关问题
for ori_item_name, ori_item_price in origin_item_price: ori_item_price = xor_float_r2(ori_item_price) if ori_item_price <= 0: break # 数据格式为("*饲料*猪饲料120g", (['12.3', '11.92'], ['asx...', 'sxc...'])) match_res = True for inv_item_name, (matched_item_prices, matched_invoice_indexes) in item_name_map.items(): if double_in(ignore_brackets(ori_item_name), ignore_brackets(inv_item_name)): if ori_item_price in matched_item_prices: match_res = True break meta_ori.append(str((ori_item_name, ori_item_price)).replace("'", '')) meta_inv.append(str((inv_item_name, matched_item_prices)).replace("'", '')) for index in matched_invoice_indexes: inv_info = details[index] err_msg_temp.append(f"发票号码{inv_info['发票号码']}") err_img_temp.append(images[index]) match_res = False if not match_res: err_msg.extend(err_msg_temp) err_img.extend(err_img_temp)优化这段代码
这段代码可以进行以下优化:
1. 将`ignore_brackets`函数提前定义,以避免重复调用。
2. 将`if ori_item_price <= 0: break`改为`if ori_item_price > 0:`,从而避免使用`break`语句。
3. 将`meta_ori.append`和`meta_inv.append`放在`for`循环外部,以避免重复执行。
4. 将`err_msg_temp`和`err_img_temp`改为`err_msg`和`err_img`,并将其初始化为空列表,以避免重复定义。
5. 将`if not match_res:`改为`if match_res == False:`,从而使代码更易读。
改写后的代码如下:
```
def ignore_brackets(item_name):
return re.sub(r'(.*)', '', item_name)
ignore_brackets = re.compile(r'(.*)')
meta_ori = []
meta_inv = []
err_msg = []
err_img = []
for ori_item_name, ori_item_price in origin_item_price:
ori_item_price = xor_float_r2(ori_item_price)
if ori_item_price > 0:
match_res = True
err_msg_temp = []
err_img_temp = []
for inv_item_name, (matched_item_prices, matched_invoice_indexes) in item_name_map.items():
if double_in(ignore_brackets(ori_item_name), ignore_brackets(inv_item_name)):
if ori_item_price in matched_item_prices:
match_res = True
break
else:
meta_ori.append(str((ori_item_name, ori_item_price)).replace("'", ''))
meta_inv.append(str((inv_item_name, matched_item_prices)).replace("'", ''))
for index in matched_invoice_indexes:
inv_info = details[index]
err_msg_temp.append(f"发票号码{inv_info['发票号码']}")
err_img_temp.append(images[index])
match_res = False
if match_res == False:
err_msg.extend(err_msg_temp)
err_img.extend(err_img_temp)
```
for i in range(0, len(ori_data) - seq_len): _x = ori_data[i:i + seq_len] temp_data.append(_x)
这段代码是一个循环,用于将长度为seq_len的子序列从ori_data中提取出来,并将其添加到temp_data列表中。
循环的范围是从0到len(ori_data) - seq_len。这样可以保证在提取子序列时不会超出ori_data的边界。
在每次循环中,通过切片操作ori_data[i:i + seq_len],将从索引i开始,长度为seq_len的子序列提取出来,并赋值给变量_x。
然后,将_x添加到temp_data列表中,用于存储所有提取出的子序列。
循环结束后,temp_data中就包含了所有长度为seq_len的子序列,可以在后续的代码中使用。
相关推荐
![v](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![v](https://img-home.csdnimg.cn/images/20210720083646.png)