Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help to find a command
#6
(Feb-03-2019, 12:51 PM)perfringo Wrote: I still feel that description of the problem and your idea how to solve it are mixed.

How I understand the problem: you have list which consists lists as elements. List represents chain and element lists represent links. Links are represented with integer pair where first number indicates strength and second location. It can be expressed something like that (this ain't code, this is visualisation of data with descriptive names):

chain = link_1: (strength, location), link_2: (strength, location)...

You apply tension (which is integer) to chain and need to know which links will break. Link will break only when tension is larger than strength i.e if they are equal chain stays in one piece.

Sidenote: it puzzles me that if first link of chain breaks then the chain is broken and it can't break in another link (but I don't know the whole picture)

I am wondering whether it makes point to find weakest link? There is no point to compare tension and strength if we know that tension is smaller. We can find strengths of all links (it assumes that strength is first integer, in you first post strength seemed to be second integer):

In [1]: chain = [[10, 20], [30, 40], [50, 60]]

In [2]: [link[0] for link in chain]        # give me first element of every link in chain
Out[2]: [10, 30, 50]

In [3]: min([link[0] for link in chain])   # find the smallest value i.e. strength of weakest link
Out[3]: 10
If I know this value (strength of weakest link) I know that it is kind of pointless to compare with tension values <= 10.

Now to the tension. What do you actually need to know? At which tension link will not hold anymore and chain will be broken? You can just min + 1 and have your answer. If you have fixed tension then one should compare strength to tension to find out whether it will break or not.

If you need the location of weakest link:

In [4]: min([(link[0], i) for i, link in enumerate(chain)])
Out[4]: (10, 0)
Weakest link in chain is at index 0 (first element) with value 10.

If you need compare all links to some arbitrary tension, let's say 35:

In [5]: tension = 35

In [6]: [(link[0], i) for i, link in enumerate(chain) if tension > link[0]]
Out[6]: [(10, 0), (30, 1)]
Links at index position 0 and 1 will break.

I personally think that appending some value to link is not good idea. If you know location of the 'broken' links you can access them by their location. However, if it is needed:

In [7]: [link +  [0] if tension > link[0] else link for link in chain]
Out[7]: [[10, 20, 0], [30, 40, 0], [50, 60]]

Thank you for your help. I am leaning a lot.

As I said, the situation is more complicated then the "chain" example. The real case is a fiber composite (one fiber embedded by polymer) with random flaws in it (with random strength at random position). Each flaw can be considered as a link, and while I am increasing the tensile, the fiber will break at that flaw strength and so one. However the area around the break need to be treated differently. For that reason I need to increase the tensile step by step, when one break occurs, I will need to modify the local property. But lets comeback to python hehe.

Now my problem is to increase the tensile one by one and as soon it reaches one link strength, this link shall append 1.

Feel free to suggest any python function that suits better. I go and check how to use it.

I am trying this with "while" but it is not working.

In the bellow example, the break will stop when the is a gap bigger than 10 between 2 link strength.
For example: [[77, 2], [16, 5], [36, 11], [7, 12], [93, 15], [42, 17], [82, 18], [33, 33], [79, 42], [94, 43]]
the result would be:
[[77, 2], [16, 5, 1], [36, 11], [7, 12, 1], [93, 15], [42, 17], [82, 18], [33, 33], [79, 42], [94, 43]]

import random
import operator

chain = []

for x in range (10):
    link = []
    s = random.randint (0, 100)
    link.append (s)
    l = random.randint (0, 50)
    link.append (l)
    chain.append (link)
print (chain)

print ([link[0] for link in chain])

print (max([link[0] for link in chain]))

chain.sort(key = operator.itemgetter(1))
print (chain) 

tension = 0
cont = 0
while (cont < 10):  
    for link in chain:
        if tension > link[0]:
            link.append(1)
            cont = 0
        else:   
            cont += 1
    tension += 1
print (chain)
Reply


Messages In This Thread
Help to find a command - by andre_fermart - Feb-02-2019, 08:06 AM
RE: Help to find a command - by stullis - Feb-02-2019, 04:15 PM
RE: Help to find a command - by perfringo - Feb-02-2019, 11:47 PM
RE: Help to find a command - by perfringo - Feb-03-2019, 12:51 PM
RE: Help to find a command - by andre_fermart - Feb-04-2019, 08:01 AM
RE: Help to find a command - by perfringo - Feb-04-2019, 09:49 AM
RE: Help to find a command - by andre_fermart - Feb-05-2019, 05:47 AM
RE: Help to find a command - by perfringo - Feb-05-2019, 09:01 AM
RE: Help to find a command - by perfringo - Feb-05-2019, 12:15 PM
RE: Help to find a command - by andre_fermart - Feb-12-2019, 02:34 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020