Python ObjectListView -- how to split a list into two columns - python

I have an ObjectListView in wxPython that I would like to split into two identical lists that sit next to each other instead of one long list with a scrollbar. So, instead of:
Column 1 -- Column 2 -- Column 3
data data data
data data data
I would like it to look like this:
Column 1 -- Column 2 -- Column 3 Column 1 -- Column 2 -- Column 3
data data data data data data
Of course, with more data split evenly between them, if possible. Is there a way to accomplish this without making two separate lists? The reason I don't want to make two lists is that I have one large object list I'd like to pass it and with two lists I think I'd have to split the object in two and send a section to each list; if elegance is an option, I'd prefer it.

If you use list_B = list_A you are not making "another" list, instead you are simply saying that there are 2 names for the same list, they both point to the same thing. You can use id() to check that this is true.
>>> list_A = [1,2,3,4,5,6,7,8]
>>> id(list_A)
140229575676488
>>> list_B = list_A
>>> id(list_B)
140229575676488
>>> list_A
[1, 2, 3, 4, 5, 6, 7, 8]
>>> list_B
[1, 2, 3, 4, 5, 6, 7, 8]
>>> list_A.append(9)
>>> list_A.append(10)
>>> list_A
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list_B
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Related

Slicing and appending lists leaves extra values on the end

I want to append the first 80% of the values in two arrays into one new array. I can figure out a work around to this issue but I came across this problem when trying to do it in one line and I'm interested in knowing if it is possible.
# I create my two arrays
a = [1,2,3,4,5]
b = [6,7,8,9,10]
# Showing what slicing a and b in this way returns
# 4 in this case is equal to 80%
a[:4]
[1, 2, 3, 4]
b[:4]
[6, 7, 8, 9]
# Append 80% of b to 80% of a
a[:4] += b[:4]
# Print a
# There is a 5 at the end which I don't want to be there :(
a
[1, 2, 3, 4, 6, 7, 8, 9, 5]
I understand that what my one line is doing is telling python to insert 80% of b, 80% of the way through a, but not actually to only keep 80% of a.
Is there a way to do this without then having to remove the last 20% of a afterwards?
What you're writing is like a[:4] = a[:4] + b[:4], you're leaving the last index of a (i.e. 5), unchanged. You could think of it a bit like an insert (without creating a nested list), i.e.
>>> a = [1,2,3,4,5]
>>> a.insert(4, b[:4])
>>> a
[1, 2, 3, 4, [6, 7, 8, 9], 5]
The solution to modify the same list as you want is to assign not just to a[:4], but to the entire list:
>>> a[:] = a[:4] + b[:4]
>>> a
[1, 2, 3, 4, 6, 7, 8, 9]
a[:4] += b[:4]
With this equation, you're basically adding the first 4 elements of b[6,7,8,9] to a[1,2,3,4], leaving the last element of a[5] untouched. You can leave out the last index of a[5] by doing
c = a[:4] + b[:4]
# I create my two arrays
a = [1,2,3,4,5]
b = [6,7,8,9,10]
a=a.copy()[:4]+b.copy()[:4]
# output [1, 2, 3, 4, 6, 7, 8, 9]
shallow copy, a reference of object is copied in other object.
It means that any changes made to a copy of object do reflect in the original objec
t. In python, this is implemented using “copy()” function.
created a shallow copy of a and b then, i am taking first 4 elements from it and
appending b element with a .

Using a List of Indexes to Grab Values in Another List

I have two lists, one containing a set of index points that I would like to pull from a second list.
index_values = [3, 3, 6, 7]
A list of ten numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I'm trying to get the result to print the numbers at the 3, 3, 6, and 7 indexes in the second list.
Any help is appreciated.
Thank you!
You have two alternatives the way I see it.
String join with list comprehension
s = ', '.join([str(numbers[idx]) for idx in index_values]) # or '\n' for newlines
print(s)
Loop
for idx in index_values:
print(numbers[idx])

Is it possible to insert specific sections of one list into a specific location in another list?

