I am trying to find the zig zag pattern between two arrays. I've come across posts where 1 array is used. I understand the logic of it but even when i append the two lists together, I am unable to get the desired output.
arr1 = [1,2,3,4,5]
arr2 = [6,7,8,9,10]
expected_output = [5,9,3,7,1,6,2,8,4,10]
any hint to solve it would help. P.S I am still learning programming, so if i missed an obvious pattern, do let me know. thanks!
Related
Recently I've started using ndarray to handle my multi-dimentional arrays when using Rust, and I love a lot of it's convenience features, if I could just figure out one problem, it would be perfect.
Normally, when you collect an iterator over set a Results (e.g. you just did a map operation that was fallible), you can tell rust that you would like to collect it as a Result<Vec<_>, _>, instead of a Vec<Result<, _>>. This is super helpful, but as far as I can tell Ndarray's producers don't have a similar feature? What I'd like to know is how can I efficiently deal with Results when using Ndarray's Producers/when using Zip to operate over a ndarray.
All I can think to do is to either turn the ndarray into a normal iterator, which I don't want to do because the dimentionality is lost (e.g. an Array3 turns into a Vec), do some kind of second iteration after the first to find any Err variants, which seems inefficient, or just use unwrap everywhere...
I'd really appreciate any other perspectives on using ndarray, or other ways to manage multi-dimensional arrays in rust.
code-example:
let thingy: Array3 = whatever;
let output = Zip::from(thingy).and(another_thingy).map_collect(|thing1, thing2| {
function_that_returns_result(thing1, thing2)
});
//output ends up being an Array3 of Results, where I would like an idiomatic way for it to be a Result containing an Array3
I'm currently researching different ways to combine, organize and manipulate data for different purposes.
Just today I found this zip() / iter() technique of creating a LIST containing TUPLES while also being able to specify how many elements are in each TUPLE. However, I'm unable to fully understand part of the syntax.
Here is the code:
mylist = [1,2,3,4,5,6]
converted = [x for x in zip(*[iter(mylist)]*2)]
print(converted)
This is the output (which is what I want):
[(1, 2), (3, 4), (5, 6)]
What I'm trying to grasp is the first asterisk. I understand that it's most likely in relation to the '*2' telling the 'iter' or 'zip' function how many elements each tuple should contain, however, I'm trying to grasp the need for it's placement.
Any help is greatly appreciated.
Also, if you know of another technique to accomplish this and feel like sharing, I'd greatly appreciate learning from you.
Thanks again in advance guys!
Basically iter(mylist) makes an iterator object for the list, then it's put into a list [iter(mylist)] which is multiplied by 2 basically making a list that contains two references to the same iterator object: [iter(mylist)]*2 -> [<list_iterator object at 0x7f8fbc1ac610>, <list_iterator object at 0x7f8fbc1ac610>]
The first asterisk unpacks the list as arguments into the zip() function.
To make it easier to understand as I'm not very good at explaining things, this code does the same as yours:
mylist = [1,2,3,4,5,6]
iterators = [iter(mylist)] * 2
converted = [x for x in zip(*iterators)]
print(converted)
So it makes an iterator, then it makes a list that contains two references to the same iterator object by multiplying it by 2.
And then it unpacks the list to be used as arguments for the zip() function
I hope this cleared it up at least a little for you as I'm not very good at explaining.
I know this isn't exactly the first question about list comprehensions, but I've been looking around and experimenting for a while and can't figure this one out. I'll apologize in advance, I'm a self-taught novice learning from failures.
This is working code, but it screams list comprehension. I understand list comprehensions and use them, but the combination of stacking for and working with the dictionaries within the lists is breaking my brain. How would you simplify this:
results = []
for system in systems: # list of dicts
for result in telnet_results: # list of dicts
if system['master_ip'] == result['master_ip']:
combined = {**system, **result} # merge dicts, right takes precedence
results.append(combined)
Thanks in advance for any help on this.
results = [{**system, **result} for system in systems for result in telnet_results if
system['master_ip'] == result['master_ip']]
Can be also splitted more logically:
results = [{**system, **result}
for system in systems
for result in telnet_results
if system['master_ip'] == result['master_ip']]
Is this "simplified"? I'm not sure. List comprehensions are not magic, and they do not always simplify the code or make it more readable.
I was doing some code from a MIT course and I got this code as a help function, but there are some parts that are a lil obscure from my python
understanding.
def partitions(set_):
if not set_:
yield []
return
for i in range(2**len(set_)//2):
parts = [set(), set()]
for item in set_:
parts[i&1].add(item)
i >>= 1
for b in partitions(parts[1]):
yield [parts[0]]+b
I undrstand that it make lots of different combinations of a list, a dict or other iterating obj.
I got my eyes rolling about this parts:
parts[i&1].add(item)
i >>= 1
Some of the fellows here could try to explain to me this part?
I look for some in the net, but not sure if I understand completly.
Thanks in advance!
PS- There are any way to make a more readble and using biult in functions like permutations, combinations?
I am looking for a way to overcome a huge memory issue.
I have about 100 lists, each containing an average of 10 elements.
And I need to create all possible combinations and work with them one by one (I don't need all at once).
Currently my code looks like this:
import itertools
l1 = ['a','b','c']
l2 = ['a','b','c']
l3 = ['a','b','c']
all_lists = [l1,l2,l3]
combination_list = [item for item in itertools.product(*all_lists)]
for c in combination_list:
print(c) #do something with c
Sadly if I try to use more than 10 lists I get a memory error.
Any idea how I can overcome that memory issue?
Is there way saving the combinations one-by-one in a file and accessing them that way, too?
Edit: I should have said, that I need to access that combinations later on again, storing them in a dict as a key and assigning them a value.
Yep, use the iterator straight up instead of putting it in a list.
for c in itertools.product(*all_lists):
print(c) #do something with c
Looking at the doc for product you see it's just making an iterator.
EDIT
If you want to reuse the combinations later, you are better off just enumerating them again (so you don't have to store them).
combination_list = lambda: itertools.product(*all_lists)