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 itertools
và numpy
, đ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__12The 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]
1The 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__12The 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]
1The 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
numpy
0The 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__12The 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]
1The 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]