Reputation: 77
I am trying to change this list
['AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5',
'BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2',
'K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1']
to something that looks like this
AAAAA -- [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
BBB -- [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3, 2]
K -- [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]
i have tried multiple ways and cant figure it out
Upvotes: 1
Views: 103
Reputation: 4618
you can make a dict, and also use that dict to get your specific output if that's what you want:
assuming your list is called full_list
lists = [sub.split() for sub in full_list]
keys = [l[0] for l in lists]
vals = [list(map(int,l[1:])) for l in lists]
d = {k:v for k,v in zip(keys,vals)}
if desired to get that specific output:
for k,v in d.items():
print(f'{k} -- {v}')
output:
AAAAA -- [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
BBB -- [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3, 2]
K -- [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]
Upvotes: 1
Reputation: 700
inputlist=['AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5', 'BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2', 'K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1']
for item in inputlist:
item_to_list=item.split(" ")
temp_list=[int(i) for i in list(filter(None, item_to_list[2:]))]
print("{0} -- {1}".format(item_to_list[0],str(temp_list)))
Output:
AAAAA -- [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
BBB -- [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3,2]
K -- [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]
Upvotes: 2
Reputation: 499
x = ['AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5', 'BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2', 'K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1']
for i in x:
i = i.split(' ')
tmp = {i[0]:[int(items) for items in i[1:]]}
for i, j in tmp.items():
print(f"{i} - {j}")
Output:
AAAAA - [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
BBB - [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3, 2]
K - [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]
Upvotes: 1
Reputation: 7204
Try this:
name=["AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5",
"BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2",
"K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1"]
for item in name:
thelist = list(map(int, ','.join(item.split(' ')[1:]).split(',')))
print(f"{item.split(' ')[0]} -- {thelist}")
output:
AAAAA -- [4,2,1,2,4,2,4,4,5,2,2,1,5,2,4,3,1,1,3,3,5]
BBB -- [5,2,1,2,4,5,4,4,1,2,2,2,4,4,4,3,1,2,3,3,2]
K -- [4,1,2,1,2,1,2,5,1,1,1,1,4,2,2,1,5,1,3,4,1]
Upvotes: 1
Reputation: 15872
You just need to split the elements in each string, take the first element and set it as key of the dictionary, and convert the rest of the elements to integers, and store as values:
list_ = ['AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5',
'BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2',
'K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1']
dict_ = {}
for string in list_:
alpha, *numbers = string.split()
dict_[alpha] = [*map(int,numbers)]
for alpha, numbers in dict_.items():
print(f"{alpha} -- {numbers}")
Output:
AAAAA -- [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
BBB -- [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3, 2]
K -- [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]
If you want to go fancy:
generator_ = (f"{alpha} -- {[*map(int,numbers)]}" for alpha, *numbers in [l.split() for l in list_])
print(*generator_, sep = '\n')
Upvotes: 3
Reputation: 8122
If you want to reproduce what you asked in the question:
x = ['AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5', 'BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2', 'K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1']
for elem in x:
split = elem.split(" ")
print("{} -- {}".format(split[0],[int(i) for i in split[1:]]))
This:
x
split
Or using a dictionary:
x = ['AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5', 'BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2', 'K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1']
# Create dictionary following above logic
d = dict()
for elem in x:
split = elem.split(" ")
d.update({split[0] : [int(i) for i in split[1:]]})
# Loop through its keys and values and print as needed
for k, v in d.items():
print("{} -- {}".format(k, v))
K -- [4, 1, 2, 1, 2, 1, 2, 5, 1, 1, 1, 1, 4, 2, 2, 1, 5, 1, 3, 4, 1]
AAAAA -- [4, 2, 1, 2, 4, 2, 4, 4, 5, 2, 2, 1, 5, 2, 4, 3, 1, 1, 3, 3, 5]
BBB -- [5, 2, 1, 2, 4, 5, 4, 4, 1, 2, 2, 2, 4, 4, 4, 3, 1, 2, 3, 3, 2]
Upvotes: 2