I have a list that looks like this -
item1,5,1,648,16,10,0,3,5,1,1,0,1,1,1,0,0,0,0,0,1,0,0,1,0,0
item2,3,1,29,3,6,0,0,3,1,0,0,1,0,1,0,0,0,0,0,1,0,0,0,1,0
item3,4,1,2388,20,8,2,0,3,1,1,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0
item4,6,3,0,0,1,0,0,5,1,0,1,1,1,0,1,0,0,0,0,0,0,1,1,1,0
What would be the best way of deleting the "item" part of the list?
I've tried
del list[j][0]
But then I get "TypeError: 'str' object doesn't support item deletion"
I've tried
list[j].remove(list[j][0])
But then I get AttributeError: 'str' object has no attribute 'remove'
Pop doesn't seem to work either - AttributeError: 'str' object has no attribute 'pop'
Any ideas?
EDIT: So I've read in a list -
data = open("flags.features", 'r').readlines()
Then just split each line -
for i in data:
input_list.append(i.split('\n')[0])
With an output of -
['item1,5,1,648,16,10,0,3,5,1,1,0,1,1,1,0,0,0,0,0,1,0,0,1,0,0', 'item2,3,1,29,3,6,0,0,3,1,0,0,1,0,1,0,0,0,0,0,1,0,0,0,1,0', 'item3,4,1,2388,20,8,2,0,3,1,1,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0', 'item4,6,3,0,0,1,0,0,5,1,0,1,1,1,0,1,0,0,0,0,0,0,1,1,1,0', 'item5,3,1,0,0,6,3,0,3,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0', '
CodePudding user response:
How about this?
data = open("flags.features", 'r').readlines()
input_list = []
for i in data:
input_list.append(",".join(i.split('\n')[0].split(",")[1:]))
This outputs
['5,1,648,16,10,0,3,5,1,1,0,1,1,1,0,0,0,0,0,1,0,0,1,0,0',
'3,1,29,3,6,0,0,3,1,0,0,1,0,1,0,0,0,0,0,1,0,0,0,1,0',
'4,1,2388,20,8,2,0,3,1,1,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0',
'6,3,0,0,1,0,0,5,1,0,1,1,1,0,1,0,0,0,0,0,0,1,1,1,0']
CodePudding user response:
You may try this:
my_list = [x.split(",") for x in my_list]
my_list[j].pop(0)
CodePudding user response:
You could do:
list[j].pop(1) #the 1 can be replaced with any number
This will index the list then pop the item in that list off.
This question is also very similar to: Using pop for removing element from 2D array