python中list.append什么意思_python中list类型的append()和extend()的区别

python中的list类型常用的有append()函数和extend()函数。它二者功能类似,可之间又有什么区别呢?

1. append()

append()用法示例:

>>> mylist = [1,2,0,'abc']

>>> mylist

[1, 2, 0, 'abc']

>>> mylist.append(4)

>>> mylist

[1, 2, 0, 'abc', 4]

>>> mylist.append('haha')

>>> mylist

[1, 2, 0, 'abc', 4, 'haha']

2. extend()

extend()用法示例:

>>> mylist

[1, 2, 0, 'abc', 4, 'haha']

>>> mylist.extend(['lulu'])

>>> mylist

[1, 2, 0, 'abc', 4, 'haha', 'lulu']

>>> mylist.extend([aaa,'lalalala'])

Traceback (most recent call last):

File "", line 1, in

NameError: name 'aaa' is not defined

>>> mylist.extend(['123123','lalalala'])

>>> mylist

[1, 2, 0, 'abc', 4, 'haha', 'lulu', '123123', 'lalalala']

>>> mylist.extend([111111,222])

>>> mylist

[1, 2, 0, 'abc', 4, 'haha', 'lulu', '123123', 'lalalala', 111111, 222]

分析可以发现:

append()只能在list中添加一个元素;

extend()只能添加另外一个list。

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Activity Grader What is it? ----------- This application opens all activities in a given folder in PT, extracts the grade and then outputs a CSV file with the filename, total percentage and sub component scores. System Requirements ------------------- JRE: 1.8 or above (http://java.com/getjava/) Packet Tracer 7.0 or above Upgrading Activity Grader ------------------------- If there is a previous version of Activity Grader installed, remove it from the approved list and delete the "extensions\ActivityGrader" folder. Then follow the "Installing Activity Grader" section. Installing Activity Grader -------------------------- 1) Unzip ActivityGrader.zip 2) Copy the "ActivityGrader" folder to "<Packet Tracer Installation Folder>\extensions" Typically: C:\Program Files\Packet Tracer\extensions 3) Add ActivityGrader.pta using the IPC menu. a) Extensions Menu -> IPC -> Configure Apps b) Click on the Add button c) Select the folder "ActivityGrader" d) Select the ActivityGrader.pta file, then OK (See PT Help and Tutorials for additional information) Launching the Application ------------------------- There are two ways to launch the application. A) Within Packet Tracer: 1) Extensions Menu -> IPC -> Configure Apps 2) Select Activity Grader from App List 3) Click on Launch button B) Standalone, execute "extensions\ActivityGraderx86_64.exe". This method does not launch Packet Tracer automatically. Packet Tracer must be already running and the IPC enabled at port 39000. For either method, Packet Tracer must allow this application to use the IPC. (See Step 3: Installing) Using the Application --------------------- This application uses the activity filenames as the index. So, it would be best to have students append their name or student ID to the activity filename for submission. The location of the activity files should be in a single directory. This directory may be a local disk, network drive, or flash usb drive -- anywhere that Packet Tracer can access. 1) Enter the location of the activities or browse 2) Click Grade A Comma Separated Value (CSV) file will be created in the directory where the activity files are stored.
Activity Grader What is it? ----------- This application opens all activities in a given folder in PT, extracts the grade and then outputs a CSV file with the filename, total percentage and sub component scores. System Requirements ------------------- JRE: 1.8 or above (http://java.com/getjava/) Packet Tracer 7.0 or above Upgrading Activity Grader ------------------------- If there is a previous version of Activity Grader installed, remove it from the approved list and delete the "extensions\ActivityGrader" folder. Then follow the "Installing Activity Grader" section. Installing Activity Grader -------------------------- 1) Unzip ActivityGrader.zip 2) Copy the "ActivityGrader" folder to "<Packet Tracer Installation Folder>\extensions" Typically: C:\Program Files\Packet Tracer\extensions 3) Add ActivityGrader.pta using the IPC menu. a) Extensions Menu -> IPC -> Configure Apps b) Click on the Add button c) Select the folder "ActivityGrader" d) Select the ActivityGrader.pta file, then OK (See PT Help and Tutorials for additional information) Launching the Application ------------------------- There are two ways to launch the application. A) Within Packet Tracer: 1) Extensions Menu -> IPC -> Configure Apps 2) Select Activity Grader from App List 3) Click on Launch button B) Standalone, execute "extensions\ActivityGraderx86.exe". This method does not launch Packet Tracer automatically. Packet Tracer must be already running and the IPC enabled at port 39000. For either method, Packet Tracer must allow this application to use the IPC. (See Step 3: Installing) Using the Application --------------------- This application uses the activity filenames as the index. So, it would be best to have students append their name or student ID to the activity filename for submission. The location of the activity files should be in a single directory. This directory may be a local disk, network drive, or flash usb drive -- anywhere that Packet Tracer can access. 1) Enter the location of the activities or browse 2) Click Grade A Comma Separated Value (CSV) file will be created in the directory where the activity files are stored.
优化以下代码,# 构建特征矩阵和标签向量 X = [] y = data['Rating'] for index, row in data.iterrows(): features = [] # 添加运行时长区间评分 if pd.notna(row['RunTime']): category1 = pd.cut([row['RunTime']], bins=bins1, labels=labels1)[0] if category1 in avg_runtime_ratings: features.append(avg_runtime_ratings[category1]) else: features.append(0) else: features.append(0) # 添加年份区间评分 if pd.notna(row['year']): category2 = pd.cut([row['year']], bins=bins2, labels=labels2)[0] if category2 in avg_year_ratings: features.append(avg_year_ratings[category2]) else: features.append(0) else: features.append(0) # 添加导演评分 if row.Director in avg_director_ratings: features.append(avg_director_ratings[row.Director]) else: features.append(0) # 添加编剧评分 if row.Writer in avg_writer_ratings: features.append(avg_writer_ratings[row.Writer]) else: features.append(0) # 添加主演评分 casts = row.TopTwoCasts.split(',') if len(casts) == 1: cast = casts[0] if cast in avg_casts_ratings: features.append(avg_casts_ratings[cast]) else: features.append(0) features.extend([0, 0]) else: cast_1, cast_2 = casts if cast_1 in avg_casts_ratings: features.append(avg_casts_ratings[cast_1] * 0.6) else: features.append(0) if cast_2 in avg_casts_ratings: features.append(avg_casts_ratings[cast_2] * 0.4) else: features.append(0) # 添加类型评分 genres = row.Genres.split(',') if len(genres) == 1: genre = genres[0] if genre in avg_genres_ratings: features.append(avg_genres_ratings[genre]) else: features.append(0) features.extend([0, 0]) elif len(genres) == 2: genre_1, genre_2 = genres if genre_1 in avg_genres_ratings: features.append(avg_genres_ratings[genre_1] * 0.6) else: features.append(0) if genre_2 in avg_genres_ratings: features.append(avg_genres_ratings[genre_2] * 0.4) else: features.append(0) features.append(0) else: genre_1, genre_2, genre_3 = genres if genre_1 in avg_genres_ratings: features.append(avg_genres_ratings[genre_1] * 0.4) else: features.append(0) if genre_2 in avg_genres_ratings: features.append(avg_genres_ratings[genre_2] * 0.3) else: features.append(0) if genre_3 in avg_genres_ratings: features.append(avg_genres_ratings[genre_3] * 0.3) else: features.append(0) X.append(features) X = pd.DataFrame(X)
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值