问题:我的代码在值[3]和[4]中有0时有效。但是,当我尝试添加我的实际值时,代码会混乱(见下文)。
此处说明了文本文件。
Joe,Bloggs,J.bloggs@anemailaddress.com,01269512355, 1,15, 0, 0, 0
FName, LName, Email, Number, Division, Points, ..., ...,
代码目标:代码应该取每个分区中的底部和前两个得分球员,并且-1或+1分区。如果低分则+1,如果高分则-1。所以,如果一个师有6人,那么2人会上升,2人会下来,2人会留在同一师。
代码:
f = open('test copy.txt', 'r')
lines = []
for line in f.readlines():
line = [x.strip() for x in line.split(',')]
line[4] = int(line[4])
line[5] = int(line[5])
lines.append(line)
f.close()
ordered = sorted(zip(range(len(lines)), lines), key=lambda x: x[1][3])
lines[ordered[-1][0]][4] += 1
lines[ordered[-2][0]][4] += 1
lines[ordered[0][0]][4] -= 1
lines[ordered[1][0]][4] -= 1
with open('test copy.txt', 'w') as f:
for line in lines:
line = [str(x) for x in line]
f.write(', '.join(line) + '\n')
有效的文字文件:
Joe,Bloggs,0,0, 1,15, 0, 0, 0,
Sarah,Brown,0,0, 1,12, 0, 0, 0,
Andrew,Smith,0,0, 1,4, 0, 0, 0,
Ray,Charles,0,0, 1,3, 0, 0, 0,
Kevin,White,0,0, 1,8, 0, 0, 0,
Samantha,Collins,0,0, 1,2, 0, 0, 0,
需要工作的测试文件但不包含:
Joe,Bloggs,J.bloggs@anemailaddress.com,01269512355, 1,15, 0, 0, 0
Sarah,Brown,S.brown@anemailaddress.com,01866522555, 1,12, 0, 0, 0
Andrew,Smith,A.smith@anemailaddress.com,01899512785, 1,4, 0, 0, 0
Ray,Charles,R.charles@anemailaddress.com,01268712321, 1,3, 0, 0, 0
Kevin,White,K.white@anemailaddress.com,01579122345, 1,8, 0, 0, 0
Samantha,Collins,S.collins@anemailaddress.com,04269916257, 1,2, 0, 0, 0
期望的结果:
Joe,Bloggs,J.bloggs@anemailaddress.com,01269512355, 0,15, 0, 0, 0
Sarah,Brown,S.brown@anemailaddress.com,01866522555, 0,12, 0, 0, 0
Andrew,Smith,A.smith@anemailaddress.com,01899512785, 1,4, 0, 0, 0
Ray,Charles,R.charles@anemailaddress.com,01268712321, 2,3, 0, 0, 0
Kevin,White,K.white@anemailaddress.com,01579122345, 1,8, 0, 0, 0
Samantha,Collins,S.collins@anemailaddress.com,04269916257, 2,2, 0, 0, 0
因为值[3]是一个整数,所以在整理出值[5]中的哪个数字更大然后将值[4]更改为正确的数字时,似乎正在弄乱代码。
这就是需要工作的文本文件:
Joe, Bloggs, J.bloggs@anemailaddress.com, 01269512355, 0, 15, 0, 0, 0
Sarah, Brown, S.brown@anemailaddress.com, 01866522555, 1, 12, 0, 0, 0
Andrew, Smith, A.smith@anemailaddress.com, 01899512785, 2, 4, 0, 0, 0
Ray, Charles, R.charles@anemailaddress.com, 01268712321, 0, 3, 0, 0, 0
Kevin, White, K.white@anemailaddress.com, 01579122345, 1, 8, 0, 0, 0
Samantha, Collins, S.collins@anemailaddress.com, 04269916257, 2, 2, 0, 0, 0
正如您所看到的,由于电话号码[3],它有所不同。
答案 0 :(得分:1)
编辑:问题是分数在第6列,因此key=lambda x: x[1][3]
应该是key=lambda x: x[1][5]
。
以下原始回复:
在我看来,您的代码是正确的并且正如您所期望的那样。但是,您所需的输出与您的算法不匹配 - 最高的两个Samantha
和Andrew
应该从1和1变为2和2,而Ray
应该保持不变在1
。
答案 1 :(得分:0)
我使用pandas和NumPy来解决这个问题。在以下源代码中,我解释了每个步骤:
import numpy as np
import pandas as pd
# Load data:
d = np.genfromtxt('copy.txt', dtype=np.str, delimiter=',')
# Optional: Remove last three columns:
d = d[:,:-3]
# Convert to a dataframe:
d = pd.DataFrame(data = d[:,:], columns=['fname', 'lname', 'email', 'number', 'division', 'points'])
# Set relevant datatypes (for sorting):
d[['division', 'points']] = d[['division', 'points']].astype(int)
# Sorting:
d = d.sort_values(by=['division', 'points'], ascending=[True, False])
# Reindexing:
d.index = range(1, len(d) + 1)
# Print the data structure:
print d[['fname', 'division', 'points']]
# fname division points
# 1 Joe 1 15
# 2 Andrew 1 4
# 3 Ray 1 3
# 4 Samantha 1 2
# 5 Sarah 2 12
# 6 Kevin 2 8
# Duplicate the column:
d['new_division'] = d['division']
def increase_first_elements (group, count):
group['new_division'][:count] -= 1
return group
def increase_last_elements (group, count):
group['new_division'][-count:] += 1
return group
# Reordering:
# In this example: Select first and last element (1).
# Important: Higher numbers (e.g. 2) work with larger tables (without select-intersections).
d = d.groupby('division').apply(increase_first_elements, 1)
d = d.groupby('division').apply(increase_last_elements, 1)
print d[['fname', 'division', 'new_division', 'points']]
# fname division new_division points
# 1 Joe 1 0 15
# 2 Andrew 1 1 4
# 3 Ray 1 1 3
# 4 Samantha 1 2 2
# 5 Sarah 2 1 12
# 6 Kevin 2 3 8
# Sorting:
d = d.sort_values(by=['new_division', 'points'], ascending=[True, False])
# Reindexing:
d.index = range(1, len(d) + 1)
# Cleanup:
d[['division']] = d[['new_division']].astype(int)
d = d.drop('new_division', 1)
print d[['fname', 'division', 'points']]
# fname division points
# 1 Joe 0 15
# 2 Sarah 1 12
# 3 Andrew 1 4
# 4 Ray 1 3
# 5 Samantha 2 2
# 6 Kevin 3 8
# Extra: If you want to reset the points:
d[['points']] = 0
print d[['fname', 'division', 'points']]
# fname division points
# 1 Joe 0 0
# 2 Sarah 1 0
# 3 Andrew 1 0
# 4 Ray 1 0
# 5 Samantha 2 0
# 6 Kevin 3 0