मुख्य सामग्री पर जाएँ

Python में लिस्ट को कैसे स्प्लिट करें: बेसिक उदाहरण और एडवांस्ड तरीक़े

Slicing, list comprehensions, और itertools जैसी तकनीकों से Python लिस्ट को स्प्लिट करना सीखें। डेटा हैंडलिंग को बेहतर करने के लिए हर विधि कब उपयोगी है, यह जानें।
· 11 मि॰ पढ़ें

AI के साथ खोजें

Python में लिस्ट एक dynamic, mutable array डेटा टाइप है जिसमें क्रमबद्ध आइटम्स स्टोर किए जाते हैं। लिस्ट को स्प्लिट करना एक आम कार्य है, जो डेटा को कुशलता से संशोधित और विश्लेषित करने के लिए आवश्यक है. 

आप Data Analyst with Python करियर ट्रैक करके डेटा विश्लेषण के बारे में और सीख सकते हैं, जिसमें Python में लिस्ट को स्प्लिट करने के विभिन्न तरीकों, व्यावहारिक उदाहरणों और सर्वोत्तम प्रथाओं पर गहराई से चर्चा की गई है. 

इन तकनीकों में महारत हासिल करने से आपके कोडिंग कौशल बेहतर होंगे और आपकी स्क्रिप्ट्स अधिक कुशल और अनुरक्षित रहेंगी। आइए शुरू करें।

त्वरित उत्तर: Python में लिस्ट को कैसे स्प्लिट करें

  • Python में लिस्ट को स्प्लिट करने का सबसे आसान तरीका : ऑपरेटर के साथ slicing है। उदाहरण के लिए, हम लिस्ट को इस तरह स्प्लिट कर सकते हैं: split_list = my_list[:5], जो लिस्ट को पाँचवें इंडेक्स पर विभाजित करता है. 

  • इस लेख के बाकी हिस्सों में लिस्ट स्प्लिट करने के अन्य तरीकों जैसे list comprehensions, itertools, numpy, आदि की पड़ताल की गई है। हर विधि के अपने फायदे हैं और अलग-अलग स्थितियों के लिए उपयुक्त हैं, जैसा कि आप देखेंगे।

सबसे आम विधि: slicing से लिस्ट स्प्लिट करना

Python split list at index संभवतः सबसे आम तकनीक है। slicing विधि से निर्दिष्ट इंडेक्स पर Python लिस्ट उपलिस्टों में विभाजित हो जाती है। नीचे Python में slicing लागू करने के चरण दिए गए हैं।

  • लिस्ट परिभाषित करें: मान लीजिए आपके पास [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] लिस्ट है और आप इसे किसी विशेष इंडेक्स n पर स्प्लिट करना चाहते हैं।
  • स्प्लिट इंडेक्स तय करें: वह इंडेक्स n बताएँ जहाँ आप लिस्ट को slice करना चाहते हैं।
  • पहला slice बनाएँ: नीचे दिए कोड में, first_slice में लिस्ट की शुरुआत से लेकर इंडेक्स n (excluding) तक के तत्व होंगे।
  • दूसरा slice बनाएँ: second_slice में इंडेक्स n से लिस्ट के अंत तक के सभी तत्व होंगे।
# Define a list of integers from 1 to 10
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Index where the list will be split
n = 4

# Slice the list from the beginning to the nth index (not inclusive)
first_slice = my_list[:n]

# Slice the list from the nth index to the end
second_slice = my_list[n:]

# Print the first slice of the list
print("First slice:", first_slice)

# Print the second slice of the list
print("Second slice:", second_slice)

# Expected output:
# First slice: [1, 2, 3, 4]
# Second slice: [5, 6, 7, 8, 9, 10]

Python में लिस्ट स्प्लिट करना समझें

Python में list slicing का मतलब मुख्य लिस्ट से किसी उपलिस्ट या उपलिस्टों को निकालना होता है। जब आप लिस्ट को स्प्लिट करना चाहते हैं, तो उचित उपलिस्ट पाने के लिए slicing सिंटैक्स समझना ज़रूरी है। slicing सिंटैक्स है slice[start:stop:step]।

  1. Start slicing शुरू होने वाले इंडेक्स को दर्शाता है।
  2. Stop slicing के समाप्त होने वाले इंडेक्स को दर्शाता है।
  3. Step slicing के बीच का अंतराल दर्शाता है (डिफ़ॉल्ट 1)।
