two list convert into one ndarray - list

Here is my problem,
import numpy
lst1 = [[1,2,3],[7,8,9]]
lst2 = [4,5,6]
lst1.extend(lst2)
print(len(lst1))
print(len(lst1[0]))
NewArr = numpy.asarray(lst1)
print(NewArr.shape)
print ("List:", lst1)
print ("Array: ", NewArr)
run this code, it print result :
5
3
(5,)
List: [[1, 2, 3], [7, 8, 9], 4, 5, 6]
Array: [list([1, 2, 3]) list([7, 8, 9]) 4 5 6]
but I want the result look like this:
5
3
(5,3)
List: [[1,2,3,4,5,6],[7,8,9]]
Array: [list([1,2,3,4,5,6]) list([7,8,9])]
could someone help me please?

Your are extending the whole list, not the first element of the list.
You should write:
lst1[0].extend(lst2)
Which gives you your result:
list([list([1,2,3,4,5,6]) list([7,8,9])])
However, your first 2 print-statements should also be:
print(len(lst1[0])) # 5
print(len(lst1[1])) # 3

Related

How to print out lists in a list

Note: code created in python 2.7
I am taking a robotics class in school and we are learning python.
The task was to take
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
and print it out as such
1
2
3
4
5
6
7
8
9
When I tried my code out I got the error message
I tried looking at what this means online but I am just starting to learn python and I didn't understand any of the answers that I found.
What am I doing wrong? How do I fix my code?
First Post sorry about the mess
Reason why you get an error:
You get the error in the line
results.append(lists[numbers[each_list]])
because numbers is an integer and numbers[each_list] isn't a valid function. So instead use the square brackets correctly:
results.append(lists[numbers][each_list])
Other Methods:
You don't really need to use range function:
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
for i in n:
for j in i:
print(j)
or a one liner:
print('\n'.join(str(j) for i in n for j in i))
Or if you are flattening a nested list:
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flat(lis):
res = []
for i in lis:
for j in i:
res.append(j)
return res
flat_n = flat(n)
or a one-liner:
>>> n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
>>> flat = lambda x: [j for i in x for j in i]
>>> flat(n)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
or just simply:
flat_n = [j for i in n for j in i]
n=[[1,2,3],[4,5,6,7,8,9]]
def flatten(lists):
results=[]
for lists in n:
for numbers in lists:
results.append(numbers)
return results
new_list =(flatten(n))
for num in new_list:
print (num)
When Executed
1
2
3
4
5
6
7
8
9
>>>

Why the map function doesn't return the list without duplicate elements?

I have this list:
list1 = [1, 1, 1, 3, 3, 3, 56, 6, 6, 6, 7]
And I wat to get rid of duplicate values. The code for the map function is taken from here.
this is the complete testing code:
list1 = [1, 1, 1, 3, 3, 3, 56, 6, 6, 6, 7]
list2 = []
map(lambda x: not x in list2 and list2.append(x), list1)
print(list2)
list2 = []
[list2.append(c) for c in list1 if c not in list2]
print(list2)
list2 = []
for c in list1:
if c not in list2:
list2.append(c)
print(list2)
In Python 2.7 is prints:
[1, 3, 56, 6, 7]
[1, 3, 56, 6, 7]
[1, 3, 56, 6, 7]
In Python 3.4 it prints:
[]
[1, 3, 56, 6, 7]
[1, 3, 56, 6, 7]
Why the map function returns an empty list in Python3?
Because in python-3.x a map is not evaluate immediately. It works as a generator where elements are generated on the fly by need: this can be more efficient since it is possible you for instance only need the first three elements so why would you calculate all elements? So as long as you do not materialize the output of map in a way, you have not really calculated the map.
You can for instance use list(..) to force Python to evaluate the list:
list(map(lambda x: not x in list2 and list2.append(x), list1))
In that case python-3.x will generate the same result for list2.

Python - get repeat matches in two lists

