C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Python - Get Last Element after str.split()

By : user2174291
Date : October 18 2020, 08:10 PM
To fix the issue you can do Use a list comprehension to take the last element of each of the split strings:
code :
``````ids = [val[-1] for val in your_string.split()]
``````

Share :

## Python split list by one element

By : Mostafaa Refaat
Date : March 29 2020, 07:55 AM
Any of those help We can calculate the sum of all elements. Then we can easily for each element find the sum of the rest of the elements. We subtract from the total sum the value of the current element and then in order to find the average we divide by N - 1:
code :
``````s = sum(my_list)
for my_element in my_list:
avg_of_others = (s - my_element) / float(len(my_list) - 1)
...
``````
``````import numpy as np
l = np.array([(1, 2), (1, 3)])
m = np.array([(3, 1), (2, 4)])
my_list = [l, m]

s = sum(my_list)
for my_element in my_list:
avg_of_others = (s - my_element) / float(len(my_list) - 1)
``````

## Python: Split list in subsets if the current element is minor than previous element

By : Juan Carlos SC
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Here's a comprehension-ish approach. But while you could cram this into one long expression, why on Earth would you want to?
code :
``````>>> my_list = [1, 2, 3, 1, 2, 1, 2, 3]
>>> brks = [i for i in range(1,len(my_list)) if my_list[i] < my_list[i-1]]
>>> [my_list[x:y] for x,y in zip([0]+brks,brks+[None])]
[[1, 2, 3], [1, 2], [1, 2, 3]]
``````

## How to split each element of the RDD in spark with python?

By : Berkay Kara
Date : November 25 2020, 11:01 PM
will be helpful for those in need You should use flatMap() to get each word in RDD so you will get RDD[String]. try it as below
code :
``````val rdd=sc.textFile(filePath)
rdd.flatMap(line=>line.split(" "))
``````

## Split an element at whitespace in python 2

By : Yumic
Date : March 29 2020, 07:55 AM
With these it helps split doesn't mutate the string but returns a list of strings instead. If you call it without storing the result as in your example it won't do anything good. Just store the result of split to new list:
code :
``````>>> nums = ['1231  34  42 432', '12 345345 7686', '234234  45646 435']
>>> result = []
>>> for item in nums:
...     result.extend(item.split())
...
>>> result
['1231', '34', '42', '432', '12', '345345', '7686', '234234', '45646', '435']
``````
``````>>> [x for item in nums for x in item.split()]
['1231', '34', '42', '432', '12', '345345', '7686', '234234', '45646', '435']
``````

## Python Split if there is only one element

By : Arun Kumar
Date : March 29 2020, 07:55 AM
it should still fix some issue Use strip to normalize input, the problem is there is an extra ; for one number situation, so we should remove it first.
code :
``````In [1]: def lnum(s):
...:     return s.strip(';').split(';')[-1]
...:

In [2]: lnum('89;50;20')
Out[2]: '20'

In [3]: lnum('89;')
Out[3]: '89'

In [5]: lnum('10;')
Out[5]: '10'
``````