Hướng dẫn how do you add a number to each element of a list in python? - làm thế nào để bạn thêm một số vào mỗi phần tử của danh sách trong python?

Nhiều câu trả lời ở trên là rất tốt. Tôi cũng đã thấy một số câu trả lời kỳ lạ sẽ thực hiện công việc. Ngoài ra, câu trả lời cuối cùng được nhìn thấy là thông qua một vòng lặp bình thường. Sự sẵn sàng đưa ra câu trả lời này dẫn tôi đến itertoolsnumpy, điều này sẽ làm cùng một công việc theo một cách khác.

Ở đây tôi trình bày những cách khác nhau để thực hiện công việc, không được trả lời ở trên.

import operator
import itertools

x = [3, 5, 6, 7]

integer = 89

"""
Want more vairaint can also use zip_longest from itertools instead just zip
"""
#lazy eval
a = itertools.starmap[operator.add, zip[x, [89] * len[x]]] # this is not subscriptable but iterable
print[a]
for i in a:
  print[i, end = ","]


# prepared list
a = list[itertools.starmap[operator.add, zip[x, [89] * len[x]]]] # this returns list
print[a]



# With numpy [before this, install numpy if not present with `pip install numpy`]
import numpy

res = numpy.ones[len[x], dtype=int] * integer + x # it returns numpy array
res = numpy.array[x] + integer # you can also use this, infact there are many ways to play around
print[res]
print[res.shape] # prints structure of array, i.e. shape

# if you specifically want a list, then use tolist

res_list = res.tolist[]
print[res_list]

Đầu ra

>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]

Lý do duy nhất của tôi để làm nổi bật việc sử dụng numpy là người ta phải luôn luôn thực hiện các thao tác như vậy với các thư viện như Numpy vì nó hiệu quả hiệu suất cho các mảng rất lớn.sole reason to highlight the use of numpy is that one should always do such manipulations with libraries like numpy because it is performance efficient for very large arrays.

Trong khi làm việc với các danh sách Python, chúng tôi có thể gặp phải một tình huống mà chúng tôi yêu cầu để thêm số nguyên k vào mỗi phần tử trong danh sách. Chúng ta có thể cần lặp lại và thêm K vào mỗi phần tử nhưng điều đó sẽ làm tăng dòng mã. Hãy để thảo luận về một số tốc ký để thực hiện nhiệm vụ này.

Phương pháp số 1: Sử dụng danh sách hiểu hiểu biết chỉ là cách ngắn để thực hiện nhiệm vụ chúng tôi thực hiện bằng phương pháp ngây thơ. Điều này chủ yếu là hữu ích để tiết kiệm thời gian và cũng là tốt nhất trong số những người khác khi nói đến khả năng đọc của mã.
List comprehension is just the short way to perform the task we perform using the naive method. This is mainly useful to save time and also is best among others when it comes to readability of the code.

test_list =

>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
0
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
1
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
2
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
3
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
2
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
5__12

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
1
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
2
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
3
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
4
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
5
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
6

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
7=
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
1

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
0____9
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
2
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
4
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
7
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
5
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
6
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
7
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
8

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
1
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
2
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
1 ________ 24 & nbsp;
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
5
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
4

Đầu ra:

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]

Phương pháp số 2: Sử dụng hàm

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
5 + LambDAMAP có thể được sử dụng để ghép từng phần tử với hàm Lambda thực hiện nhiệm vụ thêm K vào mỗi phần tử trong danh sách.
map function can be used to pair each element with the lambda function which performs the task of adding K to each element in the list.

test_list =

>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
0
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
1
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
2
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
3
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
2
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
5__12

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
1
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
2
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
3
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
4
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
5
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
6

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
7=
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
1

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
0____9
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
2
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
4
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
7
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
5
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
6
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
7
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
8

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
1
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
2
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
1 ________ 24 & nbsp;
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
5
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
4

Đầu ra:

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]

Phương pháp số 2: Sử dụng hàm

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
5 + LambDAMAP có thể được sử dụng để ghép từng phần tử với hàm Lambda thực hiện nhiệm vụ thêm K vào mỗi phần tử trong danh sách.
This is similar to the above function but uses thetest_list 5 to add each element to other element from the other list of K formed before applying the map function. It adds the similar index elements of list.

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
0____9 numpy0
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
2222

test_list =

>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
0
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
1
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
2
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
3
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
2
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
5__12

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
1
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
2
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
3
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
4
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
5
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
6

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
7=
>>>  # output by lazy val
>>> 92,94,95,96,                                     # output of iterating above starmap object
>>> [92, 94, 95, 96]                                 # output obtained by casting to list
>>>                   __
>>> # |\ | |  | |\/| |__| \ /
>>> # | \| |__| |  | |     |
>>> [92 94 95 96]                                    # this is numpy.ndarray object
>>> [4,]                                             # shape of array
>>> [92, 94, 95, 96]                                 # this is a list object [doesn't have a shape]
1

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
0____9
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
2
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
4
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
7
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
5
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
6
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
7
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
8

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
1
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
2
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
1 ________ 24 & nbsp;
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
5
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
4

Đầu ra:

The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]

Làm cách nào để thêm một số vào tất cả các yếu tố của một mảng numpy?

Thêm số trực tiếp vào mảng sử dụng thư viện Numpy Nhập Numpy và tạo một mảng Numpy.Bây giờ thêm số vào mảng bằng toán tử cộng.Điều này sẽ trả về một mảng mới chứa các phần tử từ mảng numpy ban đầu và số được cho thêm vào mỗi phần trong số chúng.In mảng.Import numpy library and create a numpy array. Now add the number to array using the plus operator. This will return a new array contains the elements from original numpy array and given number added to each of them. Print the array.

Làm thế nào để bạn thêm số vào một danh sách?

Sử dụng Enumerate [] để thêm số nguyên vào danh sách..
a_list = [0, 0, 0].
Integers_to_add = [1, 2, 3].
cho chỉ mục, số nguyên trong liệt kê [integers_to_add]:.
a_list [index] += số nguyên ..
print[a_list].

Làm thế nào để bạn nối một giá trị trong Python?

Phương thức append [] nối thêm một phần tử vào cuối danh sách. appends an element to the end of the list.

Làm thế nào để bạn thêm nhiều giá trị vào một danh sách trong Python?

Mở rộng để mở rộng danh sách bằng nhiều giá trị từ bất kỳ loại khác, là một danh sách khác hoặc bất kỳ điều gì khác cung cấp một chuỗi các giá trị.Vì vậy, bạn có thể sử dụng danh sách.nối [] để nối một giá trị duy nhất và list.extend [] để nối nhiều giá trị.list. extend[] to append multiple values.

Chủ Đề