Mẹo về Python join first n elements of list Chi Tiết
You đang tìm kiếm từ khóa Python join first n elements of list được Cập Nhật vào lúc : 2022-09-21 14:40:18 . Với phương châm chia sẻ Kinh Nghiệm Hướng dẫn trong nội dung bài viết một cách Chi Tiết Mới Nhất. Nếu sau khi đọc nội dung bài viết vẫn ko hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Mình lý giải và hướng dẫn lại nha.
With linq I would
Nội dung chính
- Slicing a list
- Slicing a generator
- How do I merge the first two elements in a list Python?
- How do you join elements in a list in Python?
- How do you print the top 5 elements of a list in Python?
- How do you make a list with N elements in Python?
var top5 = array.Take(5);
How to do this with Python?
martineau
115k25 gold badges160 silver badges283 bronze badges
asked Mar 8, 2011 14:53
Jader DiasJader Dias
85.6k152 gold badges415 silver badges615 bronze badges
1
Slicing a list
top5 = array[:5]
- To slice a list, there’s a simple syntax: array[start:stop:step]
- You can omit any parameter. These are all valid: array[start:], array[:stop], array[::step]
Slicing a generator
import itertools
top5 = itertools.islice(my_list, 5) # grab the first five elements
You can’t slice a generator directly in Python. itertools.islice() will wrap an object in a new slicing generator using the syntax itertools.islice(generator, start, stop, step)
Remember, slicing a generator will exhaust it partially. If you want to keep the entire generator intact, perhaps turn it into a tuple or list first, like: result = tuple(generator)
Nico Schlömer
48.5k24 gold badges186 silver badges223 bronze badges
answered Mar 8, 2011 15:00
5
import itertools
top5 = itertools.islice(array, 5)
answered Mar 8, 2011 14:56
Jader DiasJader Dias
85.6k152 gold badges415 silver badges615 bronze badges
4
@Shaikovsky’s answer is excellent, but I wanted to clarify a couple of points.
[next(generator) for _ in range(n)]
This is the most simple approach, but throws StopIteration if the generator is prematurely exhausted.
On the other hand, the following approaches return up to
n items which is preferable in many circumstances:
List: [x for _, x in zip(range(n), records)]
Generator: (x for _, x in zip(range(n), records))
Neuron
4,5684 gold badges32 silver badges53 bronze badges
answered Aug 17, 2022 11:29
6
In my taste, it’s also very concise to combine zip() with xrange(n) (or range(n) in Python3), which works nice on generators as well and
seems to be more flexible for changes in general.
# Option #1: taking the first n elements as a list
[x for _, x in zip(xrange(n), generator)]
# Option #2, using ‘next()’ and taking care for ‘StopIteration’
[next(generator) for _ in xrange(n)]
# Option #3: taking the first n elements as a new generator
(x for _, x in zip(xrange(n), generator))
# Option #4: yielding them by simply preparing a function
# (but take care for ‘StopIteration’)
def top_n(n, generator):
for _ in xrange(n):
yield next(generator)
Neuron
4,5684 gold
badges32 silver badges53 bronze badges
answered Oct 3, 2014 20:21
ShaikovskyShaikovsky
4865 silver badges7 bronze badges
The answer for how to do this can be found here
>>> generator = (i for i in xrange(10))
>>> list(next(generator) for _ in range(4))
[0, 1, 2, 3]
>>> list(next(generator) for _ in range(4))
[4, 5, 6, 7]
>>> list(next(generator) for _ in range(4))
[8, 9]
Notice that the last call asks for the next 4 when only 2 are remaining. The use of the list() instead of [] is what gets the comprehension to terminate on the StopIteration exception that is thrown by next().
answered Oct 15, 2015 19:37
ebergersonebergerson
3492 silver badges6 bronze badges
2
Do you mean the first N items, or the N largest items?
If you want the first:
top5 = sequence[:5]
This also works for the largest N items,
assuming that your sequence is sorted in descending order. (Your LINQ example seems to assume this as well.)
If you want the largest, and it isn’t sorted, the most obvious solution is to sort it first:
l = list(sequence)
l.sort(reverse=True)
top5 = l[:5]
For a more performant solution, use a min-heap (thanks Thijs):
import heapq
top5 = heapq.nlargest(5, sequence)
answered Mar 8, 2011 14:57
ThomasThomas
166k46 gold badges342 silver badges455 bronze badges
5
With itertools you will obtain another generator object so in most of the cases you will need another step the take the first n elements.
There are least two simpler solutions (a little bit less efficient in terms of performance but very handy) to get the elements ready to use from a generator:
Using list comprehension:
first_n_elements = [generator.next() for i in range(n)]
Otherwise:
first_n_elements = list(generator)[:n]
Where n is the number of elements you want to take (e.g. n=5 for the first five elements).
Neuron
4,5684 gold badges32 silver badges53 bronze badges
answered Feb 7, 2015
11:17
G MG M
18.6k10 gold badges76 silver badges79 bronze badges
This should work
top5 = array[:5]
answered Mar 8, 2011 14:57
Bala RBala R
105k23 gold badges193 silver badges207 bronze badges
1
How do I merge the first two elements in a list Python?
To join specific list elements (e.g., with indices 0 , 2 , and 4 ) and return the joined string that’s the concatenation of all those, use the expression ”. join([lst[i] for i in [0, 2, 4]]) . The list comprehension statement creates a list consisting of elements lst[0] , lst[2] , and lst[4] .
How do you join elements in a list in Python?
To join specific list elements in Python:. Use list slicing to select the specific elements in the list.. Use the str. join() method to join the elements into a string.. Replace the list elements with the string..
How do you print the top 5 elements of a list in Python?
Method #1 : Using sorted() + lambda
The combination of above functionality can be used to perform this particular task. In this, we just employ sorted function with reverse flag true, and print the top N elements using list slicing.
How do you make a list with N elements in Python?
To create a list of n placeholder elements, multiply the list of a single placeholder element with n . For example, use [None] * 5 to create a list [None, None, None, None, None] with five elements None . You can then overwrite some elements with index assignments.
Tải thêm tài liệu liên quan đến nội dung bài viết Python join first n elements of list
programming
python
Array 1 Python
Slice list Python
Reply
7
0
Chia sẻ
Chia Sẻ Link Tải Python join first n elements of list miễn phí
Bạn vừa Read nội dung bài viết Với Một số hướng dẫn một cách rõ ràng hơn về Clip Python join first n elements of list tiên tiến và phát triển nhất và ShareLink Download Python join first n elements of list miễn phí.
Hỏi đáp vướng mắc về Python join first n elements of list
Nếu sau khi đọc nội dung bài viết Python join first n elements of list vẫn chưa hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Admin lý giải và hướng dẫn lại nha
#Python #join #elements #list