I have a list of lists where some lists are not complete. I.e.:
data = [
['id1',1],
['id2'],
['id3'],
['id4'],
['id5',1]
]
I want to create a new list that contains only "full" lists.
Desired output:
[
['id1',1],
['id5',1]
]
I have tried:
new_data = [i for i in data if i[1]]
However, this returned:
List index error: Index out of range
I find it odd because the if[1] is right there.
Question
Is there a simple fix for a one-line approach, or must we use a for loop?
Instead of trying accessing the values, check for length:
new_data = [i for i in data if len(i) == 2]
Related
I am using Python 3.9.4 with openpyxl.
I have an Excel Workbook with 4 Worksheets.
I have written some code to rename the Worksheets that work.
names = ['Ja', 'Ro', 'Ra', 'El']
idx = 0
for w_sheet in w_book:
w_sheet.title = names[idx]
idx += 1
I would like to convert this to List Comprehension and have written the following code (which does not work).
names = ['Ja', 'Ro', 'Ra', 'El']
[w_book._sheets[i].title for i in names]
I am getting the Error:
TypeError: list indices must be integers or slices, not str
How can I reference the list of names?
I'm not sure why you want to do this inside a list comprehension: these should, in general, not be used for flow control.
The code, as it stands is also error prone as it depends upon an implicit ordering of the worksheets. In general, you should probably use a mapping that explicitly maps one sheet name to another.
But, if you just want to use a list, then the simplest approach is to use zip
for sheet, name in zip(w_book, names):
sheet.title = name
This can be adapted for a mapping
names = {"foo": "Ja", "baz": "Ro", "bar": "Ra", "bop": "El"}
for old, new in names.items():
wb[old].title = new
I want to determine the rows in a data frame that has the same value in some special columns (sex, work class, education).
new_row_data=df.head(20)
new_center_clusters =new_row_data.head(20)
for j in range(len(new_center_clusters)):
row=[]
for i in range(len(new_row_data)):
if (new_center_clusters.iloc[j][5] == new_row_data.iloc[i][5]):
if(new_center_clusters.iloc[j][2] == new_row_data.iloc[i][2]):
if(new_center_clusters.iloc[j][3] == new_row_data.iloc[i][3]):
if(new_center_clusters.iloc[j][0] != new_center_clusters.iloc[i][0]):
row.append(new_center_clusters.iloc[j][0])
row.append(new_center_clusters.iloc[i][0])
myset = list(set(row))
myset.sort()
print(myset)
I need a list that includes all the IDs of similar rows in one list. but I can not merge all the lists in one list.
I get this result:
I need to get like this:
[1,12,8,17,3,18,4,19,5,13,6,9]
Thank you in advance.
if you want combine all list
a=[1,3,4]
b=[2,4,1]
a.extend(b)
it will give output as:
[1,3,4,2,4,1]
similary if you want to remove the duplicates, convert it into set and again list:
c=list(set(a))
it will give output as:
[1,3,4,2]
I am currently trying to write a function to iterate through a nested list and check if one item from the list, 'team', is already in a separate list 'teams'.
If it is not, I want to append a nested list, 'player_values' with a different item from the original nested list that was examined, in the form of a new list in the nested list.
If it is, I want to append the nested list 'player_values' with the item from the original nested list, but I want to add it to the most recent list in the nested list 'player_values' instead of creating a new list.
Currently, my code looks like this :
def teams_and_games(list, player, idx):
teams = []
player_values = []
x = 0
y = -1
for rows in list:
if player == list[x][BD.player_id] and list[x][BD.team] not in teams:
teams.append(list[x][BD.team])
player_values.append([list[x][idx]])
x += 1
y += 1
elif player == list[x][BD.player_id]:
player_values[y].append(list[x][idx])
x += 1
return player_values, teams
However, when I run the code in my main, using
values, teams = teams_and_games(NiceRow, name, BD.games)
print(values)
print(teams)
It only prints empty lists. The fact that it prints empty lists shows that it is returning the correct variables, but I can't figure out why the code in the function is failing to add anything to the lists. I have tried switching the .append with a more simple list += statement, but the result has been the same so far.
Ideally, I would be getting a nested list, containing an amount of lists equal to the number of items added to the other 'teams' list, and the list of teams in the order they were added.
The data I am working with is a nested list pulled from a .csv file, which has been formatted slightly using the .strip() and .split() commands. Each number has been converted to an int, and strings left as they are. The .CSV file it is from has 19 columns and ~80,000 rows, with each column always being either a string or an int.
I am a newbie to python and learning it from a book, as suggested in this forum by one of the developers. I am trying to create the following list
alist == ['47c', ['47d','47e'], '47f']
Usually, we do this with the square brackets [ ] to create a list. Is there a way to create 'alist' without using 'square bracket delimiters' [ and ] in my code?
Thanks in advance for help!
# I know of following way and few other methods but they all use [ and ]
alist = ['47c', '47f']
blist = ['47d','47e']
alist.insert(1,blist)
Yes, we can create list without using square brackets.
To do so, create a list using the list() constructor and then append item to it.
aList = list()
aList.append('47c')
innerList = list()
innerList.append('47d')
innerList.append('47e')
aList.append(innerList)
aList.append('47f')
print(aList)
Output:
['47c', ['47d', '47e'], '47f']
Explanation:
The list() constructor returns a mutable sequence list of elements.
If no parameters are passed, it creates an empty list.
If iterable is passed as parameter, it creates a list of elements in the iterable.
Reference:
List tutorial in programiz site
I have a large dictionary and I am trying to remove values from keys if they start with certain values. Below is a small example of the dictionary.
a_data = {'78567908': {'26.01.17', '02.03.24', '26.01.12', '04.03.03', '01.01.13', '02.03.01', '01.01.10', '26.01.21'}, '85789070': {'26.01.02', '09.01.04', '02.05.04', '02.03.17', '02.05.01'}, '87140110': {'03.15.25', '03.15.24', '03.15.19'}, '87142218': {'26.17.13', '02.11.01', '02.03.22'}, '87006826': {'28.01.03'}}
After I read in the dictionary, I want to remove values from all keys that start with '26.' or '02.' It is possible that would leave a key with no values (an empty set).
I do have code that works:
exclude = ('26.', '02.')
f_a_data = {}
for k, v in a_data.items():
f_a_data.setdefault(k,[])
for code in v:
print (k, code, not code.startswith(exclude))
if not code.startswith(exclude):
f_a_data[k].append(code)
print('Filtered dict:')
print(f_a_data)
This returns a filtered dict:
Filtered dict:
{'78567908': ['04.03.03', '01.01.13', '01.01.10'], '85789070': ['09.01.04'], '87140110': ['03.15.25', '03.15.24', '03.15.19'], '87142218': [], '87006826': ['28.01.03']}
Question 1: Is this the best way to filter a dictionary?
Question 2: How could i modify the above snippet to return values in a set like the original dict?
Your code is quite all right in complexity terms but can be "pythonized" a little and still remain readable.
My proposal: you can rebuild a dictionary using nested comprehensions and all to test if you should include the values:
a_data = {'78567908': {'26.01.17', '02.03.24', '26.01.12', '04.03.03', '01.01.13', '02.03.01', '01.01.10', '26.01.21'}, '85789070': {'26.01.02', '09.01.04', '02.05.04', '02.03.17', '02.05.01'}, '87140110': {'03.15.25', '03.15.24', '03.15.19'}, '87142218': {'26.17.13', '02.11.01', '02.03.22'}, '87006826': {'28.01.03'}}
exclude = ('26.', '02.')
new_data = {k:{x for x in v if all(s not in x for s in exclude)} for k,v in a_data.items()}
result:
>>> new_data
{'78567908': {'01.01.10', '01.01.13', '04.03.03'},
'85789070': {'09.01.04'},
'87006826': {'28.01.03'},
'87140110': {'03.15.19', '03.15.25', '03.15.24'},
'87142218': set()}
(here using a dictionary comprehension embedding a set comprehension (since you need a set) using a generator comprehension in all)