# Define a list of integers from 1 to 10
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Slice the list from index 2 to 7 (index 2 inclusive, index 7 exclusive)
# This will include elements at index 2, 3, 4, 5, and 6
sublist = my_list[2:7]

# Print the sliced sublist
print(sublist) 

# Expected output: [3, 4, 5, 6, 7]

ऊपर दिए कोड के अनुसार, slicing इंडेक्स 2 से 6 तक होती है क्योंकि उपलिस्ट में इंडेक्स 7 शामिल नहीं है।

Python में लिस्ट स्प्लिट करने के फायदे, कोड रीडेबिलिटी और दक्षता के संदर्भ में, इस प्रकार हैं:

  • डेटा प्रबंधन को सरल बनाना: डेटा को छोटे हिस्सों में बाँटने से डेटा वैज्ञानिक मशीन लर्निंग में training और testing डेटा को आसानी से अलग कर सकते हैं।
  • मॉड्यूलैरिटी बढ़ाना: विशिष्ट उपलिस्टों के साथ फ़ंक्शन लिखने से आपके कोड में मॉड्यूलैरिटी बढ़ती है।
  • प्रदर्शन का अनुकूलन: छोटी लिस्टों पर काम करना पूरी लिस्ट की तुलना में तेज़ होता है।
  • मेमोरी दक्षता बढ़ाना: यह विधि आधारभूत डेटा की कॉपी बनाए बिना हिस्से बनाती है, जिससे मेमोरी की बचत होती है।

Python में लिस्ट स्प्लिट करने की विभिन्न तकनीकें

Python में लिस्ट स्प्लिट करने के कई तरीके हैं। नीचे कुछ सामान्य उदाहरण दिए गए हैं जिनसे डेटा प्रैक्टिशनर अक्सर रूबरू होते हैं।

List slicing का उपयोग

Python split list slicing में : ऑपरेटर से इंडेक्स पर लिस्ट को स्प्लिट किया जाता है। यह विधि slicing की शुरुआत और अंत तय करने के लिए indexing का उपयोग करती है।

Slicing की अलग-अलग तकनीकें इस प्रकार हैं:

पॉज़िटिव इंडेक्स slicing

यह विधि बाएँ से दाएँ चलते हुए पॉज़िटिव इंडेक्स से लिस्ट के तत्वों तक पहुँचती है।

# Define a list of integers from 1 to 10
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Slice the list from index 2 to 7 (index 2 inclusive, index 7 exclusive)
# This will include elements at index 2, 3, 4, 5, and 6
sublist = my_list[2:7]

# Print the sliced sublist
print(sublist) 

# Expected output: [3, 4, 5, 6, 7]

नेगेटिव इंडेक्स slicing

यह Python split list विधि नेगेटिव इंडेक्स का उपयोग करती है और लिस्ट के आख़िरी तत्व से दाएँ से बाएँ की ओर गिनती करती है।

# Define a list of integers from 1 to 10
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Slice the list to get the last 5 elements
sublist = my_list[-5:]

# Print the sliced sublist containing the last 5 elements
print(sublist)

# Slice the list to get all elements except the last 3
sublist = my_list[:-3]

# Print the sliced sublist containing all but the last 3 elements
print(sublist)

# Expected output:
# [6, 7, 8, 9, 10]
# [1, 2, 3, 4, 5, 6, 7]

Step का उपयोग

यह तरीका अलग-अलग तत्वों तक पहुँचते समय विशेष शर्तों के साथ लिस्ट को स्प्लिट करता है।

# Define a list of integers from 1 to 10
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Slice the list to get every second element, starting from the beginning
sublist = my_list[::2]

# Print the sliced sublist containing every second element
print(sublist) 

# Slice the list to get every second element, starting from index 1
sublist = my_list[1::2]

# Print the sliced sublist containing every second element, starting from index 1
print(sublist) 

# Slice the list to get the elements in reverse order
sublist = my_list[::-1]

# Print the sliced sublist containing the elements in reverse order
print(sublist) 

