Hướng dẫn add integer to all elements in list python - thêm số nguyên vào tất cả các phần tử trong danh sách 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

Is

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]
222222

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 thế nào để bạn thêm một số nguyên vào tất cả các yếu tố trong danh sách Python?
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

Is

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]
222222

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 thế nào để bạn thêm một số nguyên vào tất cả các yếu tố trong danh sách Python?
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.

test_list 6 test_list 7

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

Is

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]
222222

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 thế nào để bạn thêm một số nguyên vào tất cả các yếu tố trong danh sách Python?

Sử dụng danh sách hiểu để thêm một số nguyên vào mỗi phần tử trong một danh sách. Sử dụng danh sách hiểu để tạo danh sách mới, new_list, với từng phần tử trong danh sách ban đầu, A_List, được tăng lên bởi một số nguyên, integer_to_add, bằng cách lặp qua từng phần tử trong danh sách ban đầu.. Use a list comprehension to generate a new list, new_list , with each of the elements in the original list, a_list , incremented by an integer, integer_to_add , by iterating through each element in the original list.

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

Nối.Một trong những cách phổ biến nhất để thêm số nguyên vào danh sách, chứ đừng nói đến bất kỳ loại dữ liệu nào khác là sử dụng phương thức append ().Điều này chỉ đơn giản sẽ thêm một số nguyên duy nhất vào cuối danh sách.Có một số cách khác để thêm một hoặc nhiều số nguyên vào danh sách mà chúng ta sẽ khám phá trong các phần tiếp theo.use the append() method. This will simply add a single integer to the very end of the list. There are a number of other ways to add one or more integers to a list, which we'll explore in the next sections.

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

append () - Cách thêm một mục vào danh sách trong Python ...
Chèn () - Chèn một phần tử ở bất cứ đâu trong danh sách ..
append () - Luôn thêm các mục (chuỗi, số, danh sách) ở cuối danh sách ..
Extend () - Thêm các mục có thể lặp lại (danh sách, bộ dữ liệu, chuỗi) vào cuối danh sách ..

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.