Python:如何从列表元素中检索一些值?

时间:2016-02-05 11:06:18

标签: python list extraction

我有一个像这样的元素列表:

mylist=['event_100of1000', 'event_17of1000', 'event_1000of1000',...]

如何仅提取事件的“数字”并生成另一个列表,如下所示:

extracted_list=['100','17','1000',...]

7 个答案:

答案 0 :(得分:3)

你可以使用'正则表达式'....

>>> import re
>>> mylist=['event_100of1000', 'event_17of1000', 'event_1000of1000']
>>> [re.findall('_(\d+)', i)[0] for i in mylist]

['100', '17', '1000']

答案 1 :(得分:3)

您可以lstrip使用event_split使用'of'

res = [i.lstrip('event_').split('of')[0] for i in mylist]

print(res)
['100', '17', '1000']

修改

res = [int(i.lstrip('event_').split('of')[0]) for i in mylist]

print(res)
[100, 17, 1000]

答案 2 :(得分:2)

您可以使用地图:

  events = map(lambda x: x.split('of')[0], mylist)
  map(lambda x: x.split('_')[1], events)

答案 3 :(得分:1)

使用列表理解和正则表达式:

import re
mylist=['event_100of1000', 'event_17of1000', 'event_1000of1000',...]
extracted_list = [re.search("\d+", x).group(0) for x in mylist]

答案 4 :(得分:1)

我也会用re

var markers = ["chennai", "Kashmir", "Russia", "Jalandhar", "Netherlands", "Koregaon Park", "Piparia", "South Africa", "USA", "Siliguri", "Dhule", "United Kingdom", "Shendra", "Baramulla", "Haridwar", "New Delhi", "United Arab Emirates", "Ladakh", "Noida", "Shanghai", "Gurgaon", "Rajouri", "Netherlands", "Ranchi", "Abruzzo", "Waluj", "Ho Chi Minh", "Germany", "Bhopal", "Soenderborg", "Delhi", "Dindori", "Brazil", "Magarpatta", "Chennai", "Taiwan", "Jharsuguda", "Rakholi", "Turkey", "Denmark", "Bangalore", "Selangor", "Rajgarh", "Vardhaman Industrial Estate", "Sagar", "Jammu", "Beijing", "Jamshedpur", "Jabalpur", "SIDCUL", "Hyderabad", "PTB Rakholi", "Haimen", "Ahmedabad", "OFC Rakholi", "Mumbai", "Bafliaz", "Srinagar", "Argentina", "Dadra", "Mexico", "Tiruchirappalli"]
var map;
function initializeMaps(eventObject)
{
    

   map = new google.maps.Map(document.getElementById('googlemap1'), {
      zoom: 5,
      center: new google.maps.LatLng(21.0000,78.0000),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();
    var geocoder = new google.maps.Geocoder();

    var marker, i;

    for (i = 0; i < markers.length; i++) {
      geocodeAddress(markers[i]);
    }
   
}
function geocodeAddress(location) {
var geocoder= new google.maps.Geocoder();
  geocoder.geocode( { 'address': location}, function(results, status) {
 // alert(status);
    if (status == google.maps.GeocoderStatus.OK) {

     // alert(results[0].geometry.location);
     // map.setCenter(results[0].geometry.location);
      createMarker(results[0].geometry.location,location);
    
    }
    else
    {
      alert("some problem in geocode" + status);
    }
  }); 
}








function createMarker(latlng){
  var marker = new google.maps.Marker({
    position: latlng,
    map: map
  }); 

答案 5 :(得分:1)

使用重新

import re
mylist=['event_100of1000', 'event_17of1000', 'event_1000of1000']
pattern = re.compile(r'^event_(\d+).')

mylist = list(map(lambda x : pattern.findall(x)[0] , mylist))
print(mylist)
#['100', '17', '1000']

答案 6 :(得分:0)

使用map功能:

>>> extracted_list = map(lambda x: int(x[:x.find('of')].lstrip('event_')), mylist)
>>> print extracted_list
[100, 17, 1000]