# Expected output:
# [1, 3, 5, 7, 9]
# [2, 4, 6, 8, 10]
# [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

इंडेक्स छोड़ना

यह विधि केवल आवश्यक तत्व लौटा कर Python लिस्ट को slice करती है।

# Define a list of integers from 1 to 10
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Slice the list from the start to index 5 (index 5 not inclusive)
sublist = my_list[:5]

# Print the sliced sublist containing elements from the start to index 4
print(sublist) 

# Slice the list from index 5 to the end
sublist = my_list[5:]

# Print the sliced sublist containing elements from index 5 to the end
print(sublist) 

# Slice the list to get the entire list
sublist = my_list[:]

# Print the sliced sublist containing the entire list
print(sublist) 

# Expected output:
# [1, 2, 3, 4, 5]
# [6, 7, 8, 9, 10]
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

List comprehensions का उपयोग

Python list comprehension से मौजूदा लिस्ट के मानों के आधार पर लिस्ट को हिस्सों में बाँटा जा सकता है।

# Define a function to split a list into chunks of a specified size
def split_list(lst, chunk_size):
    # Use a list comprehension to create chunks
    # For each index 'i' in the range from 0 to the length of the list with step 'chunk_size'
    # Slice the list from index 'i' to 'i + chunk_size'
    return [lst[i:i + chunk_size] for i in range(0, len(lst), chunk_size)]

# Define a list of integers from 1 to 15
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

# Split the list into chunks of size 3 using the split_list function
chunks = split_list(my_list, 3)

# Print the resulting list of chunks
print(chunks)

# Expected output:
# [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]]

ऊपर दिया कोड Python split list into n chunks के रूप में तीन-तीन के हिस्सों में बाँटकर नई लिस्ट लौटाता है, जिसमें हर chunk में 3 मान हैं।

Itertools का उपयोग

Python में itertools का उपयोग करके लिस्ट स्प्लिट करने में iteration के ज़रिये डेटा रूपांतरित किया जाता है।

# Import the islice function from the itertools module
from itertools import islice
# Define a function to yield chunks of a specified size from an iterable
def chunks(iterable, size):
    # Create an iterator from the input iterable
    iterator = iter(iterable)
    
    # Loop over the iterator, taking the first element in each iteration
    for first in iterator:
        # Yield a list consisting of the first element and the next 'size-1' elements from the iterator
        yield [first] + list(islice(iterator, size - 1))

# Define a list of integers from 1 to 15
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

# Convert the generator returned by the chunks function into a list of chunks
chunked_list = list(chunks(my_list, 3))

# Print the resulting list of chunks
print(chunked_list)

# Expected output:
# [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]]

numpy का उपयोग

Python की numpy लाइब्रेरी arrays को उपलिस्टों में बाँटने के लिए उपयोगी है। .array_split() फ़ंक्शन इच्छित स्प्लिट की संख्या निर्दिष्ट करने पर विभाजन करने देता है।

# Import the numpy library and alias it as np
import numpy as np

# Define a list of integers from 1 to 15
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

# Use numpy's array_split function to split the list into 3 chunks
chunks = np.array_split(my_list, 3)

# Convert each chunk back to a regular list and print the resulting list of chunks
print([list(chunk) for chunk in chunks])

# Expected output:
# [[np.int32(1), np.int32(2), np.int32(3), np.int32(4), np.int32(5)], [np.int32(6), np.int32(7), np.int32(8), np.int32(9), np.int32(10)], [np.int32(11), np.int32(12), np.int32(13), np.int32(14), np.int32(15)]]

लिस्ट को हिस्सों में बाँटना

नीचे दिया Python फ़ंक्शन भी लिस्ट को हिस्सों में बाँटने देता है। ये हिस्से उपयोगकर्ता द्वारा चाही गई संख्या के अनुसार कस्टम हो सकते हैं।

# Define a function to split a list into chunks of a specified size
def split_into_chunks(lst, chunk_size):
    chunks = []  # Initialize an empty list to store chunks
    
    # Iterate over the list with a step of chunk_size
    for i in range(0, len(lst), chunk_size):
        # Slice the list from index 'i' to 'i + chunk_size' and append it to chunks
        chunks.append(lst[i:i + chunk_size])
    
    return chunks  # Return the list of chunks