I'm fairly new to Python, and besides finding it useful and rather easy to understand for the most part, there are still some things I'm unclear of, ergo this question.
Is it possible to insert specific sections of one list into a specific location in another list?
Say for example, I have a list called 'a', and in this list I have the numbers 1, 3 and 5 in this format:
a = [1, 3, 5]
I also have a list called 'b' which contains the numbers 2 and 4 in this format:
b = [2, 4]
My end goal would be for list 'a' to be changed to this:
a = [1, 2, 3, 4, 5]
As you can see, this would require me to specify using indices for both lists to combine them into one list in this custom format, and I am unsure as to how I could go about this.
I unintentionally left out a major detail, and that is the fact that I wanted to make use of the 'insert' function rather than 'append'.
Yes. Just use insert() and have the second argument be a list element.
a = [1, 3, 5]
b = [2, 4]
# The first argument is the index to insert it at.
# The second is the actual item to be inserted.
a.insert(1, b[0]) # [1, 2, 3, 5]
a.insert(3, b[1]) # [1, 2, 3, 4, 5]
Note that if you just want a sorted, you can just use this:
for item in b:
a.append(item) # Order does not matter, we will sort it later
# a is currently [1, 3, 5, 2, 4] since append() adds to the end of the list
a.sort()
# a is now [1, 2, 3, 4, 5] as sort() re-ordered it
Or if you want it even simpler, concatenate them and then sort:
a = a + b # [1, 3, 5, 2, 4]
a.sort() # [1, 2, 3, 4, 5]
Let me know if this is unclear or not what you wanted.
How does this work for you?
a = [1,2,5,6,7]
b = [3,4]
for n,k in enumerate(a):
if(k<b[0]):
a = a[0:n]+b+a[n:]
break
print(a)
using the colon to slice arrays is super useful.
If you're interleaving the lists, you can zip them using izip_longest:
>>> a = [1, 3, 5]
>>> b = [2, 4]
>>> from itertools import izip_longest
>>> [c for pair in izip_longest(a, b)
... for c in pair if c is not None]
[1, 2, 3, 4, 5]

Python: Numpy accumulated list not seperated by commas

Simple example: I got a list called 'mylist' and I want to accumulate the numbers inside and save them into a new list called 'mylist_accum'.
import numpy
mylist = [1,2,3,4,5]
print mylist
mylist_accum = numpy.add.accumulate(mylist)
print mylist_accum
My prints look like this:
[1, 2, 3, 4, 5]
[ 1 3 6 10 15]
And I want them to look like this:
[1, 2, 3, 4, 5]
[1, 3, 6, 10, 15]
I need my accumulated listelements to be seperated by commas. Otherwise Matplotlib cant work with them.
It's just printing, matplotlib could handle with the numpy.arrays easy:
In [77]: type(mylist_accum)
Out[77]: numpy.ndarray
If you want to see with commas you could use .tolist method of the numpy.array:
In [75]: mylist_accum.tolist()
Out[75]: [1, 3, 6, 10, 15]
Or convert it to usual list:
In [74]: list(mylist_accum)
Out[74]: [1, 3, 6, 10, 15]

Need Help in switching values in a list in python

I have a list of numbers, such as:
[1, 2, 3, 4, 5, 6]
I'm having trouble figuring out how to switch the first 2 items of the list with the last 2, or the first 3 with the last 3, and so on.
When i assign the values of the 1st two numbers to the last 2 items of the list, i then cannot assign the last 2 values (what used to be the last 2 values of the list) to the first two because the last 2 values have been lost.
If i try using another empty list and appending the last 2 values of the original list to that list, then appending the middle values, and then the first 2 values of the old list, I end up with something like this:
[[[5, 6], [3, 4], [1, 2]]]
I don't want nested lists! What I want is:
[5, 6, 3, 4, 1, 2]
Can someone help me?
>>> nums = [1, 2, 3, 4, 5, 6]
>>> nums[:2], nums[-2:] = nums[-2:], nums[:2]
>>> nums
[5, 6, 3, 4, 1, 2]
This modifies the original list but if you want a separate new list you should use the following:
>>> nums = [1, 2, 3, 4, 5, 6]
>>> swapped_nums = nums[-2:] + nums[2:-2] + nums[:2]
>>> swapped_nums
[5, 6, 3, 4, 1, 2]
Note: This won't work properly if your list has < 4 elements

Categories

Resources