列表位置更改

2024-10-16 20:50:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经问过同样的问题了,但是我觉得我是怎么说的很模糊。 基本上我需要改变:

data = 
[['15674' '24000' 'Manager' 'Gregory the 1st' 'John'], 
['15674' '24000' 'Manager' 'Gregory the 1st' 'John'], 
['15674' '24000' 'Manager' 'Gregory the 1st' 'John'],
['15674' '24000' 'Manager' 'Gregory the 1st' 'John']] 

data = [number, salary,position, othernames, firstname] 

分为:

data1= 
('John', 'Gregory the 1st',15674,'Manager',24000),
('John', 'Gregory the 1st',15674,'Manager',24000), 
('John', 'Gregory the 1st',15674,'Manager',24000,) 
('John', 'Gregory the 1st',15674,'Manager',24000)

data1 = (Firstname, othernames, number, position,salary)

我重复了一遍,以表明我可以得到一个包含100名员工的文件,这些员工需要重新安排他们的信息,并按这个顺序以元组的形式打印出来。所有项目都将具有相同的位置,例如number[0]salary[1]position [2],但是中间部分即其他名称可能有多个名称,因此它不会有一个明确的位置。但是名字只能是一个项目,因此可以通过list1[-1]找到。你知道吗

def ex1 ():
    b= input("Please enter a file name to be opened: ")
    a = (b+".txt")    
    data =[]    
def employee(lanme, oname, num,title,salary):
    return (lanme, oname, num, title, salary)

def readfile(a):
    try:
        data =[]
        check = open(a, 'r')
        line =check.readlines()
        for items in line:
            breakup= items.split()
            data.append(breakup)
    except IOError as e :
        print("Failed to open", fileName)
readfile(a)

例1()


Tags: theto项目名称numberdatadef员工
2条回答

如果您的输入数据是有效的,则可以很容易地完成重新排序。你知道吗

# First, Last, Number, Position, Salary
data = [(f,l,n,p,s) for n,s,p,l,f in data]

只有当您的输入值是一个列表列表时,这才会起作用。你的样品无效。你知道吗

我的解决方案是列表列表,但很简单。你知道吗

data = ["15674 24000 Manager Gregory the 1st John", "15674 24000 Manager Gregory the 1st John",
        "15674 24000 Manager Gregory the 1st John", "15674 24000 Manager Gregory the 1st John"]

data1 = []

for items in data:
    splitNames = items.split()
    number, salary, position, first, des1, des2, last = splitNames
    data1.append([last, first + ' ' + des1 + ' ' + des2, number, position, salary])

for items in data1:
    print items

结果:

['John', 'Gregory the 1st', '15674', 'Manager', '24000']
['John', 'Gregory the 1st', '15674', 'Manager', '24000']
['John', 'Gregory the 1st', '15674', 'Manager', '24000']
['John', 'Gregory the 1st', '15674', 'Manager', '24000']

这不是最好的解决方案,我是这样写的,所以很容易理解,因为这是基本的操作。你知道吗

元组:

如果您想让它成为元组列表,只需重写这一行:

data1.append(tuple([last, first + ' ' + des1 + ' ' + des2, number, position, salary]))

通过使用tuple(),请参阅python文档中的解释here。你知道吗

结果:

('John', 'Gregory the 1st', '15674', 'Manager', '24000')
('John', 'Gregory the 1st', '15674', 'Manager', '24000')
('John', 'Gregory the 1st', '15674', 'Manager', '24000')
('John', 'Gregory the 1st', '15674', 'Manager', '24000')

适应您的代码:

for items in line:
    breakup= items.split()
    number, salary, position, first, des1, des2, last = breakup
    data.append(tuple([last, first + ' ' + des1 + ' ' + des2, number, position, salary]))

print data

结果:

[('John', 'Gregory the 1st', '15674', 'Manager', '24000'), ('John', 'Gregory the 1st', '15674', 'Manager', '24000'), ('John', 'Gregory the 1st', '15674', 'Manager', '24000'), ('John', 'Gregory the 1st', '15674', 'Manager', '24000')]

相关问题 更多 >