python疑难问题---11、python内嵌for...in循环
python疑难问题---11、python内嵌for...in循环
一、总结
一句话总结:
y=[x for x in range(100)] 表示先做for...in循环,x会得到0到99,然后把这0到99这100个数放到[](列表)中
y=[x for x in range(100)] print(y) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
二、python内嵌for...in循环
博客对应课程的视频位置:11、python内嵌for...in循环-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/369
1、python内嵌for...in循环简单使用
In [1]:
y=[[x]for x in range(100)]
print(y)
如果去掉[x]上的中括号,得到的结果是怎样
In [2]:
y=[x for x in range(100)]
print(y)
2、用内嵌for...in循环反转字典键值
In [3]:
dict1={'fawn': 34701, 'tsukino': 52006, 'nunnery': 52007, 'sonja': 16816}
In [5]:
dict2=[(value,key) for (key,value) in dict1.items()]
print(dict2)
print(dict(dict2))
3、内嵌for...in循环另一个例子
In [6]:
import tensorflow as tf
In [7]:
(train_data, train_labels),(test_data, test_labels)= tf.keras.datasets.imdb.load_data(num_words=10000)
In [8]:
print(train_data.shape)
In [9]:
print(train_data[0])
需求:将这段评论数据转换成文字
In [10]:
# word_index是单词与数字索引对应表
word_index = tf.keras.datasets.imdb.get_word_index()
In [11]:
print(type(word_index))
In [16]:
# print(word_index)
{'fawn': 34701, 'tsukino': 52006, 'nunnery': 52007, 'sonja': 16816, 'vani': 63951, 'woods': 1408, 'spiders': 16115, 'hanging': 2345, 'woody': 2289, 'trawling': 52008, "hold's": 52009, 'comically': 11307, 'localized': 40830, 'disobeying': 30568, "'royale": 52010, "harpo's": 40831,......}
In [17]:
# 键值颠倒,将整数 索引映射为单词
reverse_word_index = dict(
[(value, key) for (key, value) in word_index.items()])
# print(reverse_word_index)
{34701: 'fawn', 52006: 'tsukino', 52007: 'nunnery', 16816: 'sonja', 63951: 'vani', 1408: 'woods', 16115: 'spiders', 2345: 'hanging', 2289: 'woody', 52008: 'trawling', 52009: "hold's", 11307: 'comically', 40830: 'localized', 30568: 'disobeying', 52010: "'royale", 40831: "harpo's", 52011: 'canet', 19313: 'aileen', 52012: 'acurately', 52013: "diplomat's", 25242: 'rickman', 6746: 'arranged', 52014: 'rumbustious', 52015: 'familiarness'......}
In [14]:
# 将评论解码。注意,索引减去了3,因为0、1、2 是为
# “padding”(填充)、“ start of sequence”(序 列开始)、
# “unknown”(未知词)分别保留的索引
data=[reverse_word_index.get(i - 3, '?') for i in train_data[0]]
print(data)
In [15]:
print(' '.join(data))
In [ ]:
系列博客对应课程视频地址:
1、遍历列表的三种方式-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/293
2、字典排序-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/294
3、可变和不可变数据类型-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/295
4、python文件处理-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/305
5、二维列表初始化-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/306
6、python中浅拷贝和深度拷贝-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/307
7、python中的with...as...-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/320
8、只有一个数的元组表示-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/343
9、Python类中call函数的作用-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/361
10、python遍历字典的几种方式-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/368
11、python内嵌for...in循环-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/369
12、python生成器-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/370
13、Python切片操作-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/377
1、遍历列表的三种方式-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/293
2、字典排序-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/294
3、可变和不可变数据类型-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/295
4、python文件处理-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/305
5、二维列表初始化-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/306
6、python中浅拷贝和深度拷贝-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/307
7、python中的with...as...-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/320
8、只有一个数的元组表示-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/343
9、Python类中call函数的作用-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/361
10、python遍历字典的几种方式-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/368
11、python内嵌for...in循环-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/369
12、python生成器-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/370
13、Python切片操作-范仁义-读书编程笔记
https://www.fanrenyi.com/video/32/377
版权申明:欢迎转载,但请注明出处
一些博文中有一些参考内容因时间久远找不到来源了没有注明,如果侵权请联系我删除。
在校每年国奖、每年专业第一,加拿大留学,先后工作于华东师范大学和香港教育大学。
2024-10-30:27岁,宅加太忙,特此在网上找女朋友,坐标上海,非诚勿扰,vx:fan404006308
AI交流资料群:753014672