Tag: 列表

在Python中列表的第一个位置插入

如何在列表的第一个索引处插入一个元素? 如果我使用list.insert(0,elem),请elem修改第一个索引的内容? 或者,我必须创build一个新的列表与第一elem,然后复制在这个新的旧列表?

在列表中列出元素

这是我的代码,我需要在列表中总结一个未定义数量的元素。 这个怎么做? l = raw_input() l = l.split(' ') l.pop(0) 我的input: 3 5 4 9input后,我通过l.pop(0)删除第一个元素。 之后.split(' ')我的列表是['5', '4', '9'] ,我需要总结列表中的所有元素。 在这种情况下,总和是18.请注意,没有定义元素的数量。

如何从一个List <int>与LINQ获得最接近的数字?

如何从一个List<int>与LINQ获得最接近的数字? 例如: List<int> numbers = new List<int>(); numbers.Add(2); numbers.Add(5); numbers.Add(7); numbers.Add(10) 我需要find列表中最接近的数字9,在这种情况下10。 我怎样才能做到这一点与LINQ?

自动初始化C#列表

我正在创build一个新的C#列表( List<double> )。 有没有办法,除了在列表上循环,初始化所有的起始值为0?

如何设置ListPreference的默认值

当Activity开始时,我需要为ListPreference设置defult值。 我已经尝试了ListPreference.setDefaultvalue("value"); 但是它将默认列表的入口作为默认值。 我需要它,因为我必须检查一个条件,并设置为默认值满足该条件,所以我认为它不能从XML文件(与android:defaultValue ) 例如,假设我在arrays.xml中有这个值的数组: <string-array name="opts"> <item>red</item> <item>green</item> <item>blue</item> </string-array> <string-array name="opts_values"> <item>1</item> <item>2</item> <item>3</item> </string-array> 在PreferenceScreen xml中: <ListPreference android:title="Colour select" android:summary="Select your favourite" android:key="colour" android:entries="@array/opts" android:entryValues="@array/opts_values" /> 在活动中,我想要做这样的事情: String mycolour; if (something) { mycolour="1"; } else { mycolour="2"; } ListPreference colour = (ListPreference) findPreference ("colour"); colour.setDefaultValue(mycolour); 但它不起作用,因为它作为默认的首选。 你能解释一下如何使另一个默认? 谢谢。

java.util.AbstractList.add的UnsupportedOperationException

我有问题得到一个代码块正确运行。 我不完全确定这个代码是做什么的(我试图得到一个插件已经过时,可以正常使用我们的服务器),我只知道它每运行20分钟就会抛出一个错误。 以下是发生问题的部分代码: public class DynamicThread extends Thread { private LocalShops plugin = null; public DynamicThread(ThreadGroup tgroup, String tname, LocalShops plugin) { super(tgroup, tname); this.plugin = plugin; } public void run() { Map<ItemInfo, List<Integer>> itemStockMap = Collections.synchronizedMap(new HashMap<ItemInfo, List<Integer>>()); //Dump all the shop stock data into the map. for ( Shop shop : plugin.getShopManager().getAllShops() ) […]

为什么在Java中list.size()> 0比list.isEmpty()慢?

为什么在Java中list.size()>0比list.isEmpty()慢? 换句话说,为什么isEmpty()比size()>0更可取? 当我查看ArrayList中的实现时,看起来速度应该是相同的: ArrayList.size() /** * Returns the number of elements in this list. * * @return the number of elements in this list */ public int size() { return size; } ArrayList.isEmpty() /** * Returns <tt>true</tt> if this list contains no elements. * * @return <tt>true</tt> if this list contains no elements */ public […]

2个列表之间的通用元素比较

def common_elements(list1, list2): """ Return a list containing the elements which are in both list1 and list2 >>> common_elements([1,2,3,4,5,6], [3,5,7,9]) [3, 5] >>> common_elements(['this','this','n','that'],['this','not','that','that']) ['this', 'that'] """ for element in list1: if element in list2: return list(element) 到目前为止,但似乎无法得到它的工作! 谢谢

在C#中排列对象列表

public class CarSpecs { public String CarName { get; set; } public String CarMaker { get; set; } public DateTime CreationDate { get; set; } } 这是一个列表,我试图找出一个有效的方式来sorting列表CarList,包含6(或任何整数量)汽车,汽车制造date。 我打算做泡沫sorting,但将工作? 任何帮助? 谢谢

为什么Python中没有list.clear()方法?

受这个问题的启发。 为什么Python中没有list.clear()方法? 我在这里发现了几个问题,说明正确的做法是以下几点,但没有人说为什么不只是一个方法。 del lst[:] lst[:] = [] 虽然这可能违背了“python之禅”有不止一种做法,但对我来说,有一个“list.clear()”方法当然更为明显。 它也符合指令和集合,两者都有.clear()。 我偶然发现了一些有关python-dev和python的文章,并没有给出明确的答案(参见这里 (2006)和here (2009))。 Guido有权衡吗? 这只是一个争论点,在过去的4-5年里还没有解决吗? 更新: list.clear()被添加到3.3的Python中 – 请参阅此处