How to merge a list of lists with same type of items to a single list of items?
Use the SelectMany extension method
list = listOfList.SelectMany(x => x).ToList();
join list of lists in python
import itertools
a = [['a','b'], ['c']]
print(list(itertools.chain.from_iterable(a)))
merging lists in a list of lists that has one similar item in python
Here is a way to do it.
For each pair:
- if we find a group that contains one of the values, we append the pair to the group
- if we find a second group that contains the other value, we merge the groups.
- if we found no matching group, then our pair constitutes a new one.
def group_equals(lst):
groups = []
for pair in lst:
pair = set(pair)
equals_found = 0
for idx, group in enumerate(groups):
if group.intersection(pair):
equals_found += 1
if equals_found == 1:
# We found a first group that contains one of our values,
# we can add our pair to the group
group.update(pair)
first_group = group
elif equals_found == 2:
# We found a second group that contains the other one of
# our values, we merge it with the first one
first_group.update(group)
del groups[idx]
break
# If none of our values was found, we create a new group
if not equals_found:
groups.append(pair)
return [list(sorted(group)) for group in groups]
tests = [ [["a", "b"], ["c", "d"], ["b", "c"]], # all equal
[["a","b"],["c","d"],["a", "e"],["f","d"]],
[["a","b"],["c","d"],["a", "e"],["f","d"],["x","y"]]
]
for lst in tests:
print(group_equals(lst))
# [['a', 'b', 'c', 'd']]
# [['a', 'b', 'e'], ['c', 'd', 'f']]
# [['a', 'b', 'e'], ['c', 'd', 'f'], ['x', 'y']]
Merge lists with same first element in list of lists
Try this:
d = {}
for key, value in a:
if key not in d.keys():
d[key] = [key]
d[key].append(value)
result = list(d.values())
Merging a list of lists into a single list with minimal comparisons
Given that none of those lists are sorted, and you don't have any pre-existing knowledge about the data in the lists, you cannot do this better than O(nlogn) comparisons. Here n is size(list 1) + size(list 2) +...size(list final).
You can simply iterate through the list of lists and construct a master list with n elements. Then you can sort the master list using either quick sort or merge sort. The time complexity would be O(nlogn) for sorting. There is an additional memory of O(n) for the master list.
So you would ask the user about nlogn times. You can minimize the number of times you ask the user about comparison by caching the results of the prior comparisons and not asking the user the same comparison again.
Linq list of lists to single list
You want to use the SelectMany
extension method.
var residences = details.SelectMany(d => d.AppForm_Residences).ToList();
Merge elements in list of lists
If you don't want to write a loop you can use map
and str.join
>>> list(map(''.join, A))
['baaaa', 'baaaa']
However, the loop using a list comprehension is almost as short to write, and I think is clearer:
>>> [''.join(e) for e in A]
['baaaa', 'baaaa']
How to merge two element inside a list of lists?
Here is a solution in 2 steps.
First, collect all the items in the list with more than one element.
l = [[11710000035, 11710000034], [11710000038, 11710000031, 11710000033], [11710000099]]
r = [i for i in l if len(i) > 1]
Then add the single elements in one of the items of r
. Since it doesn't matter for you, I would simply add them to the first item in the list.
for i in l:
if len(i) == 1:
r[0] += i
print(r)
[[11710000035, 11710000034, 11710000099], [11710000038, 11710000031, 11710000033]]
Converting a list of lists into a single list using linq
You can do:
var allTrackAreasCombined = allTrackAreas.SelectMany(t => t).ToList();
Related Topics
Convert from Binary Data to an Image Control in ASP.NET
How to Get JSON Response from a 3.5 Asmx Web Service
Calling Generic Method with Type Variable
Duck Typing in the C# Compiler
Can You Specify Format for Xmlserialization of a Datetime
How to Use Switch-Case on a Type
Additional Text Encountered After Finished Reading JSON Content:
Configurationmanager Doesn't Save Settings
Transitive References in .Net Core 1.1
Double Precision Problems on .Net
How to Detect a Process Start & End Using C# in Windows
It Is Possible to Copy All the Properties of a Certain Control? (C# Window Forms)
Is There Some Way to Handle Async/Await Behind an Asmx Service