# Define a list of integers from 1 to 16
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

# Split the list into chunks of size 4 using the split_into_chunks function
chunks = split_into_chunks(my_list, 4)

# Print the resulting list of chunks
print(chunks)

# Expected output:
# [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

शर्तों के आधार पर लिस्ट स्प्लिट करना

जब आप Python में लिस्ट को स्प्लिट करना चाहते हैं, तो उपलिस्ट के लिए कुछ नियम/शर्तें तय कर सकते हैं। इस विधि से आप केवल वे उपलिस्ट बनाते हैं जो इन शर्तों को पूरा करती हैं।

# Define a list of integers from 1 to 15
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

# List comprehension to filter elements divisible by 3
div_3 = [x for x in my_list if x % 3 == 0]

# List comprehension to filter elements not divisible by 3
not_div_3 = [x for x in my_list if x % 3 != 0]

# Print the list of elements divisible by 3
print(div_3)

# Print the list of elements not divisible by 3
print(not_div_3)

# Expected output:
# [3, 6, 9, 12, 15]
# [1, 2, 4, 5, 7, 8, 10, 11, 13, 14]

for loops का उपयोग

For loops का उपयोग भी शर्तों के आधार पर और iteration के माध्यम से Python में लिस्ट को स्प्लिट करने के लिए किया जा सकता है।

# Define a function to split a list into sub-lists of size n
def split_by_n(lst, n):
    # Use a list comprehension to create sub-lists
    # For each index 'i' in the range from 0 to the length of the list with step 'n'
    # Slice the list from index 'i' to 'i + n'
    return [lst[i:i + n] for i in range(0, len(lst), n)]

# Define a list of integers from 1 to 15
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

# Split the list into sub-lists of size 5 using the split_by_n function
sub_lists = split_by_n(my_list, 5)

# Print the resulting list of sub-lists
print(sub_lists) 

# Expected output:
# [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]

zip() फ़ंक्शन का उपयोग

आप Python में zip() फ़ंक्शन का उपयोग करके पेयरिंग के ज़रिये भी लिस्ट को स्प्लिट कर सकते हैं।

# Define two lists: list1 containing integers from 1 to 10, and list2 containing letters from 'a' to 'j'
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

# Use the zip function to pair corresponding elements from list1 and list2 into tuples
paired = list(zip(list1, list2))

# Print the list of paired tuples
print(paired)

# Expected output:
# [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f'), (7, 'g'), (8, 'h'), (9, 'i'), (10, 'j')]

enumerate() फ़ंक्शन का उपयोग

enumerate() फ़ंक्शन इंडेक्स-आधारित शर्तों से Python लिस्ट को स्प्लिट करता है। यह फ़ंक्शन iteration के माध्यम से लिस्ट को बराबर हिस्सों में बाँटने में मदद करता है।

# Define a list of integers from 1 to 15
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

# Define the chunk size for splitting the list
chunk_size = 5

# Create a list of empty lists, one for each chunk
split_list = [[] for _ in range(chunk_size)]

# Iterate over the elements and their indices in my_list
for index, value in enumerate(my_list):
    # Calculate the index of the sublist where the current value should go using modulo operation
    sublist_index = index % chunk_size
    
    # Append the current value to the corresponding sublist in split_list
    split_list[sublist_index].append(value)

# Print the list of sublists after splitting my_list into chunks
print(split_list)

# Expected output:
# [[1, 6, 11], [2, 7, 12], [3, 8, 13], [4, 9, 14], [5, 10, 15]]

Python में स्ट्रिंग को लिस्ट में स्प्लिट करना

split() मेथड का उपयोग

split() मेथड किसी निर्दिष्ट delimiter के आधार पर Python में स्ट्रिंग को लिस्ट में बाँटता है। स्ट्रिंग में delimiter जितनी बार होगा, यह उतने सबस्ट्रिंग्स लौटाता है।

# Define a string
myString = "Goodmorning to you"

# Split the string into a list of words based on whitespace (default separator)
my_list = myString.split()

# Print the resulting list
print(my_list) 

# Expected output: ['Goodmorning', 'to', 'you']

splitlines() मेथड का उपयोग

