打印连续数字(Printing consecutive numbers)
我已经对整数数组{1,2,3,7,9,24,25,26,78}进行了排序,并希望连续打印以打印{1-3,7,9,24-26,78}。 也就是说,每次在阵列中出现一组连续数字时,我想打印出从最小数字到最大数字的范围。
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Consecutive{ public static void main(String[] args){ int[] a={1,2,3,7,9,24,25,26,78}; for(int i=0;i<a.length;i++){ int count=0; int first=0; /* System.out.println(i);*/ first=a[i]; if(a[i+1]-a[i]==1){ count++; int last=a[i]+count; i++; System.out.println(first + " " + last); }else{ System.out.println(a[i]); } } } }
I have sorted integer array {1,2,3,7,9,24,25,26,78} and would like to print consecutive to print {1-3,7,9,24-26,78}. That is, every time there is a set of consecutive numbers occurring in the array, I would like to print out the range from the minimum number to the maximum number.
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Consecutive{ public static void main(String[] args){ int[] a={1,2,3,7,9,24,25,26,78}; for(int i=0;i<a.length;i++){ int count=0; int first=0; /* System.out.println(i);*/ first=a[i]; if(a[i+1]-a[i]==1){ count++; int last=a[i]+count; i++; System.out.println(first + " " + last); }else{ System.out.println(a[i]); } } } }
原文:https://stackoverflow.com/questions/36963019
最满意答案
简短回答:使用大括号$ \ alpha_ {mod} $
更长的版本:Matplotlib使用LaTeX格式的字符串。 在LaTeX中,如果您希望下标包含多个字母,则必须使用大括号。
import matplotlib.pyplot as plt ax = plt.gca() ax.text(2, 2, r'$\alpha_{mod}$', fontsize=10) y = [2,3,4,5] x = [1,2,3,4] plt.plot(x,y) plt.show()
Short answer: Use curly brackets $\alpha_{mod}$
Longer version: Matplotlib uses LaTeX format for strings. In LaTeX, if you want a subscript to include more than one letter you have to use curly brackets.
import matplotlib.pyplot as plt ax = plt.gca() ax.text(2, 2, r'$\alpha_{mod}$', fontsize=10) y = [2,3,4,5] x = [1,2,3,4] plt.plot(x,y) plt.show()
相关问答
更多-
轴是情节的轴心,即获得刻度和刻度标签的东西。 坐标轴是您的绘图出现的区域。 Axis is the axis of the plot, the thing that gets ticks and tick labels. The axes is the area your plot appears in.
-
Matplotlib中的坐标轴位置相似。 您可以使用轴的get_position和set_position方法。 import matplotlib.pyplot as plt ax = plt.subplot(111) pos1 = ax.get_position() # get the original position pos2 = [pos1.x0 + 0.3, pos1.y0 + 0.3, pos1.width / 2.0, pos1.height / 2.0] ax.set_positi ...
-
在matplotlib轴下标(Subscripts in matplotlib axes)[2022-04-10]
简短回答:使用大括号$ \ alpha_ {mod} $ 更长的版本:Matplotlib使用LaTeX格式的字符串。 在LaTeX中,如果您希望下标包含多个字母,则必须使用大括号。 import matplotlib.pyplot as plt ax = plt.gca() ax.text(2, 2, r'$\alpha_{mod}$', fontsize=10) y = [2,3,4,5] x = [1,2,3,4] plt.plot(x,y) plt.show() Short answer: Use ... -
您可以从网格获取轴手柄,这只是一个带有ax=grid[3]的列表,然后使用xticks = ax.xaxis.get_major_ticks()和xticks[1].label1.set_visible(False) 。 作为一个最小的例子, import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import AxesGrid import numpy as np from matplotlib.cbook import get_sampl ...
-
我想你几乎在那里。 不知道你的绘图功能是什么,我只是做了一个虚拟的插图。 import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec def dummy_plots(): """ Return a 1d array of dummy plots. """ _, ax_arr = plt.subplots(1, 9) for ax in ax_arr.flat: ax ...
-
matplotlib:改变轴(matplotlib: change axes)[2023-08-14]
抱歉,而不是xlabels你需要创建一个轴,我们ax.set_xticklabels: from matplotlib.pyplot import figure fig = figure() ax = fig.add_subplot(111) def mk_labels(vals): labels = [] for i in vals: if i % 60 == 0: labels.append("Some new special label") ... -
正如我在评论中所说,阅读如何将pyplot函数附加到图形实例? 有关Op和matplotlib状态机接口之间差异的解释。 你应该修改你的绘图功能 def plot(..., ax=None, **kwargs): if ax is None: ax = gca() ax.plot(..., **kwargs) As I said in the comment, read How can I attach a pyplot function to a figure insta ...
-
动画matplotlib轴/蜱(Animating matplotlib axes/ticks)[2023-12-12]
轴bbox不包括轴的“内部”之外的任何内容(例如,它不包括刻度标签,标题等) 一个快速解决这个问题的方法就是在你进行blitting时抓住整个区域。 (例如background = canvas.copy_from_bbox(fig.bbox) ) 如果您有多个子图并且只想为其中一个子图设置动画,则可能会出现问题。 在这种情况下,您可以按照background = canvas.copy_from_bbox(ax.bbox.expanded(1.1, 1.2)) 。 但是你必须猜测你需要的比率。 如果你需要 ... -
以下两件事对我有用(注意你的大写): from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.mplot3d import axes3d 但是我为你所写的内容得到了同样的错误(用小写的d表示d)。 请注意,两个有效的导入不相同。 The following two things work for me (watch your capitalization): from mpl_toolkits.mplot3d import Axes3D from ...
-
Matplotlib子图轴(Matplotlib subplot axes)[2022-07-13]
您需要告诉netwkorkx要绘制哪些轴,如果不这样,它将绘制到当前活动的轴( plt.gca()返回( doc )。 nx.drawing.nx_pylab.draw_networkx_nodes(..., ax=Axes[0]) nx.drawing.nx_pylab.draw_networkx_nodes(..., ax=Axes[1]) 作为旁注,你不应该使用驼峰式实例变量( pep8 ),它可以倾向于与类名冲突(在本例中为matplotlib.axes.Axes )。 You need to t ...