How to run a ij loop in Python, and not repeat (j,i) if (i,j) has already been done?

2024/9/22 12:31:42

I am trying to implement an "i not equal to j" (i<j) loop, which skips cases where i = j, but I would further like to make the additional requirement that the loop does not repeat the permutation of (j,i), if (i,j) has already been done (since, due to symmetry, these two cases give the same solution).

First Attempt

In the code to follow, I make the i<j loop by iterating through the following lists, where the second list is just the first list rolled ahead 1:

mylist = ['a', 'b', 'c']
np.roll(mylist,2).tolist() = ['b', 'c', 'a']

The sequence generated by the code below turns out to not be what I want:

import numpy as npmylist = ['a', 'b', 'c']
for i in mylist:for j in np.roll(mylist,2).tolist():print(i,j)

since it returns a duplicate a a and has repeated permutations a b and b a:

a b
a c
a a
b b
b c
b a
c b
c c
c a

The desired sequence should instead be the pair-wise combinations of the elements in mylist, since for N=3 elements, there should only be N*(N-1)/2 = 3 pairs to loop through:

a b
a c
b c
Answer

You can do this using quite a hacky method just by removing the first element and appending it:

mylist.append(mylist.pop(0))

Where .append(...) will append an element to the end of a list, and .pop(...) will remove an element from a given index and return it.

  • You can read up about the builtin data structure functions here
https://en.xdnf.cn/q/119137.html

Related Q&A

Split a string with multiple delimiters

I have the string "open this and close that" and I want to obtain "open this and" and "close that". This is my best attempt:>>>print( re.split(r[ ](?=(open|clos…

Extracting a string between 2 chracters using python [duplicate]

This question already has answers here:Python-get string between to characters(4 answers)Closed 7 years ago.I need a Python regex to give me all the strings between ~ and ^ from a string like this:~~~~…

remove empty line printed from hive query output using python

i am performing a hive query and storing the output in a tsv file in the local FS. I am running a for loop for the hive query and passing different parameters. If the hive query returns no output once …

.exceptions.WebDriverException: Message: Can not connect to the Service

struggling to find a solution all over, have latest chrome 117 and also downloaded chromedriver and used the path accordingly in script also tried with chrome browser Although it opens the browser but …

How to call a previous function in a new function? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about programming within the scope defined in the help center.Cl…

Using simpleauth to login in with GAE

This question is in the reference of this. As suggested I am using simpleauth to login via linkedin. Now I am having trouble with the redirect_uri. I have successfully deployed dev_appserver.py example…

How do i force my code to print in python

Im having trouble trying to work out an error in my code. It isnt printing the final product and leaving a blank space.playing = True string = "" Alphabet = (z,a,b, c, d, e, f, g, h, i, j, k,…

Adding specific days in python table

I have a dataset (Product_ID,date_time, Sold) which has products sold on various dates. The dates are not consistent and are given for 9 months with random 13 days or more from a month. I have to segre…

django how to following relationships backwards?

I am having some issue with following relationships backwards. From the parent page i want to be able to see what children belong to that parent. Heres what i got so farmodel.pyclass Parents(models.Mod…

Python File handling: Seaching for specific numbers

Im creating a document in which I need to record license plates of vehicles (its a practice exercise, nothing illegal) and calculate the speed they travel at and display all the vehicles that are trave…