微信扫一扫打赏支持

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)
[[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]]

如果去掉[x]上的中括号,得到的结果是怎样

In [2]:
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]

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))
[(34701, 'fawn'), (52006, 'tsukino'), (52007, 'nunnery'), (16816, 'sonja')]
{34701: 'fawn', 52006: 'tsukino', 52007: 'nunnery', 16816: 'sonja'}

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)
(25000,)
In [9]:
print(train_data[0])
[1, 14, 22, 16, 43, 530, 973, 1622, 1385, 65, 458, 4468, 66, 3941, 4, 173, 36, 256, 5, 25, 100, 43, 838, 112, 50, 670, 2, 9, 35, 480, 284, 5, 150, 4, 172, 112, 167, 2, 336, 385, 39, 4, 172, 4536, 1111, 17, 546, 38, 13, 447, 4, 192, 50, 16, 6, 147, 2025, 19, 14, 22, 4, 1920, 4613, 469, 4, 22, 71, 87, 12, 16, 43, 530, 38, 76, 15, 13, 1247, 4, 22, 17, 515, 17, 12, 16, 626, 18, 2, 5, 62, 386, 12, 8, 316, 8, 106, 5, 4, 2223, 5244, 16, 480, 66, 3785, 33, 4, 130, 12, 16, 38, 619, 5, 25, 124, 51, 36, 135, 48, 25, 1415, 33, 6, 22, 12, 215, 28, 77, 52, 5, 14, 407, 16, 82, 2, 8, 4, 107, 117, 5952, 15, 256, 4, 2, 7, 3766, 5, 723, 36, 71, 43, 530, 476, 26, 400, 317, 46, 7, 4, 2, 1029, 13, 104, 88, 4, 381, 15, 297, 98, 32, 2071, 56, 26, 141, 6, 194, 7486, 18, 4, 226, 22, 21, 134, 476, 26, 480, 5, 144, 30, 5535, 18, 51, 36, 28, 224, 92, 25, 104, 4, 226, 65, 16, 38, 1334, 88, 12, 16, 283, 5, 16, 4472, 113, 103, 32, 15, 16, 5345, 19, 178, 32]

需求:将这段评论数据转换成文字

In [10]:
# word_index是单词与数字索引对应表
word_index = tf.keras.datasets.imdb.get_word_index() 
In [11]:
print(type(word_index))
<class 'dict'>
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)
['?', 'this', 'film', 'was', 'just', 'brilliant', 'casting', 'location', 'scenery', 'story', 'direction', "everyone's", 'really', 'suited', 'the', 'part', 'they', 'played', 'and', 'you', 'could', 'just', 'imagine', 'being', 'there', 'robert', '?', 'is', 'an', 'amazing', 'actor', 'and', 'now', 'the', 'same', 'being', 'director', '?', 'father', 'came', 'from', 'the', 'same', 'scottish', 'island', 'as', 'myself', 'so', 'i', 'loved', 'the', 'fact', 'there', 'was', 'a', 'real', 'connection', 'with', 'this', 'film', 'the', 'witty', 'remarks', 'throughout', 'the', 'film', 'were', 'great', 'it', 'was', 'just', 'brilliant', 'so', 'much', 'that', 'i', 'bought', 'the', 'film', 'as', 'soon', 'as', 'it', 'was', 'released', 'for', '?', 'and', 'would', 'recommend', 'it', 'to', 'everyone', 'to', 'watch', 'and', 'the', 'fly', 'fishing', 'was', 'amazing', 'really', 'cried', 'at', 'the', 'end', 'it', 'was', 'so', 'sad', 'and', 'you', 'know', 'what', 'they', 'say', 'if', 'you', 'cry', 'at', 'a', 'film', 'it', 'must', 'have', 'been', 'good', 'and', 'this', 'definitely', 'was', 'also', '?', 'to', 'the', 'two', 'little', "boy's", 'that', 'played', 'the', '?', 'of', 'norman', 'and', 'paul', 'they', 'were', 'just', 'brilliant', 'children', 'are', 'often', 'left', 'out', 'of', 'the', '?', 'list', 'i', 'think', 'because', 'the', 'stars', 'that', 'play', 'them', 'all', 'grown', 'up', 'are', 'such', 'a', 'big', 'profile', 'for', 'the', 'whole', 'film', 'but', 'these', 'children', 'are', 'amazing', 'and', 'should', 'be', 'praised', 'for', 'what', 'they', 'have', 'done', "don't", 'you', 'think', 'the', 'whole', 'story', 'was', 'so', 'lovely', 'because', 'it', 'was', 'true', 'and', 'was', "someone's", 'life', 'after', 'all', 'that', 'was', 'shared', 'with', 'us', 'all']
In [15]:
print(' '.join(data))
? this film was just brilliant casting location scenery story direction everyone's really suited the part they played and you could just imagine being there robert ? is an amazing actor and now the same being director ? father came from the same scottish island as myself so i loved the fact there was a real connection with this film the witty remarks throughout the film were great it was just brilliant so much that i bought the film as soon as it was released for ? and would recommend it to everyone to watch and the fly fishing was amazing really cried at the end it was so sad and you know what they say if you cry at a film it must have been good and this definitely was also ? to the two little boy's that played the ? of norman and paul they were just brilliant children are often left out of the ? list i think because the stars that play them all grown up are such a big profile for the whole film but these children are amazing and should be praised for what they have done don't you think the whole story was so lovely because it was true and was someone's life after all that was shared with us all
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
 
 
 
posted @ 2020-10-20 18:21  范仁义  阅读(600)  评论(0编辑  收藏  举报