Mobin
Mobin

Reputation: 13

How to split a list In the form of array?

I want to separate the arrays that were paired in the list one by one.

For example see the below list:

data = [
    'id_code:3211238576;birth_year:1350;born_in:Boushehr',
    'id_code:9801233575;born_in:Argentina;birth_year:1360',
    'born_in:Portugal;id_code:0219206431;birth_year:1358',
    'id_code:0021678913;born_in:Shiraz;birth_year:1120',
    'id_code:1101102135;born_in:Gilan;birth_year:1152',

]

I want the list to be separated as follows:

data = [
    'id_code:3211238576','birth_year:1350',
    'born_in:Boushehr','id_code:9801233575','born_in:Argentina',
    'birth_year:1360','born_in:Portugal','id_code:0219206431',
    'birth_year:1358','id_code:0021678913','born_in:Shiraz',
    'birth_year:1120','id_code:1101102135','born_in:Gilan','birth_year:1152'
]

my code for this work:

m = [','.join(sorted(i.split(';'))) for i in data]
print(m)

My output code(wrong answer):

['birth_year:1350,born_in:Boushehr,id_code:3211238576', 'birth_year:1360,born_in:Argentina,id_code:9801233575', 'birth_year:1358,born_in:Portugal,id_code:0219206431', 'birth_year:1120,born_in:Shiraz,id_code:0021678913', 'birth_year:1152,born_in:Gilan,id_code:1101102135']

The code I wrote gives the wrong output, please help me correct the output

Upvotes: 0

Views: 76

Answers (6)

Yazan Ahmed
Yazan Ahmed

Reputation: 23

here you go

data = [
    'id_code:3211238576;birth_year:1350;born_in:Boushehr',
    'id_code:9801233575;born_in:Argentina;birth_year:1360',
    'born_in:Portugal;id_code:0219206431;birth_year:1358',
    'id_code:0021678913;born_in:Shiraz;birth_year:1120',
    'id_code:1101102135;born_in:Gilan;birth_year:1152',
]
w=[]
for i in data : 
    x=i.split(';')
    for e in x:
        w.append(e)

print(w)

and you can use replace function insted if the nested loop

for i in data:
w += i.replace(';',' ').split()

Upvotes: 0

Alex028502
Alex028502

Reputation: 3814

I think the problem is the ",".join part. Without it, you are almost there:

>>> [sorted(i.split(';')) for i in data]
[['birth_year:1350', 'born_in:Boushehr', 'id_code:3211238576'], ['birth_year:1360', 'born_in:Argentina', 'id_code:9801233575'], ['birth_year:1358', 'born_in:Portugal', 'id_code:0219206431'], ['birth_year:1120', 'born_in:Shiraz', 'id_code:0021678913'], ['birth_year:1152', 'born_in:Gilan', 'id_code:1101102135']]

but then the join takes each of the lists within a list and turns it into a string...

What you really want to do is turn them into one big list

First, we can remove the sorted from what we have, because sorting each sublist doesn't really help us:

>>> [i.split(';') for i in data]
[['id_code:3211238576', 'birth_year:1350', 'born_in:Boushehr'], ['id_code:9801233575', 'born_in:Argentina', 'birth_year:1360'], ['born_in:Portugal', 'id_code:0219206431', 'birth_year:1358'], ['id_code:0021678913', 'born_in:Shiraz', 'birth_year:1120'], ['id_code:1101102135', 'born_in:Gilan', 'birth_year:1152']]

Now to combine them into a single list:

I would start with an old fashioned loop in this case before trying to compress it:

list_of_lists = [i.split(';') for i in data]
final_list = []
for partial_list in list_of_lists:
  final_list += partial_list # add each set of entries to the new full list
print(final_list)

and then maybe sort it in place

final_list.sort()
print(final_list)

the other ways that don't modify variables or add items to existing list variables are much nicer, but when I am stuck, I usually attack it the old fashioned way, and then wrap everything in a function to isolate all my temporary variables from the rest of the program.

Upvotes: 0

Deepak Tatyaji Ahire
Deepak Tatyaji Ahire

Reputation: 5309

You can make use of chain.iterable() to flatten the list as follows:

from itertools import chain

data = [
    'id_code:3211238576;birth_year:1350;born_in:Boushehr',
    'id_code:9801233575;born_in:Argentina;birth_year:1360',
    'born_in:Portugal;id_code:0219206431;birth_year:1358',
    'id_code:0021678913;born_in:Shiraz;birth_year:1120',
    'id_code:1101102135;born_in:Gilan;birth_year:1152',
]

m = list(chain.from_iterable([i.split(';') for i in data]))
print(m)

Output:

['id_code:3211238576', 'birth_year:1350', 'born_in:Boushehr', 'id_code:9801233575', 'born_in:Argentina', 'birth_year:1360', 'born_in:Portugal', 'id_code:0219206431', 'birth_year:1358', 'id_code:0021678913', 'born_in:Shiraz', 'birth_year:1120', 'id_code:1101102135', 'born_in:Gilan', 'birth_year:1152']

Upvotes: 0

RBB
RBB

Reputation: 409

Why use join? When you call join it returns a string that contains ",", not a list. I believe this is what you want

    res = list()
    for i in data:
        res.extend(i.split(";"))

Upvotes: 0

Tom Kuntz
Tom Kuntz

Reputation: 65

It doesn't work because when you just replace ";" by ","... You should do something like this :

data1 = [] # New list
for i in data :
   x = i.split(';')
   for e in x :
       data1 += [e]

The problem is that you split your string to join it again in one piece āš 

(zanga has proposed a version with list comprehension but it is the same method) šŸ‘

Upvotes: 0

zanga
zanga

Reputation: 719

You may want to use list comprehension:

First split your strings in list of strings:

t = [i.split(';') for i in data]

Then flatten your lists

res = [item for sublist in t for item in sublist]

Upvotes: 1

Related Questions