I am trying to match two lists, but I want to pick up the repeat matches too. I can't use set because that would only give me {3} in my second example below.
a = [1,2,3,4]
b = [3,3,4,5]
return [3,4]
a = [1,2,3,3]
b = [3,3,4,5]
return [3,3]
You can use list comprehesion to check and return every item in a if it exists in b like below:
[item for item in a if item in b]
If you want only the elements that are in both a and b (to cover the cases mentioned by #kabanus in the comment), you can use the following:
[item for item in set(a) for i in range(min(a.count(item), b.count(item)))]
Output:
>>> a = [1, 2, 3, 4]
>>> b = [3, 3, 4, 5]
>>> [item for item in set(a) for i in range(min(a.count(item), b.count(item)))]
[3, 4]
>>>
>>> a = [1, 2, 3, 3]
>>> b = [3, 3, 4, 5]
>>> [item for item in set(a) for i in range(min(a.count(item), b.count(item)))]
[3, 3]
>>>
>>> a = [3, 3, 4]
>>> b = [4, 4, 3]
>>> [item for item in set(a) for i in range(min(a.count(item), b.count(item)))]
[3, 4]
Try something like (if order doesn't matter), Python 2:
from collections import Counter
a = [1,2,3,4]
b = [3,3,4,5]
ca=Counter(a)
cb=Counter(b)
print sum([[x]*min(ca[x],cb[x]) for x in set(a)],[])
This should return the list of all repeating matches the number of time they repeat, with no particular ordering beyond grouping together same elements. The output for the above example is:
[3,4]
I'm assuming you missed 4. The other example you have yields what you posted:
[3,3]

How to append a list using values from other list(maximum in the first must be minimum in the second)

a = [1 5 2 4 6 3]
I have this list. Is there a way to make a second corresponding list with the same number but 6 from a list must be 1 in the b list and 1 in a list must be 6 in b list .I want exactly this:
b = [6 2 5 3 1 4]
Use zip of the sorted, and reversed-sorted list to create a reverse mapping from reversed-tuples:
>>> a = [1, 5, 2, 4, 6, 3]
>>> reverse_mapping = dict(zip(sorted(a), reversed(sorted(a))))
Creates the following mapping:
{1: 6, 2: 5, 3: 4, 4: 3, 5: 2, 6: 1}
Now use that to construct the new list:
>>> b = [reverse_mapping[item] for item in a]
>>> b
[6, 2, 5, 3, 1, 4]
This can be optimized if needed, I just kept it clear.

How to copy list items certain amount of times?

I have a big list of around 2000 numbers in the list. This is just an example of what I want.
I have list1=[1,2,3,4] and list2=[1,3,2,5]. I want it so that list1[i] will be used list2[i] times in the new list.
So for this example the new list would be:list3=[1,2,2,2,3,3,4,4,4,4,4]
The new list3 has 1x1, 3x2, 2x3, 5x4.
This isn't pretty and isn't particularly easy to understand, but works:
>>> list1 = [1, 2, 3, 4]
>>> list2 = [1, 3, 2, 5]
>>> import itertools
>>> list3 = list(itertools.chain(*[[list1[i]] * count for i, count in enumerate(list2)]))
>>> list3
[1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 4]
Brief explanation...
You can multiply a list:
>>> [1] * 3
[1, 1, 1]
Using this in the list comprehension will get you a list-of-lists:
>>> [[list1[i]] * count for i, count in enumerate(list2)]
[[1], [2, 2, 2], [3, 3], [4, 4, 4, 4, 4]]
You can then use itertools to flatten the list as above.
list1=[1,2,3,4]
list2=[1,3,2,5]
list3 = []
for a, b in zip(list1, list2):
for i in range(b):
list3.append(a)
list3 == [1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 4]
Another alternative:
list1=[1,2,3,4]
list2=[1,3,2,5]
z=[]
for x,y in zip(list1,list2):
z.extend([x] * y)
print z