Python 3.2 split the string values in a list - python-3.x

I have a list like below:
rawinput = ['corp\\asre', 'corp\\banjar', 'corp\\bicknk', 'corp\\daniele']
I want to be able to do
users = []
users = rawinput.split(",")
print(users)
How do I do this in Python 3.2? Thanks.

What you have,
rawinput = ['corp\\asre', 'corp\\banjar', 'corp\\bicknk', 'corp\\daniele']
is a list of strings already. You can just iterate through it as a list. You don't need to split anything.
If you had something like this,
rawinput = "corp\\asre, corp\\banjar, corp\\bicknk, corp\\daniele"
rawinput.split(',') would return the above list.

split() is applied on string, in return it gives you a list[] which contains the substring as elements in order of the parent string.
In your case:
input = "corp\\asre, corp\\banjar, corp\\bicknk, corp\\daniele"
input.split(',')
will return
['corp\\asre', 'corp\\banjar', 'corp\\bicknk', 'corp\\daniele']

Related

Appending strings with parentheses in a list

I have a set of strings.
people = {'RAM_S', 'SHYAM', 'GEORGEY', 'MUFASSIR'}
I have an empty list : list_c = [].
I want to append items in people to list_c such that resulting list looks as follows:
list_c = [('RAM_S'),('SHYAM'),('GEORGEY'),('MUFASSIR')]
I'm unable to append elements with parentheses in the list.
Please suggest some way to do it.
If you need tuples:
result = [tuple([i]) for i in people]
OUTPUT:
[('MUFASSIR',), ('SHYAM',), ('GEORGEY',), ('RAM_S',)]

sort a list based on integer returns wrong sort

I am trying to sort a list that contain in each index an integer and a string. like the one in the example.
I used sort() and split but I get always the wrong ordered that I expect
def takeSecond(elem):
return elem.split("|")[2]
list = ['|val1: 0|0','|val: 0|80','|val1 0|140','|val1: 0|20','|val1: 0|90']
list.sort(key=takeSecond)
print(list)
that returns:
['|val1: 0|90','|val: 0|80','|val1: 0|20','|val1: 0|0','|val1 0|140']
and I expect to get this:
['|val1: 0|140','|val: 0|90','|val1: 0|80','|val1: 20|0','|val1 0|0']
Where is my mistake in here?
Try this:
l = ['|val1: 0|0','|val: 0|80','|val1 0|140','|val1: 0|20','|val1: 0|90']
l.sort(key=lambda x:int(x.rsplit('|')[-1]), reverse=True)
This will sort your list based on what you need. and the expected output is:
In [18]: l
Out[18]: ['|val1 0|140', '|val1: 0|90', '|val: 0|80', '|val1: 0|20', '|val1: 0|0']
In addition note that:
Do not use list as a variable name. list is a built-in name in python, you will override its functionality .

retain only 1 item in a list per unique prefix

I have an example situation where I have a list as follows:
test = ['a-nyc','a-chi','b-sf','c-dal','a-phx','c-la']
the items in this list are naturally ordered in some way, and the objective is to keep the first encountered value for each prefix, e.g. the desired result is a list as follows:
['a-nyc', 'b-sf', 'c-dal']
is there a handy way of doing this?
looks like this can be done this way:
newl = []
prel = []
for i in range(len(test)):
if test[i].split('-')[0] not in prel:
newl.append(test[i])
else:
pass
prel.append(test[i].split('-')[0])
but not sure if there is a more pythonic solution
Yes, you can try like following also:
test = ['a-nyc','a-chi','b-sf','c-dal','a-phx','c-la']
prefix = []
newlist = []
for i in test:
if i.split('-')[0] not in prefix:
prefix.append(i.split('-')[0])
newlist.append(i)
print(newlist)
In this, if any query then let me know.
Thank you.

Dictionary to custom string format

Right, I have a dictionary like this one:
my_dict = {'BAM': (1.985, 1.919), 'PLN': (4.509, 4.361),'SEK': (9.929, 9.609), 'CZK': (27.544, 26.544),
'NOK': (9.2471, 8.9071), 'AUD': (1.4444, 1.4004),
'HUF': (315.89, 307.09), 'GBP': (0.8639, 0.8399),
'HRK': (7.6508, 7.4208), 'RUB': (71.9393, 66.5393),
'USD': (1.0748, 1.0508), 'MKD': (62.11, 60.29),
'CHF': (1.0942, 1.0602), 'JPY': (121.83, 118.03),
'BGN': (1.979, 1.925), 'RSD': (124.94, 121.14),
'DKK': (7.5521, 7.3281), 'CAD': (1.4528, 1.4048)}
I need to write a function (my_dict, ["GBP", "USD", "RUB", "HRK", "HUF"]) that returns this:
GBP......0.8639......0.8399
USD......1.0748......1.0508
RUB.....71.9393.....66.5393
HRK......7.6508......7.4208
HUF....315.8900....307.0900
We are learning how to format string, and I have no idea how to approach this. Any help would be appreciated. It needs to be formatted exactly like this, with all the dots and stuff (without the empty space before GBP).
One of the simpler ways to do what you're asking is to use a
list comprehension:
def my_function(dict, list):
return ["{0}......{1}......{2}".format(item, dict[item][0], dict[item][1])
for item in list
if item in dict]
my_function will return a list of currency......value......value items. In fact, you don't even need to create a function:
strings = ["{0}......{1}......{2}".format(item, dict[item][0], dict[item][1])
for item in list
if item in dict]
If however you don't want to use a list comprehension, the same function could look like this:
def my_function(dict, list):
strings = []
for item in list:
if item in dict:
strings.append("{0}......{1}......{2}".format(item, dict[item][0], dict[item][1]))
return strings

How to assign value to variable from a string split in groovy?

I want to assign the array item into variable directly using groovy like this:
def str = "xyz=abc"
def [name, value] = str.split("=")
but groovy doesn't like it. Is there a way to do that (not storing the array result and get the index[0], index[1] from it?).
Thanks,
You just need parenthesis instead of brackets:
def str = "xyz=abc"
def (name, value) = str.split("=")
Note that you'll need to know how many elements you're expecting or you'll have unexpected results.
def name, value
(name,value) = str.split("=")
You just need to do your definition before your multiple assignment.

Resources