Skip to content Skip to sidebar Skip to footer

How Can I Put An Element Of A String In A List With A Certain Behaviour

list1 = ['A', 'B'] list2 = [[(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4)]] I need my output as: [[(1, 1), (1, 2), (1, 3), (1, 4)],[(2, 1), (2, 2), (2, 3), (2,

Solution 1:

Using split and groupby:

>>>from itertools import groupby>>>data = [map(int, (z for z in x.split(','))) for x in string1.split()]>>>a, b = [list(j) for j in groupby(data, key=operator.itemgetter(0))]>>>a
[[1, 1], [1, 2], [1, 3], [1, 4]]
>>>b
[[2, 1], [2, 2], [2, 3], [2, 4]]

Then you can do:

>>> dict(zip(list1, (len(i) for i in (a,b))))
{'A': 4, 'B': 4}

Solution 2:

Looks like you have to play with your data a bit to reduce to how you want it. Like the first section below demonstrates and then you will have to create a dictionary and then look for the values in the dict. Here is the code for your sample data. You should be abel to build up on that.

>>>string1 = '1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4'>>>list1 = string1.split(',')>>>list2 = [tuple(map(int, a.split(','))) for a in list1]
[(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4)]

>>>temp_dict = {}>>>for each in list2:...    a = each[0]...if a in temp_dict:...            temp_dict[a].append(each)...else:...            temp_dict[a] = [each]...>>>temp_dict.values()
[[(1, 1), (1, 2), (1, 3), (1, 4)], [(2, 1), (2, 2), (2, 3), (2, 4)]]

Solution 3:

You can get the list of lists as follows:

from collections import defaultdict

data = defaultdict(list)
forvalin string1.split():
    v1, v2 = val.split(',')
    data[v1].append(v2)
result = [[(int(key), int(v)) for v in values] for key, values indata.items()]

To get the dictionary you can do:

d = dict(zip(list1, result))

This gives you a list with the elements of list1 as keys. To get the lengths you can do:

d = dict([(key, len(ls)) for key, ls inzip(list1, result)])

Solution 4:

Take your list of tuples and split it into separate lists based on the first number in each tuple. In the same step, add the filtered list to the dictionary with the corresponding key from list1. Because of the double brackets in list2 (copied below), the actual data is in list2[0].

//note the double brackets, data is in list2[0], not list2
list2 = [[(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4)]]

d = dict()

for i in range (0, len(list1)):
     d[list1[i]] = [x for x in list2[0] if x[0] == i+1]
     //on the first run though, list[i] will be 'A'and will be set to [(1, 1), (1, 2), (1, 3), (1, 4)]
     //on the 2nd run though, list[i] will be 'B'and will be set to [(2, 1), (2, 2), (2, 3), (2, 4)]

Printing d shows the formatted data

print(d)
//prints {'A': [(1, 1), (1, 2), (1, 3), (1, 4)], 'B': [(2, 1), (2, 2), (2, 3), (2, 4)]}

EDIT: I misread the question (I thought you wanted the actual data in the dictionary, not just the lengths). To get the length of the list instead of the contents, just wrap the second list comprehension in a len() like

len([x for x in list2[0] if x[0] == i+1])

After that change, d will contain the lengths, not the data:

print(d) //{'A': 4, 'B': 4}

Post a Comment for "How Can I Put An Element Of A String In A List With A Certain Behaviour"