g= map(str, range(10)) g.__next__() g.__next__() x = 1 in g y = ‘4‘ in g ...python易错题详解

复习python易错题集合的第八题,若有错误和改进意见,欢迎评论交流!

执行下面操作后,y的值是( )。

g= map(str, range(10))
g.__next__()
g.__next__()
x = 1 in g
y = '4' in g 

正确答案: False

知识点补充:
            map函数原型为:map(function, iterable, …)
            参数:
                   function – 函数
                   iterable – 一个或多个序列
            序列中的每一个元素调用 function 函数,返回由每次 function 函数返回值组成的新列表;若多个序列,则函数也应有多个参数,每个序列中的第 x 个元素调用 function 函数
            map()得到的是一个生成器. 生成器有个特点, 它是一次性的,高效的来源是不进行存储,运行到哪里即用即取

题目详解:
             g = [ ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’ ]
             调用 g.__ next __() 就像鼠标的光标一样向后走了一个元素,现在是0 | 123456789,再调用即走了两位,为01 | 23456789
             且 g 列表中元素都为字符串类型,则下一个元素为 ‘2’

验证:

g= map(str, range(10))
g.__next__()
g.__next__()
print('2' in g)

输出为:
在这里插入图片描述
            注:不能在输出前加x = 1 in g,y = ‘4’ in g,因为生成器是一次性,调用过则没

            其他题目详解在:python复习之易错或难解题集合(列出对应知识点)(详解答案)(结合程序不同运行结果说明)


这里是平平无奇的77,妄想优秀 ▇ ▇ ▇ ▇ ▇ ▇ ▇ ▇ ▇ … 0%,若想转载请注明原链接,欢迎下次再来!!

  • 12
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import pandas as pd import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap from scipy.spatial.distance import cdist from ant_colony import solve_tsp # 读取城市数据 df = pd.read_excel('world_coordinate.xlsx', index_col=0, dtype=str) # 提取城市和经纬度数据 countrys = df.index.values countrys_coords = np.array(df['[longitude, latitude]'].apply(eval).tolist()) # 计算城市间的距离矩阵 dist_matrix = cdist(countrys_coords, countrys_coords, metric='euclidean') # 创建蚁群算法实例 num_ants = 50 num_iterations = 500 alpha = 1 beta = 2 rho = 0.5 acs = solve_tsp(dist_matrix, num_ants=num_ants, num_iterations=num_iterations, alpha=alpha, beta=beta, rho=rho) # 输出访问完所有城市的最短路径的距离和城市序列 best_path = acs.get_best_path() best_distance = acs.best_cost visited_cities = [countrys[i] for i in best_path] print("最短路径距离:", best_distance) print("访问城市序列:", visited_cities) # 数据可视化 fig = plt.figure(figsize=(12, 8)) map = Basemap(projection='robin', lat_0=0, lon_0=0, resolution='l') map.drawcoastlines(color='gray') map.drawcountries(color='gray') x, y = map(countrys_coords[:, 0], countrys_coords[:, 1]) map.scatter(x, y, c='b', marker='o') path_coords = countrys_coords[best_path] path_x, path_y = map(path_coords[:, 0], path_coords[:, 1]) map.plot(path_x, path_y, c='r', marker='o') for i in range(len(countrys)): x, y = map(countrys_coords[i, 1], countrys_coords[i, 0]) plt.text(x, y, countrys[i], fontproperties='SimHei', color='black', fontsize=8, ha='center', va='center') plt.title("全球首都最短路径规划") plt.show()改成现在都有调用蚁群算法库的代码
最新发布
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值