Mix list comprehension on character split and int() conversion...? - python

Given following variable:
fsw="M543x620S30006482x483S14c10520x483S14c51498x537S14c39492x593S20500496x582S22a04494x564"
if I do this:
z=[sub.split('x') for sub in re.findall("\d{3}x\d{3}",fsw[8:])]
it returns :
[['482', '483'], ['520', '483'], ['498', '537'], ['492', '593'], ['496', '582'], ['494', '564']]
but I'd like to get a list of pairs of integers ([[482,483],[520,483],...]). Is there a one-liner that would do this operation ?
Thanks.

z=[map(lambda x: int(x), sub.split('x')) for sub in re.findall("\d{3}x\d{3}",fsw[8:])]

Related

How to sort a list by integer between two values?

I have the following list:
['RC103_', 'RC109_', 'RC114_', 'RC115_', 'RC111_', 'RC100_', 'RC117_', 'RC104_', 'RC122_', 'RC120_', 'RC101_', 'RC121_', 'RC125_', 'RC116_', 'RC118_', 'RC119_', 'RC102_', 'RC129_', 'RC126_', 'RC12_4']
If I try to sort this list, I get this output:
['RC100_', 'RC101_', 'RC102_', 'RC103_', 'RC104_', 'RC109_', 'RC111_', 'RC114_', 'RC115_', 'RC116_', 'RC117_', 'RC118_', 'RC119_', 'RC120_', 'RC121_', 'RC122_', 'RC125_', 'RC126_', 'RC129_', 'RC12_4']
How can I sort this list so that RC12_4 is at the top? To be clear, I want this:
['RC12_4', 'RC100_', 'RC101_', 'RC102_', 'RC103_', 'RC104_', 'RC109_', 'RC111_', 'RC114_', 'RC115_', 'RC116_', 'RC117_', 'RC118_', 'RC119_', 'RC120_', 'RC121_', 'RC122_', 'RC125_', 'RC126_', 'RC129_']
What I want to do is sort by the value between 'RC' and the underscore. I know that to sort by, for instance, the first group created by '_' is to use:
sorted(listName, key=lambda x: x.split('_')[0])
Is there a way to modify this script so that it sorts by the first item after RC and before the underscore? Or is there an easier way?
Also I have tried the natural keys approach and it didn't work for me.
Convert it to int:
sorted(listName, key=lambda x: int(x.split('_')[0][2:]))

How can i define a function that reads and returns all values in the list using Python?

I have this string delimited by commas.
'1.0,5.0,6.0,7.0,8.0,9.0'
def var():
for i in listnumbers:
return i +'.0'
When I do
var()
I only get
1.0
How do i get the result to include all the numbers in a loop?
1.0,5.0,6.0,7.0,8.0,9.0
def myfun(mycsv):
return [i+'.0' for i in mycsv.split(',')]
print(myfun('1.0,5.0,6.0,7.0,8.0,9.0'))
#['1.0.0', '5.0.0', '6.0.0', '7.0.0', '8.0.0', '9.0.0']
If you want a string, then just use join:
print(','.join(myfun('1.0,5.0,6.0,7.0,8.0,9.0')))
Or change the function to return a string;
return ','.join([i+'.0' for i in mycsv.split(',')])
You are returning inside the for loop, before the cycle is completed.
If I understood correctly your question, it looks like what you're looking for is list comprehension.
If your input is a list:
def var(l):
return [i + '.0' for i in l]
If your input is a string, like it seems from your description, you have to split it first:
def var(l):
return [i + '.0' for i in l.split(',')]
This is equivalent to mapping in other languages.
You can divide your string in a list using string.split(',') the you iterate over the freshly created list and print each element. A the code can be arranged like this:
for s in string.split(','):
print(s+'.0')

Change a list separator delimiter to another (python)

I need help using Python.
Supposing I have the list [22,23,45].
Is it possible to get an output like this: [22;23:45] ?
It's possible to change the delimiters if you display your list as a string. You can then use the join method. The following example will display your list with ; as a delimiter:
print(";".join(my_list))
This will only work if your list's items are string, by the way.
Even if you have more than one item
str(your_list[:1][0])+";" + ":".join(map(str,your_list[1:])) #'22;23:45'
Not sure why you want to wrap in the list but if you do just wrap around the above string in list()
my list which was [22,23,45] returned [;2;2;,; ;2;3;,; ;4;5;,] for both methods.
To bring more information, I have a variable:
ID= [elements['id'] for elements in country]
Using a print (ID), I get [22,23,45] so I suppose that the list is already to this form.
Problem is: I need another delimiters because [22,23,45] corresponds to ['EU, UK', 'EU, Italy', 'USA, California'].
The output I wish is [22,23,45] --> ['EU, UK'; 'EU, Italy'; 'USA, California']
I don't know if it's clearer but hope it could help
Try this I don't know exactly what do You want?
first solution:
list = [22,23,45]
str = ""
for i in list:
str += "{}{}".format(i, ";")
new_list=str[:-len(";")]
print(new_list)
and this is second solution
list = [22,23,45]
print(list)
list=str(list)
list=list.split(",")
list=";".join(list)
print(list)