splitlines() मेथड नई पंक्ति (newline) कैरेक्टर के आधार पर Python में स्ट्रिंग को लिस्ट में बाँटता है।

# Define a string with newline characters
myString = "Goodmorning\nto\nyou"

# Split the string into a list of lines based on newline characters
my_list = myString.splitlines()

# Print the resulting list
print(my_list)

# Expected output: ['Goodmorning', 'to', 'you']

partition() मेथड का उपयोग

partition() मेथड दिए गए सेपरेटर का उपयोग करके Python में स्ट्रिंग्स को तीन भागों में बाँटता है। यह सेपरेटर से पहले का भाग, स्वयं सेपरेटर, और सेपरेटर के बाद का भाग लौटाता है।

# Define a string with a delimiter '/'
myString = "Goodmorning/to you"

# Use the partition() method to split the string into three parts based on the first occurrence of '/'
my_list = myString.partition('/')

# Print the resulting tuple
print(my_list)

# Expected output: ('Goodmorning', '/', 'to you')

रेगुलर एक्सप्रेशंस का उपयोग

Python में रेगुलर एक्सप्रेशंस का उपयोग करके भी स्ट्रिंग को लिस्ट में बाँटा जा सकता है। नीचे whitespace के आधार पर ऐसा करने का उदाहरण है।

# Import the 're' module for regular expressions
import re

# Define a string
myString = "Goodmorning to you"

# Use re.split() to split the string based on whitespace ('\s' matches any whitespace character)
# Returns a list of substrings where the string has been split at each whitespace
my_list = re.split('\s', myString)

# Print the resulting list
print(my_list)

# Expected output: ['Goodmorning', 'to', 'you']

तुलनात्मक तालिका

यह तालिका एक नज़र में दिखाती है कि अलग-अलग तकनीकें कब उपयोगी हो सकती हैं। व्यावहारिक रूप से, कई मामलों में एक ही तकनीक बिना प्रदर्शन में बड़े फ़र्क के काम कर सकती है, इसलिए किसी विशेष तकनीक के “सबसे उपयोगी” होने के संकेत कुछ हद तक अतिरंजित लग सकते हैं. 

तकनीकउपयोग का मामलाकब उपयोगी है
Slicingकिसी विशेष इंडेक्स या इंडेक्स रेंज पर लिस्ट को स्प्लिट करना।सरल, सीधी स्प्लिट्स के लिए उपयोगी।
List Comprehensionsलिस्ट के मानों या शर्तों के आधार पर उपलिस्ट बनाना।जटिल, मानदंड-आधारित स्प्लिटिंग के लिए उपयोगी।
itertools.isliceइटरेटर का उपयोग करके तय आकार के हिस्सों में लिस्ट को बाँटना।बड़ी लिस्टों को कुशलता से संभालने के लिए उपयोगी।
numpy.array_splitवांछित स्प्लिट की संख्या के आधार पर arrays को उपलिस्टों में बाँटना।संख्यात्मक डेटा को कुशलता से संभालने के लिए उपयोगी।
शर्त-आधारित स्प्लिटिंगजैसे विभाज्यता जैसी शर्तों के आधार पर लिस्ट को बाँटना।डेटा को सार्थक उपलिस्टों में विभाजित करने के लिए उपयोगी।
For Loopsलिस्ट पर इटरेट करके तय आकार की उपलिस्टें बनाना।स्प्लिटिंग प्रक्रिया पर अधिक नियंत्रण के लिए उपयोगी।
zip() फ़ंक्शनदो लिस्टों के तत्वों को पेयर करना।दो संबंधित लिस्टों के डेटा को संयोजित करने के लिए उपयोगी।
enumerate() फ़ंक्शनइंडेक्स शर्तों के साथ लिस्टों को बाँटना, अक्सर बराबर आकार के हिस्सों में।समान रूप से वितरित उपलिस्टें बनाने के लिए उपयोगी।
split() मेथडनिर्दिष्ट delimiter के आधार पर स्ट्रिंग्स को लिस्टों में बाँटना।टेक्स्ट डेटा प्रोसेसिंग के लिए उपयोगी।
splitlines() मेथडन्यूलाइन कैरेक्टर्स के आधार पर स्ट्रिंग्स को लिस्टों में बाँटना।मल्टीलाइन टेक्स्ट डेटा पढ़ने में उपयोगी।
partition() मेथडनिर्दिष्ट सेपरेटर के आधार पर स्ट्रिंग्स को तीन भागों में बाँटना।विशिष्ट टेक्स्ट प्रोसेसिंग स्थितियों के लिए उपयोगी।
रेगुलर एक्सप्रेशंसजटिल पैटर्न के आधार पर regex से स्ट्रिंग्स को लिस्टों में बाँटना।एडवांस्ड टेक्स्ट डेटा मैनिपुलेशन के लिए उपयोगी।

