# python - 如何分别显示数组的每个元素

``````import socket
import numpy as np

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('192.168.0.1', 2015))

column1 = []
column2 = []
column3 = []
column4 = []
column5 = []
column6 = []
for message in range(10):
message = sock.recv(1024)

a1 = column1.append(message.split()[0])
a2 = column2.append(message.split()[1])
a3 = column3.append(message.split()[2])
a4 = column4.append(message.split()[3])
a5 = column5.append(message.split()[4])
a6 = column6.append(message.split()[5])

b1 =message.split()[0]
b2 = message.split()[1]
b3 = message.split()[2]
b4 = message.split()[3]
b5 = message.split()[4]
b6 = message.split()[5]
print b1
print b2
print b3
print b4
print b5
print b6
``````

#### 2 个答案:

``````import collections

message = """89 39 59 88 46 1 87 21 2 34
59 40 68 74 29 29 26 30 93 38
84 60 44 98 41 29 8 60 61 83
36 44 56 8 50 94 99 1 30 52
5 27 53 85 67 69 38 67 69 26
92 17 4 13 74 89 30 49 44 20"""

rows = message.splitlines()
data = []

for row in rows:
data.append(row.split())

columns = zip(*data)
total = 0
moving = collections.deque()

# Display the moving average for the first column
for entry in columns[0]:
value = int(entry)
moving.append(value)
total  = value

if len(moving) > 3:     # Length of moving average
total -= moving.popleft()

print "=  %.1f" % (value, total/float(len(moving)))
``````

`````` 89  89.0
59  74.0
84  77.3
36  59.7
5  41.7
92  44.3
``````

``````columns = [[] for _ in range(6)]
``````

``````for message in range(10):
message = sock.recv(1024)

splits = message.split(None, 5) # split into six pieces at most
``````

``````    for index, item in enumerate(splits):
columns[index].append(item)
``````

``````print columns[0][0]  # first item of first list
``````

0条回复