Keep only two part on element within a python list

I'm looking for a commande in python in order to only keep within a list the 3 first letters between content_content > con_con
here is an example:
list_exp=["Babylona_expetiendra","Ocracylus_machabrus","Ojeris_multifasciatus"]
list_exp=["Bab_exp","Ocr_mac","Oje_mul"]
Does anyone have an idea? Thank you for your help .
You can use a list-comprehension:
['_'.join(map(lambda x: x[:3], x.split('_'))) for x in list_exp]
Code:
list_exp=["Babylona_expetiendra","Ocracylus_machabrus","Ojeris_multifasciatus"]
print(['_'.join(map(lambda x: x[:3], x.split('_'))) for x in list_exp])
# ['Bab_exp', 'Ocr_mac', 'Oje_mul']
[
*map(
lambda strip:'_'.join([st[:3] for st in strip]),
[
*map(
lambda s:s.split('_'),
["Babylona_expetiendra","Ocracylus_machabrus","Ojeris_multifasciatus"]
)
]
)
]
mess explanation:
First we are splitting every string in list by '_' gigving us
[['Babylona', 'expetiendra'], ['Ocracylus', 'machabrus'], ['Ojeris', 'multifasciatus']]
Then we are getting first 3 letters using [:3] for every string inside new lists
Finnaly joining again with '_'.join()
['Bab_exp', 'Ocr_mac', 'Oje_mul']
This example using map unpacking and lamdas
You can try like this.
Before running all these, just have a quick look at the use of list comprehension & join(), split() methods defined on strings.
>>> list_exp = ["Babylona_expetiendra","Ocracylus_machabrus","Ojeris_multifasciatus"]
>>>
>>> output = ['_'.join([part[:3] for part in name.split("_")]) for name in list_exp]
>>> output
['Bab_exp', 'Ocr_mac', 'Oje_mul']
>>>

PySpark how to convert an rdd to string

I need to pass coordinates in an url but I need to convert the rdd to a string and separate with a semicolon.
all_coord_iso_rdd.take(4)
[(-73.57534790039062, 45.5311393737793),
(-73.574951171875, 45.529457092285156),
(-73.5749282836914, 45.52922821044922),
(-73.57501220703125, 45.52901077270508)]
type(all_coord_iso_rdd)
pyspark.rdd.PipelinedRDD
Results lookin for:
"-73.57534790039062,45.5311393737793;-73.574951171875,45.529457092285156,
-73.5749282836914,45.52922821044922;-73.57501220703125,45.52901077270508"
The form of my URL should be as follows:
http://127.0.0.1/match/v1/driving/-73.57534790039062,45.5311393737793; -73.574951171875,45.529457092285156,-73.5749282836914,45.52922821044922;-73.57501220703125,45.52901077270508
From the snippet you posted all_coord_iso_rdd is an rdd, where each row is a tuple(float, float). Calling take(n) returns n records from the rdd.
x = all_coord_iso_rdd.take(4)
print(x)
#[(-73.57534790039062, 45.5311393737793),
# (-73.574951171875, 45.529457092285156),
# (-73.5749282836914, 45.52922821044922),
# (-73.57501220703125, 45.52901077270508)]
The value returned is simply a list of tuples of floating point numbers. To convert it into the desired format, we can use str.join inside of a list comprehension.
First, you need to convert the floats to str and then we can join the values in each tuple using a ",". We use map(str, ...) to map each value to a str.
This yields:
print([",".join(map(str, item)) for item in x])
#['-73.5753479004,45.5311393738',
# '-73.5749511719,45.5294570923',
# '-73.5749282837,45.5292282104',
# '-73.575012207,45.5290107727']
Finally join the resultant list using ";" to get your desired output.
print(";".join([",".join(map(str, item)) for item in x]))
Here is a pure spark way of doing the same (may be useful for larger
rdds/different use cases):
list=[(-73.57534790039062, 45.5311393737793),(-73.574951171875, 45.529457092285156),\
(-73.5749282836914, 45.52922821044922),(-73.57501220703125, 45.52901077270508)]
rdd=sc.parallelize(list)
rdd.map(lambda row: ",".join([str(elt) for elt in row]))\
.reduce(lambda x,y: ";".join([x,y]))

Categories