आम गलतियाँ और उनसे कैसे बचें

Python में लिस्ट स्प्लिट करते समय डेटा प्रैक्टिशनर कुछ सामान्य त्रुटियों का सामना कर सकते हैं। नीचे आम गलतियाँ और उनसे बचने के तरीके दिए हैं।

Off-by-one त्रुटियाँ

Off-by-one त्रुटि बेसिक slicing के दौरान तब होती है जब आप लिस्ट में ज़रूरत से कम या ज़्यादा इंडेक्स शामिल कर लेते हैं।

my_list = [1, 2, 3, 4, 5]

# Trying to get sublist from index 2 to 4, inclusive
sublist = my_list[2:5]  # Correct: my_list[2:5]
print(sublist)

# Incorrect usage:
sublist = my_list[2:4]  # Incorrect, excludes index 4
print(sublist)

# Expected output:
# [3, 4, 5]
# [3, 4]

इस त्रुटि से बचने के लिए, Python में इंडेक्सिंग अनुक्रम को समझें और slicing में किन इंडेक्सों को शामिल/बहिष्कृत करना है, यह स्पष्ट रखें।

उपयोग किए गए पैकेजों से संबंधित त्रुटियाँ

आप पैकेजों से जुड़ी त्रुटियों का भी सामना कर सकते हैं, विशेषकर जब स्प्लिट विधि Python मॉड्यूल्स पर निर्भर करती है। उदाहरण के लिए numpy, re, और itertools। इस त्रुटि से बचने के लिए सुनिश्चित करें कि पैकेज सही तरह से लोड किए गए हैं और संगत संस्करणों का उपयोग हो रहा है।

Edge cases को संभालना

Python list split में edge cases तब होते हैं जब आप कुछ स्थितियों का ध्यान नहीं रखते। उदाहरण के लिए, नीचे दिया स्निपेट 5 के हिस्सों में स्प्लिट करने की कोशिश करता है जबकि लिस्ट में केवल 4 आइटम हैं।

import numpy as np

my_list = [1, 2, 3, 4]
chunks = np.array_split(my_list, 5)
print(chunks)

# Expected output: 
# [array([1]), array([2]), array([3]), array([4]), array([], dtype=int32)]

इस त्रुटि से बचने के लिए, नीचे दिखाए अनुसार edge case संभालने हेतु conditional statements का उपयोग करें।

import numpy as np

my_list = [1, 2, 3, 4]
if len(my_list) < 5:
    chunks = [my_list]
else:
    chunks = np.array_split(my_list, 5)
print(chunks)

# Expected output: [[1, 2, 3, 4]]

स्पेशल कैरेक्टर्स को संभालना

स्पेशल कैरेक्टर्स को ठीक से हैंडल न करना भी Python में स्ट्रिंग लिस्ट स्प्लिट करते समय त्रुटियाँ पैदा कर सकता है। इन विशेष कैरेक्टर्स में whitespace, कॉमा, या अल्फ़ा-न्यूमेरिक्स शामिल हो सकते हैं।

नीचे दिया उदाहरण दिखाता है कि स्ट्रिंग लिस्ट को किस कैरेक्टर पर स्प्लिट करना है, यह बताकर इस त्रुटि से कैसे बचा जा सकता है।

# Example list with special characters
my_list = ["apple, orange", "dog, mouse", "green, blue"]

# Splitting each string by the comma
split_list = [s.split(",") for s in my_list]
print(split_list)

# Expected output:
# [['apple', ' orange'], ['dog', ' mouse'], ['green', ' blue']]

सर्वोत्तम प्रथाएँ और दिशा-निर्देश

