# Python 数据挖掘与机器学习实战 (33)：Python 语言简介 2.4 (Python 的列表)

list1 = ['a', 'b', 10, 20]list2 = [1, 2, 3, 4]list3 = ["a", "b", "c"]

list1 = ['a', 'b', 10, 20]list2 = [1, 2, 3, 4]print ("list1: ", list1)print ("list2[1:4]: ", list2[1:4])

list1:  alist2[1:4]:  [2, 3, 4]

list = []                                        #空列表list.append('Hello')                             #使用 append() 添加元素list.append('World!')print (list)

['Hello', 'World!']

list1 = ['a', 'b', 10, 20]print (list1)del list1print (" 删除后的输出为 : ")print (list1)

['a', 'b', 10, 20]删除后的输出为 :['a', 'b', 20]

Python 列表操作符和字符串操作符有些是相似的，如“+”号用于组合列表，“*”号用于重复列表。如表 2-3 所示为常见的列表操作符。

len 列表长度
+ 组合
* 重复
in 元素是否存在于列表中
for 迭代

list1 = [1,2,3]list2 = [4,5,6]print(len(list1))print(list1+list2)print(list1*3)print(3 in list1)for x in list1:    print (x)

3[1, 2, 3, 4, 5, 6][1, 2, 3, 1, 2, 3, 1, 2, 3]True123
{"ft_lang_label":"__label__zh","ft_lang_prob":0.9400918,"math_prob":0.98985565,"size":1872,"snap":"2019-51-2020-05","text_gpt3_token_len":1420,"char_repetition_ratio":0.125803,"word_repetition_ratio":0.09625668,"special_character_ratio":0.31143162,"punctuation_ratio":0.18012422,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9513222,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,2,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-20T13:02:58Z\",\"WARC-Record-ID\":\"<urn:uuid:c2358059-0da7-4659-966d-66d2d33e2341>\",\"Content-Length\":\"173415\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:dc2c4aae-3c91-4fc5-b44c-f588526c3e15>\",\"WARC-Concurrent-To\":\"<urn:uuid:5ac7d301-6cbd-44d9-94b4-c72fe4f7cd1a>\",\"WARC-IP-Address\":\"39.105.227.169\",\"WARC-Target-URI\":\"https://www.infoq.cn/article/w9gw7VOWWJeWpzT3UC7j\",\"WARC-Payload-Digest\":\"sha1:W57J5D2AXM6LYWJRUJASAK5B2GUAPFFX\",\"WARC-Block-Digest\":\"sha1:JU2ODF6JWT7RUF57VJUW2DUIRDKETPVX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250598726.39_warc_CC-MAIN-20200120110422-20200120134422-00419.warc.gz\"}"}