I have been working on a way to make list of tuples and finding the average of each tuples.
myList = [(1,2,3),(4,12,6)]
def GS(myList):
for miniList in myList:
r = miniList[0]
g = miniList[1]
b = miniList[2]
GS = round((r+g+b)/3,2)
miniList = list(miniList)
miniList[0] = GS
miniList[1] = GS
miniList[2] = GS
miniList = tuple(miniList)
return myList
print(GS(myList))
my list is [(1,2,3),(4,12,6)]
I should get the average of each tuple and replace the three
output : [(2.0,2.0,2.0),(7.33,7.33,7.33)]
You can use a list comprehension. Below is an example which avoids calculating the length of each tuple twice via map and zip iterators.
myList = [(1,2,3),(4,12,6)]
def GS(L):
lens = map(len, L)
res = [(sum(i)/i_len,)*i_len for i, i_len in zip(L, lens)]
return res
print(GS(myList))
[(2.0, 2.0, 2.0), (7.333333333333333, 7.333333333333333, 7.333333333333333)]
If you wish to round decimals, you can use:
res = [(round(sum(i)/i_len, 2),)*i_len for i, i_len in zip(L, lens)]
myList = [(1,2,3),(4,12,6)]
[(round(sum(e)/len(e)),)*len(e) for e in myList]
# [(2.0, 2.0, 2.0), (7.33, 7.33, 7.33)]
The problem is that in your for loop, you write:
for miniList in myList:
# ...
miniList = tuple(miniList)
you here seem to want to assign by reference. But that is not possible in Python (or at least not with this syntax).
You can however use for example an index, and perform it like:
def GS(myList):
for idx, miniList in enumerate(myList):
r = miniList[0]
g = miniList[1]
b = miniList[2]
GS = round((r+g+b)/3,2)
miniList = list(miniList)
miniList[0] = GS
miniList[1] = GS
miniList[2] = GS
myList[idx] = tuple(miniList)
return myList
That being said, this is a rather complex way to do this. You can for example use:
def GS(myList):
for idx, miniList in enumerate(myList):
miniList[idx] = (round(sum(miniList)/len(miniList), 2),) * len(miniList)
return myList
This will also work on tuples that contain more or less than three elements. What we do is calculating the sum(..) of the miniList, and divide that by the len(..) of the miniList to obtain the average. We then use the round(.., 2) function like in the original function.
Next we wrap this in a singleton tuple, with (.., ), and we then multiply it with the length of the tuple, to obtain a tuple where we repeat the elements in the singleton tuple len(miniList) times.
That being said, it is typically more Pythonic to construct a new list, then to change an existing one. Since it is possible that other variables refer to this list, and now are updated as well.
Related
I have two lists that both contain Objects from the same class. I want to group them together in a third list that contains lists or tuples of Objects with the same attribute value.
Example
Object1.time = 1
Object2.time = 2
Object3.time = 1
Object4.time = 2
Objekt5.time = 3
list1 = [Object1, Object2]
list2 = [Object3,Object4]
There result of the sorting should look like this:
result_list = [[Object1,Object3], [Object2,Object4], [Object5]]
I need to mention: I don't need the lists that contain only one object!
so, the final list should look like this:
final_result = [[Objekt1, Objekt3], [Objekt2, Objekt4]]
List1 contains 1500 objects, List2 over 70,000 the Problem is: if I use two for-loops to compare the objects it takes too long.
Here is my inefficient example:
class Example:
def __init__(self,time,example_attribute):
self.time = time
self.example_attribute = example_attribute
test_list1 = [1,1,2,3,4,5,6,6,7,8,9,9]
test_list2 = ["a","b","c","d","e","f","d","e","f","g","h","i"]
test_list3 = ["j","k","l","m","n","o","p","q","r","s","t","u"]
object_list1 = []
for i,j in zip(test_list1,test_list2):
object_list1.append(Example(i,j))
object_list2 = []
for i,j in zip(test_list1,test_list3):
object_list2.append(Example(i,j))
# How to group both lists together by the time attribute? This part takes too long.
group_by_time = []
for i in object_list1:
my_list = [i]
for j in object_list2:
if i.time == j.time:
my_list.append(j)
group_by_time.append(my_list)
for sub_list in group_by_time:
for index, item in enumerate(sub_list):
if index == 0:
print(item.time, ",",item.example_attribute,end =",")
else:print(item.example_attribute, end = ",")
print("")```
Use a dictionary, which is how you idiomatically group things:
import itertools
grouped = {}
for obj in itertools.chain(list1, list2):
grouped.setdefault(obj.time, []).append(obj)
Now you have a dictionary mapping the time attribute to a list of objects. You can get a list of list if you really want, something like:
final = list(grouped.values())
If you want to omit lists with only a single value, you can do something like:
final = [v for v in grouped.values() if len(v) > 1]
I want to make one large list for entering into a database with values from 4 different lists. I want it to be like
[[list1[0], list2[0], list3[0], list4[0]], [list1[1], list2[1], list3[1], list4[1]], etc.....]
Another issue is that currently the data is received like this:
[ [ [list1[0], list1[1], [list1[3]]], [[list2[0]]], etc.....]
I've tried looping through each list using indexs and adding them to a new list based on those but it hasn't worked, I'm pretty sure it didn't work because some of the lists are different lengths (they're not meant to be but it's automated data so sometimes there's a mistake).
Anyone know what's the best way to go about this? Thanks.
First list can be constructed using zip function as follows (for 4 lists):
list1 = [1,2,3,4]
list2 = [5,6,7,8]
list3 = [9,10,11,12]
list4 = [13,14,15,16]
res = list(zip(list1,list2,list3,list4))
For arbitrtary number of lists stored in another list u can use *-notation to unpack outer list:
lists = [...]
res = list(zip(*lists))
To construct list of lists for zipping from you data in second issue use flatten concept to it and then zip:
def flatten(l):
res = []
for el in l:
if(isinstance(el, list)):
res += flatten(el)
else:
res.append(el)
return res
auto_data = [...]
res = list(zip(*[flatten(el) for el in auto_data]))
Some clarification at the end:
zip function construct results of the smallest length between all inputs, then you need to extend data in list comprehension in last code string to be one length to not lose some info.
So if I understand correctly, this is your input:
l = [[1.1,1.2,1.3,1.4],[2.1,2.2,2.3,2.4],[3.1,3.2,3.3,3.4],[4.1,4.2,4.3,4.4]]
and you would like to have this output
[[1.1,2.1,3.1,4.1],...]
If so, this could be done by using zip
zip(*l)
Make a for loop which only gives you the counter variable. Use that variable to index the lists. Make a temporary list , fill it up with the values from the other lists. Add that list to the final one. With this you will et the desired structure.
nestedlist = []
for counter in range(0,x):
temporarylist = []
temporarylist.append(firstlist[counter])
temporarylist.append(secondlist[counter])
temporarylist.append(thirdlist[counter])
temporarylist.append(fourthlist[counter])
nestedlist.append(temporarylist)
If all the 4 lists are the same length you can use this code to make it even nicer.
nestedlist = []
for counter in range(0,len(firstlist)): #changed line
temporarylist = []
temporarylist.append(firstlist[counter])
temporarylist.append(secondlist[counter])
temporarylist.append(thirdlist[counter])
temporarylist.append(fourthlist[counter])
nestedlist.append(temporarylist)
This comprehension should work, with a little help from zip:
mylist = [i for i in zip(list1, list2, list3, list4)]
But this assumes all the list are of the same length. If that's not the case (or you're not sure of that), you can "pad" them first, to be of same length.
def padlist(some_list, desired_length, pad_with):
while len(some_list) < desired_length:
some_list.append(pad_with)
return some_list
list_of_lists = [list1, list2, list3, list4]
maxlength = len(max(list_of_lists, key=len))
list_of_lists = [padlist(l, maxlength, 0) for l in list_of_lists]
And now do the above comprehension statement, works well in my testing of it
mylist = [i for i in zip(*list_of_lists)]
If the flatten concept doesn't work, try this out:
import numpy as np
myArray = np.array([[list1[0], list2[0], list3[0], list4[0]], [list1[1], list2[1], list3[1], list4[1]]])
np.hstack(myArray)
Also that one should work:
np.concatenate(myArray, axis=1)
Just for those who will search for the solution of this problem when lists are of the same length:
def flatten(lists):
results = []
for numbers in lists:
for output in numbers:
results.append(output)
return results
print(flatten(n))
I have a list of 50 numbers, [0,1,2,...49] and I would like to create a list of tuples without duplicates, where i define (a,b) to be a duplicate of (b,a). Similarly, I do not want tuples of the form (a,a).
I have this:
pairs = set([])
mylist = range(0,50)
for i in mylist:
for j in mylist:
pairs.update([(i,j)])
set((a,b) if a<=b else (b,a) for a,b in pairs)
print len(pairs)
>>> 2500
I get 2500 whereas I expect to get, i believe, 1225 (n(n-1)/2).
What is wrong?
You want all combinations. Python provides a module, itertools, with all sorts of combinatorial utilities like this. Where you can, I would stick with using itertool, it almost certainly faster and more memory efficient than anything you would cook up yourself. It is also battle-tested. You should not reinvent the wheel.
>>> import itertools
>>> combs = list(itertools.combinations(range(50),2))
>>> len(combs)
1225
>>>
However, as others have noted, in the case where you have a sequence (i.e. something indexable) such as a list, and you want N choose k, where k=2 the above could simply be implemented by a nested for-loop over the indices, taking care to generate your indices intelligently:
>>> result = []
>>> for i in range(len(numbers)):
... for j in range(i + 1, len(numbers)):
... result.append((numbers[i], numbers[j]))
...
>>> len(result)
1225
However, itertool.combinations takes any iterable, and also takes a second argument, r which deals with cases where k can be something like 7, (and you don't want to write a staircase).
Your approach essentially takes the cartesian product, and then filters. This is inefficient, but if you wanted to do that, the best way is to use frozensets:
>>> combinations = set()
>>> for i in numbers:
... for j in numbers:
... if i != j:
... combinations.add(frozenset([i,j]))
...
>>> len(combinations)
1225
And one more pass to make things tuples:
>>> combinations = [tuple(fz) for fz in combinations]
Try This,
pairs = set([])
mylist = range(0,50)
for i in mylist:
for j in mylist:
if (i < j):
pairs.append([(i,j)])
print len(pairs)
problem in your code snippet is that you filter out unwanted values but you don't assign back to pairs so the length is the same... also: this formula yields the wrong result because it considers (20,20) as valid for instance.
But you should just create the proper list at once:
pairs = set()
for i in range(0,50):
for j in range(i+1,50):
pairs.add((i,j))
print (len(pairs))
result:
1225
With that method you don't even need a set since it's guaranteed that you don't have duplicates in the first place:
pairs = []
for i in range(0,50):
for j in range(i+1,50):
pairs.append((i,j))
or using list comprehension:
pairs = [(i,j) for i in range(0,50) for j in range(i+1,50)]
I created a list of lists and then tried to get a distinct list of the lists using set(), but it appears as though i cant use list on a set.
Is there another way to accomplish this with a concise statement that performs well?
CODE
x = [1,2]
y = [1,2]
z = [2,3]
xyz = []
xyz.append(x)
xyz.append(y)
xyz.append(z)
set(xyz)
Error
TypeError: unhashable type: 'list'
Goal
xyz = [[1,2],[2,3]]
if you want to preserve order and keep your lists, you could use generator function to remove the dupes from your list:
xyz = [x, y, z]
def remove_dupe_subs(l):
seen = set()
for sub in l:
tup = tuple(sub)
if tup not in seen:
yield sub
seen.add(tup)
xyz[:] = remove_dupe_subs(xyz)
Or using a generator expression taking advantage of the fact set.add returns None :
seen = set()
xyz[:] = (seen.add(tup) or sub for sub, tup in zip(xyz, map(tuple, xyz)) if tup not in seen)
print(xyz)
If the list members are hashable, it will work
x = [1,2]
y = [1,2]
z = [2,3]
xyz = []
xyz.append(tuple(x))
xyz.append(tuple(y))
xyz.append(tuple(z))
print xyz
xyz_set = set(xyz)
print xyz_set
It's a little bit convoluted, but this will do the trick in a single line:
xyz=[list(x) for x in list(set((tuple(x),tuple(y),tuple(z))))]
Suppose I have a dict like:
aDict[1] = '3,4,5,6,7,8'
aDict[5] = '5,6,7,8,9,10,11,12'
aDict[n] = '5,6,77,88'
The keys are arbitrary, and there could be any number of them. I want to consider every value in the dictionary.
I want to treat each string as comma-separated values, and find the intersection across the entire dictionary (the elements common to all dict values). So in this case the answer would be '5,6'. How can I do this?
from functools import reduce # if Python 3
reduce(lambda x, y: x.intersection(y), (set(x.split(',')) for x in aDict.values()))
First of all, you need to convert these to real lists.
l1 = '3,4,5,6,7,8'.split(',')
Then you can use sets to do the intersection.
result = set(l1) & set(l2) & set(l3)
Python Sets are ideal for that task. Consider the following (pseudo code):
intersections = None
for value in aDict.values():
temp = set([int(num) for num in value.split(",")])
if intersections is None:
intersections = temp
else:
intersections = intersections.intersection(temp)
print intersections
result = None
for csv_list in aDict.values():
aList = csv_list.split(',')
if result is None:
result = set(aList)
else:
result = result & set(aList)
print result
Since set.intersection() accepts any number of sets, you can make do without any use of reduce():
set.intersection(*(set(v.split(",")) for v in aDict.values()))
Note that this version won't work for an empty aDict.
If you are using Python 3, and your dictionary values are bytes objects rather than strings, just split at b"," instead of ",".