2015-11-16 137 views
-1

我有這個測試文件:我怎樣才能做數學項目在Python中的列表?

123 52 65 75 
40 58 34 8 

98 89 89 98 
45 34 29 49 

,我想借此每兩個數字,它們相乘。我有一些問題。我知道我應該做一些任務,我會從x = 0開始,以x + = 1結束一個計數器和任何事情,但我需要幫助開始。

這是我到目前爲止有:

我有一個文件,該文件的全數字,我想取文件中每三個數字加在一起,然後除以2,這裏是我的到目前爲止:

text= input('file: ') 
f = open(text, 'r') 
for line in f: 
    counter = 0 
    for n in line: 
     counter *= int(n) 
    print(counter) 

這將行中的所有數字相乘,但我只是想每兩個。我覺得我很接近但需要一些建議。

+0

什麼是你期望的輸出? –

+0

我想得到624 4875,等等,只是讓他們乘以等等。 – user5566334

+0

如果一行包含5個數字,該怎麼辦? –

回答

-1

使用inline for。 這樣的事情?

[line[n] * line[n+1] for range(0,len(line),2)] 
+0

「內聯」被稱爲理解,我認爲你錯過了一些東西。在解釋器會話中嘗試解決多個錯誤。 – TigerhawkT3

1

正如皮特所說,你可以在列表理解中做到這一點。

對於初學者,我認爲for循環更容易閱讀。在你的對象上調用enumerate()會給你一個迭代器。

line = [4, 5, 7, 8] 
product = 1 
# loop_number counts up from 1 
for loop_number, value in enumerate(line, start=1): 
    print('LOOP_NUMBER_(ITERATOR)_IS: ' + str(loop_number)) 
    product *= value 
    print('PRODUCT IS NOW: ' + str(product)) 
    if loop_number % 2 == 0: 
     print('OUTPUT PRODUCT: ' + str(product)) 
     product = 1 

輸出:

LOOP_NUMBER_(ITERATOR)_IS: 1 
PRODUCT IS NOW: 4 
LOOP_NUMBER_(ITERATOR)_IS: 2 
PRODUCT IS NOW: 20 
OUTPUT PRODUCT: 20 
LOOP_NUMBER_(ITERATOR)_IS: 3 
PRODUCT IS NOW: 7 
LOOP_NUMBER_(ITERATOR)_IS: 4 
PRODUCT IS NOW: 56 
OUTPUT PRODUCT: 56 
+0

迭代器是什麼意思?枚舉?我應該先參加物理課程嗎? – 7stud

+0

與物理無關,哈哈。迭代器是每次通過循環時「計數」(迭代,增量)的東西。 Enumerate只是一個內置的python函數,當你循環的時候,你可以在'value'之外給你'loop_number'。 – NotAnAmbiTurner

+0

我編輯了答案,希望能更清楚一點。 – NotAnAmbiTurner

0

我想借此每兩個數字,它們相乘。

它總是最好要發佈:

  1. 你的出發數據。

    [1, 2, 3, 4] 
    
    1 * 2 => 2 
    3 * 4 => 12 
    

    ,那麼你可以這樣做::

    import csv 
    import itertools as it 
    
    with open('data.txt', newline='') as f: 
        csv_reader = csv.reader(f, delimiter=' ') 
    
        for row in csv_reader: 
         print(row) #=> ['1', '2', '3', '4'] 
    
         every_other0 = it.islice(row, 0, False, 2) #Produces items from the row at index 0, 2, etc. 
         every_other1 = it.islice(row, 1, False, 2) #Produces items from the row at index 1, 3, etc. 
    
         while True: 
          str1 = next(every_other0, None) #if there are no more items in every_other0, return False 
          str2 = next(every_other1, None) #if there are no more items in every_other1, return False 
    
          if str1 and str2: #if either str1 or str2 is False, the condition is False 
           num1 = int(str1) 
           num2 = int(str2) 
           print("{} * {} = {}".format(num1, num2, num1*num2)) 
          else: 
           break 
    

    $ cat data.txt 
    1 2 3 4 
    5 6 7 8 
    
    ~/python_programs$ python3.4 prog.py 
    ['1', '2', '3', '4'] 
    1 * 2 = 2 
    3 * 4 = 12 
    ['5', '6', '7', '8'] 
    5 * 6 = 30 
    7 * 8 = 56 
    
  2. 你想

如果你想乘數對在一起,像這樣做的實際結果


基於您的代碼在這裏

counter = 0 
for n in line: 
    counter *= int(n) 

也許你想要的東西,像下面這樣:

import operator as op 
import functools as funcs 
import csv 

with open('data.txt', newline='') as f: #opens the file and automatically closes the file when the block is exited for any reason--including if an exception occurs 
    csv_reader = csv.reader(f, delimiter=' ') 

    for row in csv_reader: 
     every_other = [int(item) for item in row[::2] ] #=> row[::2] is equivalent to row[0:len(row):2], which means get the element at index 0, then add the step, 2, to the index value, and get the next element, etc., and stop at the index len(row). 
     print(every_other) 

[1, 1, 1] 

 result = funcs.reduce(op.mul, every_other) #multiply all the numbers in every_other together, reducing them to one number 
     print(result) 

1 

 every_other = [int(item) for item in row[1::2] ] #=> row[1::2] is equivalent to row[1:len(row):2], which means start at index 1 and stop at a the end of row, and get the element at index 1; add the step, 2, to the index value, and get the element at that index, etc. 
     print(every_other) 

[2, 2, 2] 

 result = funcs.reduce(op.mul, every_other) 
     print(result) 

8