Sorry, I'm new. Pliz tell me how out of three numbers, for example 10, 20, 30, I create an argument for the average number, I mean for the number "20". For the numbers 10 and 30, I did this:

a=float(sys.argv[1]) b=float(sys.argv[2]) c=float(sys.argv[3]) Amax= max (a, b, c) Bmin= min (a, b, c) 

But to the number 20 I can not get it. Thought so: Csr! = Amax or Bmin but gives an error. Thanks in advance!

  • Specify your question, in the current wording it is not at all clear what you want to achieve. - fori1ton
  • I need to further compare Amax, Bmin and Csr - Bogdan
  • What should be in Csr? And how do you want to compare them? - fori1ton
  • Csr should be average. And then I want to compare like this: if (Bmin + Csr <Amax): print "triangle" else: print "not triangle" - Bogdan
  • @Bogdan: What does the word "mean" mean in your case? L = [a, b, c] ; mean = average = sum(L)/len(L) ; median = L[len(L)//2] In this, the average value is equal to the median - jfs

2 answers 2

The easiest way to do this is as follows:

  1. With the help of the built-in map function, we turn the cut of the sys.argv list into a sequence of real numbers.
  2. With sorted, we get a list of sorted numbers from this sequence.
  3. We use sequence unpacking to get three numbers separately.

I.e:

 Bmin, Csr, Amax = sorted(map(float, sys.argv[1:4])) 

(using your notation; in general, it is better not to call variables so).

    In order to find which of the three numbers is greater, lesser and average, you can do this:

     l = sys.argv[1:4] # ΠΊΠ»Π°Π΄Ρ‘ΠΌ 3 Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π° ΠΊΠΎΠΌΠ°Π½Π΄Π½ΠΎΠΉ строки Π² список l.sort(key=lambda x: float(x)) # сортируСм Π΅Π³ΠΎ # Ρ‚Π°ΠΊ ΠΊΠ°ΠΊ элСмСнты списка Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ² ΠΊΠΎΠΌΠ°Π½Π΄Π½ΠΎΠΉ строки ΠΈΠΌΠ΅ΡŽΡ‚ стип string, # Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ привСсти ΠΈΡ… ΠΊ числовому Ρ‚ΠΈΠΏΡƒ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ Π½Π΅ Π² лСксикографичСском, # Π° Π² числовом порядкС # для этого Π² качСствС ΠΈΠΌΠ΅Π½ΠΎΠ²Π°Π½Π½ΠΎΠ³ΠΎ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π° key Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ sort ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‘ΠΌ Π°Π½ΠΎΠ½ΠΈΠΌΠ½ΡƒΡŽ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ, # ΠΎΡΡƒΡ‰Π΅ΡΡ‚Π²Π»ΡΡŽΡ‰ΡƒΡŽ Ρ‚Π°ΠΊΠΎΠ΅ ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ Bmin, Csr, Amax = l # ΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌΡΡ ΠΏΡ€ΠΈΡΡ‚Π½Π΅ΠΉΡˆΠ΅ΠΉ Ρ„ΠΈΡ‡Π΅ΠΉ Python'Π° - мноТСствСнным присваиваниСм 

    Or even shorter:

     Bmin, Csr, Amax = sorted(sys.argv[1:4], key=lambda x: float(x)) 

    Then we do with Amax , Csr and Bmin everything your heart desires.

    • Thank you very much, and you can, if not difficult, decipher with human language))) sys.argv - the list of command line arguments passed to the Python script. sys.argv [0] is the name of the script (empty string in the interactive shell). - Bogdan
    • Sorry, but I don't understand why when I do: import sys a = float (sys.argv [1]) b = float (sys.argv [2]) c = float (sys.argv [3]) Bmin, Csr, Amax = sorted (sys.argv [1: 4]) print Bmin, Csr, Amax and enter, for example, 100, 60, 80, it does not arrange them in order)) Sorry again, but it is always hard to start learning)) ) - Bogdan
    • I apologize for a small dezinormatsiyu, corrected the answer. The command line arguments are of type string , respectively, sorted in lexicographical order. In order to sort them ka numbers, you need to bring them to a numeric type. For this, the key parameter of the sort and sorted functions is sorted . It forces these functions to sort not the elements of the list directly, but the results of applying some function to them. In this case, cast them to type float . - fori1ton
    • The first option (with explanations) gives an error, and the second, which is shorter, works. Many thanks - Bogdan
    • one
      key=lambda x: float(x) can be replaced by key=float . - jfs