क्योंकि Python में लिस्ट स्प्लिट करना डेटा विश्लेषण में आम ऑपरेशन है, इसलिए दक्षता सुनिश्चित करने के लिए कुछ प्रथाओं का पालन करना महत्वपूर्ण है। सिफारिशें इस प्रकार हैं:

  • जहाँ संभव हो, immutability बनाए रखें: यदि मूल लिस्ट में बदलाव नहीं होना चाहिए, तो सुनिश्चित करें कि आपकी slicing से वह परिवर्तित न हो। Slicing नई लिस्ट बनाता है, इसलिए आम तौर पर यह समस्या नहीं होती, परंतु जटिल डेटा संरचनाओं में सतर्क रहें।
  • प्रदर्शन के लिए अनुकूलित करें: बड़ी लिस्टों के साथ काम करते समय प्रदर्शन पर ध्यान दें। Slicing सामान्यतः कुशल है, पर बड़ी लिस्टों की अनावश्यक कॉपी प्रदर्शन बाधाएँ बना सकती है।
  • Edge cases संभालें: खाली लिस्ट, लिस्ट की शुरुआत/अंत पर स्प्लिट करना, और अमान्य इंडेक्स जैसी स्थितियों पर विचार करें। सुनिश्चित करें कि आपका कोड इन्हें सुगमता से संभाल सके।
  • त्रुटि प्रबंधन और वैलिडेशन शामिल करें: जब आप Python में लिस्ट को कई लिस्टों में बाँटते हैं, तो error handling विशेष रूप से महत्वपूर्ण है ताकि आपका कोड अनपेक्षित रूप से न टूटे। 

DataCamp का Python Programming कोर्स error handling और प्रदर्शन जैसे सामान्य सिद्धांतों को ध्यान में रखते हुए कुशल कोड लिखने की सर्वोत्तम प्रथाओं पर विस्तृत जानकारी देता है. 

एडवांस्ड लिस्ट स्प्लिटिंग तकनीकें

लिस्ट स्प्लिटिंग को और उन्नत स्तर पर ले जाने के लिए ऐसी विधियों का उपयोग करें जो अधिक नियंत्रण और लचीलापन देती हैं. 

लिस्ट को कई लिस्टों में बाँटना

आप विशेष इंडेक्स पर स्प्लिट निर्दिष्ट करके Python लिस्ट को कई लिस्टों में बाँट सकते हैं। उदाहरण के लिए, नीचे का कोड लिस्ट को 2, 5, 7 इंडेक्स पर स्प्लिट करता है।

def split_at_indices(lst, indices):
    result = []
    prev_index = 0
    for index in indices:
        result.append(lst[prev_index:index])
        prev_index = index
    result.append(lst[prev_index:])
    return result

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
indices = [2, 5, 7]
split_lists = split_at_indices(my_list, indices)
print(split_lists)

# Expected output: [[1, 2], [3, 4, 5], [6, 7], [8, 9, 10]]

लिस्ट को बराबर हिस्सों में बाँटना

आप इन-बिल्ट लाइब्रेरी जैसे ‘numpy’ का उपयोग करके भी चाही गई संख्या के आधार पर Python लिस्ट को बराबर हिस्सों में बाँट सकते हैं।

import numpy as np

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
num_parts = 3
split_lists = np.array_split(my_list, num_parts)
print([list(arr) for arr in split_lists])

# Expected output:
# [[np.int32(1), np.int32(2), np.int32(3), np.int32(4), np.int32(5)], [np.int32(6), np.int32(7), np.int32(8), np.int32(9), np.int32(10)], [np.int32(11), np.int32(12), np.int32(13), np.int32(14), np.int32(15)]]

लिस्ट को आधा-आधा बाँटना

Slicing का उपयोग करके Python लिस्ट को दो बराबर हिस्सों में बाँटा जा सकता है।

def split_in_half(lst):
    # Calculate the midpoint index of the list
    mid_index = len(lst) // 2
    # Return the two halves of the list
    return lst[:mid_index], lst[mid_index:]

# Example list
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Split the list into two halves
first_half, second_half = split_in_half(my_list)
print(first_half)
print(second_half)

# Expected output:
# [1, 2, 3, 4, 5]
# [6, 7, 8, 9, 10]

