Counting entries of a list
Question:
I have a list:
final_list =['Angle1 &110.2 \\', 'Angle1 &110.2 \\', 'Angle3 (2) \\', 'Angle4 & 93 (2) \\']
for entry in final_list:
print(entry)
For Latex formatting I need to connect these entries (over 100 entries total).
The list should look like this:
OUTPUT =
Angle1 &110.2 &
Angle2 &110.2 \\
Angle3 (2) &
Angle4 & 93 (2) \\
I’ve tried to count the occurrences of "\\"
in the entry
counter_slash = 0
for entry in final_list:
counter_slash = final_list.count(entry)
if counter_slash == 1:
counter_slash += 1
entry = entry.replace("\\", "&")
print(entry)
elif counter_slash == 2:
counter_slash = 0
print(entry)
However my output looks like this:
OUTPUT =
Angle1 &110.2 &
Angle1 &110.2 &
Angle3 (2) &
Angle4 & 93 (2) &
Answers:
What about this?
final_list =[ 'Angle1 &110.2 \\',
'Angle1 &110.2 \\',
'Angle3 (2) \\',
'Angle4 & 93 (2) \\']
for i, entry in enumerate(final_list):
final_list[i] = entry.replace(" \\", "")
print(f'{final_list[0]} & {final_list[1]} \\ {final_list[2]} & {final_list[3]} \\')
So what you’re doing with counter_slash = final_list.count(entry)
is count how often entry
is found in final_list
. Which, if they’re all different, is going to be 1 for every list item.
It looks like you want to replace the \
in every other item with &
, in which case you could do the following:
for i, entry in enumerate(final_list):
if i % 2 == 0:
print(entry.replace('\\', '&'))
else:
print(entry)
I have a list:
final_list =['Angle1 &110.2 \\', 'Angle1 &110.2 \\', 'Angle3 (2) \\', 'Angle4 & 93 (2) \\']
for entry in final_list:
print(entry)
For Latex formatting I need to connect these entries (over 100 entries total).
The list should look like this:
OUTPUT =
Angle1 &110.2 &
Angle2 &110.2 \\
Angle3 (2) &
Angle4 & 93 (2) \\
I’ve tried to count the occurrences of "\\"
in the entry
counter_slash = 0
for entry in final_list:
counter_slash = final_list.count(entry)
if counter_slash == 1:
counter_slash += 1
entry = entry.replace("\\", "&")
print(entry)
elif counter_slash == 2:
counter_slash = 0
print(entry)
However my output looks like this:
OUTPUT =
Angle1 &110.2 &
Angle1 &110.2 &
Angle3 (2) &
Angle4 & 93 (2) &
What about this?
final_list =[ 'Angle1 &110.2 \\',
'Angle1 &110.2 \\',
'Angle3 (2) \\',
'Angle4 & 93 (2) \\']
for i, entry in enumerate(final_list):
final_list[i] = entry.replace(" \\", "")
print(f'{final_list[0]} & {final_list[1]} \\ {final_list[2]} & {final_list[3]} \\')
So what you’re doing with counter_slash = final_list.count(entry)
is count how often entry
is found in final_list
. Which, if they’re all different, is going to be 1 for every list item.
It looks like you want to replace the \
in every other item with &
, in which case you could do the following:
for i, entry in enumerate(final_list):
if i % 2 == 0:
print(entry.replace('\\', '&'))
else:
print(entry)