यदि लिस्ट में तत्वों की संख्या विषम हो, तो आप पहले हिस्से में शामिल होने वाला मध्य इंडेक्स निर्दिष्ट कर सकते हैं।

def split_in_half(lst):
    # Calculate the midpoint index of the list, including the middle element in the first half if the length is odd
    mid_index = (len(lst) + 1) // 2
    # Return the two halves of the list
    return lst[:mid_index], lst[mid_index:]

# Example list
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Split the list into two halves, handling odd-length
first_half, second_half = split_in_half(my_list)
print(first_half) 
print(second_half) 

# Expected output:
# [1, 2, 3, 4, 5]
# [6, 7, 8, 9]

Python Developer ट्रैक भी उन्नत कोड लिखने की समझ देता है, जिससे आप एडवांस्ड लिस्ट-स्प्लिटिंग तकनीकों को बेहतर समझ सकें।

निष्कर्ष

Python में लिस्ट स्प्लिट करने के अलग-अलग तरीके हैं। हर विधि लिस्ट के प्रकार और उपयोगकर्ता की दक्षता संबंधी प्राथमिकताओं पर निर्भर करती है। एक डेटा प्रैक्टिशनर के रूप में, अपनी विश्लेषण आवश्यकताओं के अनुरूप उपयुक्त स्प्लिट विधि चुनना महत्वपूर्ण है।

Python split list की गहन समझ के लिए आप हमारा Python Fundamentals कोर्स देख सकते हैं। आप Introduction to Python कोर्स भी कर सकते हैं ताकि लिस्ट और अन्य डेटा टाइप्स पर ऑपरेशन में महारत हासिल हो। Python Cheat Sheet for Beginners भी Python में लिस्ट स्प्लिट करने की झलक पाने में सहायक है।

Allan Ouko

लेखक

Allan Ouko

डेटा एनालिटिक्स, बिज़नेस इंटेलिजेंस और डेटा साइंस में व्यावहारिक अनुभव रखने वाला डेटा साइंस तकनीकी लेखक। मैं SQL, Python, Power BI, Databricks और डेटा इंजीनियरिंग पर व्यावहारिक, उद्योग-केंद्रित सामग्री लिखता/लिखती हूँ, जो वास्तविक दुनिया की एनालिटिक्स कार्य से जुड़ी होती है। मेरी लेखनी तकनीकी गहराई और व्यावसायिक प्रभाव के बीच पुल का काम करती है, जिससे पेशेवरों को डेटा को आत्मविश्वासपूर्ण निर्णयों में बदलने में मदद मिलती है. 

Frequently Asked Questions

मुझे Python में लिस्ट कब स्प्लिट करनी चाहिए?

Python में लिस्ट स्प्लिट करना डेटा प्रोसेसिंग के लिए महत्वपूर्ण है, खासकर बड़े डेटासेट्स को संभालते समय। ऐसे ट्रांसफॉर्मेशंस विश्लेषण में मदद करते हैं, जैसे मशीन लर्निंग मॉडलों के लिए training और testing डेटा अलग करना।

अगर itertools और numpy मॉड्यूल त्रुटि दें तो मुझे क्या करना चाहिए?

सुनिश्चित करें कि आवश्यक Python लाइब्रेरीज़ इंस्टॉल हैं। यदि त्रुटि बनी रहती है, तो लाइब्रेरीज़ को अपडेट करें और संगत संस्करणों का उपयोग करें।

Python लिस्ट स्प्लिट और partition() में क्या अंतर है?

partition() मेथड तीन ट्यूपल्स लौटाती है, जिसमें शर्त वाला आर्ग्युमेंट बीच में होता है।

क्या मैं किसी विशेष delimiter या दिए गए मान से Python लिस्ट को स्प्लिट कर सकता/सकती हूँ?

लिस्ट को स्प्लिट करने के लिए आपको दिए गए delimiter या मान को निर्दिष्ट करना होगा।

जब मैं लिस्ट को कई लिस्टों में बाँटूँ तो कोड का प्रदर्शन कैसे बेहतर करूँ?

जब आप लिस्ट को कई लिस्टों में बाँटें, तो एक पुन:उपयोगी फ़ंक्शन का उपयोग करें। यह तरीका दक्षता सुनिश्चित करता है।

विषय